Skip to content

Commit

Permalink
Add toggle switches support
Browse files Browse the repository at this point in the history
  • Loading branch information
ischaojie committed Dec 2, 2023
1 parent 78d7f5e commit 64f72db
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 2 deletions.
14 changes: 13 additions & 1 deletion packages/fastui-bootstrap/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,20 @@ export const classNameGenerator: ClassNameGenerator = ({ props, fullPath, subEle
return 'col-md-4'
}
}
case 'FormFieldInput':
case 'FormFieldCheckbox':
switch (subElement) {
case 'input':
return props.error ? 'is-invalid form-check-input' : 'form-check-input'
case 'label':
return 'form-check-label'
case 'error':
return 'invalid-feedback'
case 'description':
return 'form-text'
default:
return props.mode === 'checkbox' ? 'form-check' : 'form-check form-switch'
}
case 'FormFieldInput':
case 'FormFieldSelect':
case 'FormFieldSelectSearch':
case 'FormFieldFile':
Expand Down
1 change: 1 addition & 0 deletions packages/fastui/src/components/FormField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ export const FormFieldInputComp: FC<FormFieldInputProps> = (props) => {

interface FormFieldCheckboxProps extends BaseFormFieldProps {
type: 'FormFieldCheckbox'
mode: 'checkbox'
initial?: boolean
}

Expand Down
2 changes: 1 addition & 1 deletion python/demo/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,8 @@ class BigModel(BaseModel):
profile_pics: Annotated[list[UploadFile], FormFile(accept='image/*')] | None = Field(
None, description='Upload multiple images'
)

dob: date = Field(title='Date of Birth', description='Your date of birth, this is required hence bold')
human: bool = Field(title='Is human', description='Are you human?', json_schema_extra={'mode': 'switch'})
size: SizeModel

@field_validator('name')
Expand Down
1 change: 1 addition & 0 deletions python/fastui/components/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class FormFieldInput(BaseFormField):
class FormFieldCheckbox(BaseFormField):
initial: bool | None = None
type: typing.Literal['FormFieldCheckbox'] = 'FormFieldCheckbox'
mode: typing.Literal['checkbox', 'switch'] = 'checkbox'


class FormFieldFile(BaseFormField):
Expand Down
2 changes: 2 additions & 0 deletions python/fastui/json_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ class JsonSchemaFile(JsonSchemaBase, total=False):
class JsonSchemaBool(JsonSchemaBase, total=False):
type: Required[Literal['boolean']]
default: bool
mode: Literal['checkbox', 'switch']


class JsonSchemaInt(JsonSchemaBase, total=False):
Expand Down Expand Up @@ -175,6 +176,7 @@ def json_schema_field_to_field(
required=required,
initial=schema.get('default'),
description=schema.get('description'),
mode=schema.get('mode', 'checkbox'),
)
elif field := special_string_field(schema, name, title, required, False):
return field
Expand Down

0 comments on commit 64f72db

Please sign in to comment.