Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rather than copying logic, can we combine this with the logic below, maybe worth moving out into a separate function?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can probably use the {[string]: bool} variant of ClassName to make most of these cases more declarative.

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