From 57815a7791ba56cd4a28ff8fb4e7870a64976ded Mon Sep 17 00:00:00 2001 From: sergue1 Date: Thu, 30 May 2024 15:42:39 +0200 Subject: [PATCH] Fix placeholder on text inputs (#304) --- demo/forms.py | 1 + src/python-fastui/fastui/json_schema.py | 1 + src/python-fastui/tests/test_forms.py | 25 +++++++++++++++++++++++++ 3 files changed, 27 insertions(+) diff --git a/demo/forms.py b/demo/forms.py index 886ae77e..a622ca9a 100644 --- a/demo/forms.py +++ b/demo/forms.py @@ -144,6 +144,7 @@ class BigModel(BaseModel): None, description='This field is not required, it must start with a capital letter if provided' ) info: Annotated[str | None, Textarea(rows=5)] = Field(None, description='Optional free text information about you.') + repo: str = Field(json_schema_extra={'placeholder': '{org}/{repo}'}, title='GitHub repository') profile_pic: Annotated[UploadFile, FormFile(accept='image/*', max_size=16_000)] = Field( description='Upload a profile picture, must not be more than 16kb' ) diff --git a/src/python-fastui/fastui/json_schema.py b/src/python-fastui/fastui/json_schema.py index 53ee526d..c822f180 100644 --- a/src/python-fastui/fastui/json_schema.py +++ b/src/python-fastui/fastui/json_schema.py @@ -197,6 +197,7 @@ def json_schema_field_to_field( initial=schema.get('default'), autocomplete=schema.get('autocomplete'), description=schema.get('description'), + placeholder=schema.get('placeholder'), ) diff --git a/src/python-fastui/tests/test_forms.py b/src/python-fastui/tests/test_forms.py index d73e3c10..ceaa7a5d 100644 --- a/src/python-fastui/tests/test_forms.py +++ b/src/python-fastui/tests/test_forms.py @@ -522,3 +522,28 @@ def test_form_description_leakage(): 'submitUrl': '/foobar/', 'type': 'ModelForm', } + + +class RichForm(BaseModel): + repo: str = Field(json_schema_extra={'placeholder': '{org}/{repo}'}, title='GitHub repository') + + +def test_form_fields(): + m = components.ModelForm(model=RichForm, submit_url='/foobar/') + + assert m.model_dump(by_alias=True, exclude_none=True) == { + 'formFields': [ + { + 'htmlType': 'text', + 'locked': False, + 'name': 'repo', + 'placeholder': '{org}/{repo}', + 'required': True, + 'title': ['GitHub repository'], + 'type': 'FormFieldInput', + } + ], + 'method': 'POST', + 'submitUrl': '/foobar/', + 'type': 'ModelForm', + }