Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add support for step #227

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
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
3 changes: 2 additions & 1 deletion src/npm-fastui/src/components/FormField.tsx
Original file line number Diff line number Diff line change
@@ -24,7 +24,7 @@ interface FormFieldInputProps extends FormFieldInput {
}

export const FormFieldInputComp: FC<FormFieldInputProps> = (props) => {
const { name, placeholder, required, htmlType, locked, autocomplete, onChange } = props
const { name, placeholder, required, htmlType, locked, autocomplete, onChange, step } = props

return (
<div className={useClassName(props)}>
@@ -39,6 +39,7 @@ export const FormFieldInputComp: FC<FormFieldInputProps> = (props) => {
disabled={locked}
placeholder={placeholder}
autoComplete={autocomplete}
step={step}
aria-describedby={descId(props)}
onChange={onChange}
/>
1 change: 1 addition & 0 deletions src/npm-fastui/src/models.d.ts
Original file line number Diff line number Diff line change
@@ -433,6 +433,7 @@ export interface FormFieldInput {
initial?: string | number
placeholder?: string
autocomplete?: string
step?: number | 'any'
type: 'FormFieldInput'
}
/**
3 changes: 3 additions & 0 deletions src/python-fastui/fastui/components/forms.py
Original file line number Diff line number Diff line change
@@ -58,6 +58,9 @@ class FormFieldInput(BaseFormField):
autocomplete: _t.Union[str, None] = None
"""Autocomplete value for the field."""

step: _t.Union[float, _t.Literal['any'], None] = None
"""Step value for the field."""

type: _t.Literal['FormFieldInput'] = 'FormFieldInput'
"""The type of the component. Always 'FormFieldInput'."""

9 changes: 9 additions & 0 deletions src/python-fastui/fastui/json_schema.py
Original file line number Diff line number Diff line change
@@ -198,6 +198,7 @@ def json_schema_field_to_field(
autocomplete=schema.get('autocomplete'),
description=schema.get('description'),
placeholder=schema.get('placeholder'),
step=schema.get('step', _get_default_step(schema)),
)


@@ -373,6 +374,14 @@ def input_html_type(schema: JsonSchemaField) -> InputHtmlType:
raise ValueError(f'Unknown schema: {schema}') from e


def _get_default_step(schema: JsonSchemaField) -> _t.Union[_t.Literal['any'], None]:
key = schema['type']
if key == 'integer':
return None
if key == 'number':
return 'any'


def schema_is_field(schema: JsonSchemaConcrete) -> _ta.TypeGuard[JsonSchemaField]:
"""
Determine if a schema is a field `JsonSchemaField`
44 changes: 44 additions & 0 deletions src/python-fastui/tests/test_forms.py
Original file line number Diff line number Diff line change
@@ -547,3 +547,47 @@ def test_form_fields():
'submitUrl': '/foobar/',
'type': 'ModelForm',
}


class FormNumbersDefaultStep(BaseModel):
size: int
cost: float
fees: float = Field(json_schema_extra={'step': '0.01'})


def test_form_numbers_default_step():
m = components.ModelForm(model=FormNumbersDefaultStep, submit_url='/foobar')

assert m.model_dump(by_alias=True, exclude_none=True) == {
'submitUrl': '/foobar',
'method': 'POST',
'type': 'ModelForm',
'formFields': [
{
'name': 'size',
'title': ['Size'],
'required': True,
'locked': False,
'htmlType': 'number',
'type': 'FormFieldInput',
},
{
'name': 'cost',
'title': ['Cost'],
'required': True,
'locked': False,
'htmlType': 'number',
'step': 'any',
'type': 'FormFieldInput',
},
{
'name': 'fees',
'title': ['Fees'],
'required': True,
'locked': False,
'htmlType': 'number',
'step': 0.01,
'type': 'FormFieldInput',
},
],
}