Add Radio and Toggle form fields (closes #160)#388
Draft
MukundaKatta wants to merge 1 commit intopydantic:mainfrom
Draft
Add Radio and Toggle form fields (closes #160)#388MukundaKatta wants to merge 1 commit intopydantic:mainfrom
MukundaKatta wants to merge 1 commit intopydantic:mainfrom
Conversation
Closes pydantic#160. Adds two new form field components: - FormFieldRadio: renders a string-Enum/Literal field as a radio button group. Triggered by `Radio()` (or json_schema_extra={"format": "radio"}) on the Pydantic field. Supports `inline` for horizontal layout. - FormFieldToggle: dedicated on/off switch component. Triggered by `Toggle()` (or json_schema_extra={"format": "toggle"}). Optional on_label / off_label render next to the switch. Multi-value fields (list[Enum]) annotated with Radio() fall back to the existing select component, since radios can't represent multi-select. Frontend: - src/npm-fastui/src/components/FormField.tsx: new FormFieldRadioComp and FormFieldToggleComp following the existing FormFieldBooleanComp / FormFieldSelectVanillaComp patterns. Pre-computes useClassName values to satisfy react-hooks/rules-of-hooks. - src/npm-fastui/src/components/index.tsx: wires the new types into the AnyComp switch. - src/npm-fastui/src/models.d.ts: matching TypeScript interfaces. - src/npm-fastui-bootstrap/src/index.tsx: Bootstrap classNameGenerator cases for `form-check`, `form-switch`, radio sub-elements. - src/npm-fastui-bootstrap/src/Radio.tsx, Toggle.tsx: thin re-export wrappers (matches the layout of modal.tsx / navbar.tsx). Python: - src/python-fastui/fastui/components/forms.py: FormFieldRadio / FormFieldToggle Pydantic models with `type` discriminator. - src/python-fastui/fastui/forms.py: Radio() / Toggle() helper functions that mirror the existing Textarea() helper. - src/python-fastui/fastui/json_schema.py: dispatch on format='radio' (string + enum) and format='toggle' (boolean). Also preserve outer `format` keys when dereferencing $ref so per-field overrides survive. Tests: - src/python-fastui/tests/test_forms.py: 5 new tests covering radio field schema output, radio submit, invalid value, toggle schema output, and the multi-radio fallback to select.
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Closes #160. Adds two requested form fields:
Radio(radio button group) andToggle(on/off switch).What's added
FormFieldRadio— radio button group with optionalinlineflagFormFieldToggle— dedicated on/off switch with optionalon_label/off_labelFiles changed (13)
TypeScript:
src/npm-fastui/src/components/FormField.tsx,components/index.tsx,models.d.tssrc/npm-fastui-bootstrap/src/index.tsxsrc/npm-fastui-bootstrap/src/Radio.tsx(new)src/npm-fastui-bootstrap/src/Toggle.tsx(new)Python:
fastui/components/forms.py,components/__init__.py,forms.py—Radio()/Toggle()helpersjson_schema.py— radio/toggle dispatch +$refsibling-key preservationTests:
tests/test_forms.py— 5 new testsTest plan
npm run typecheckclean,npm run lintclean (0 errors), prettier cleanruff format --checkpasses,ruff checkclean on diff (1 preexisting error in unchanged code)