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(FormField): accept className #163

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/python-fastui/fastui/json_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,18 +185,21 @@ def json_schema_field_to_field(
initial=schema.get('default'),
description=schema.get('description'),
mode=schema.get('mode', 'checkbox'),
class_name=schema.get('className'),
)
elif field := special_string_field(schema, name, title, required, False):
return field
else:
return FormFieldInput(
name=name,
title=title,
placeholder=schema.get('placeholder'),
html_type=input_html_type(schema),
required=required,
initial=schema.get('default'),
autocomplete=schema.get('autocomplete'),
description=schema.get('description'),
class_name=schema.get('className'),
)


Expand Down Expand Up @@ -246,6 +249,7 @@ def special_string_field(
multiple=multiple,
accept=schema.get('accept'),
description=schema.get('description'),
class_name=schema.get('className'),
)
elif schema.get('format') == 'textarea':
return FormFieldTextarea(
Expand All @@ -270,6 +274,7 @@ def special_string_field(
options=[SelectOption(value=v, label=enum_labels.get(v) or as_title(v)) for v in enum],
initial=schema.get('default'),
description=schema.get('description'),
class_name=schema.get('className'),
autocomplete=schema.get('autocomplete'),
)
elif search_url := schema.get('search_url'):
Expand All @@ -282,6 +287,7 @@ def special_string_field(
multiple=multiple,
initial=schema.get('initial'),
description=schema.get('description'),
class_name=schema.get('className'),
)


Expand Down
98 changes: 97 additions & 1 deletion src/python-fastui/tests/test_forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from fastapi import HTTPException
from fastui import components
from fastui.forms import FormFile, Textarea, fastui_form
from pydantic import BaseModel
from pydantic import BaseModel, Field
from starlette.datastructures import FormData, Headers, UploadFile
from typing_extensions import Annotated

Expand Down Expand Up @@ -469,3 +469,99 @@ def test_form_textarea_form_fields():
}
],
}


class FormWithCustomClassNameField(BaseModel):
name: str
size: int = 4
internal_id: int = Field(
title='Internal id',
json_schema_extra={
'className': 'visually-hidden',
},
)


def test_fields_with_custom_class_name():
m = components.ModelForm(model=FormWithCustomClassNameField, submit_url='/foobar/')

assert m.model_dump(by_alias=True, exclude_none=True) == {
'submitUrl': '/foobar/',
'method': 'POST',
'type': 'ModelForm',
'formFields': [
{
'name': 'name',
'title': ['Name'],
'required': True,
'locked': False,
'htmlType': 'text',
'type': 'FormFieldInput',
},
{
'name': 'size',
'title': ['Size'],
'initial': 4,
'required': False,
'locked': False,
'htmlType': 'number',
'type': 'FormFieldInput',
},
{
'className': 'visually-hidden',
'htmlType': 'number',
'locked': False,
'name': 'internal_id',
'required': True,
'title': ['Internal id'],
'type': 'FormFieldInput',
},
],
}


class FormWithPlaceholderField(BaseModel):
name: str
size: int = 4
internal_id: int = Field(
title='Internal id',
json_schema_extra={'placeholder': '1234'},
)


def test_fields_with_placeholder_values():
m = components.ModelForm(model=FormWithPlaceholderField, submit_url='/foobar/')

assert m.model_dump(by_alias=True, exclude_none=True) == {
'submitUrl': '/foobar/',
'method': 'POST',
'type': 'ModelForm',
'formFields': [
{
'name': 'name',
'title': ['Name'],
'required': True,
'locked': False,
'htmlType': 'text',
'type': 'FormFieldInput',
},
{
'name': 'size',
'title': ['Size'],
'initial': 4,
'required': False,
'locked': False,
'htmlType': 'number',
'type': 'FormFieldInput',
},
{
'htmlType': 'number',
'locked': False,
'name': 'internal_id',
'placeholder': '1234',
'required': True,
'title': ['Internal id'],
'type': 'FormFieldInput',
},
],
}