feat: add form component and use_form_data hook#58
Open
Archmonger wants to merge 2 commits into
Open
Conversation
Implements a `form` component akin to React Router's `<Form>` component. - JS-side Form component intercepts submit events, serializes FormData into JSON, navigates via pushState, and passes form data to the server. - Python-side form() function renders a <form> element with a corresponding JS listener. On submit, form data is stored in context and the router navigates to the action URL. - use_form_data() hook retrieves the most recently submitted form data. - Form data context (FormDataState) is wired into the router so it's available to all routed components. - Non-file fields only; file serialization deferred to future work. Closes #7
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Implements a
Formcomponent a la React Router's Form, addressing #7.Form submissions are intercepted client-side, serialized via
FormDatainto JSON, navigated viapushState(GET-method), and the submitted data is made available through theuse_form_data()hook. Non-file fields only; file serialization is deferred.Changes
JavaScript:
Formcomponent (src/js/src/components.ts) that interceptssubmitevents via class-based selectors (same pattern asLink), serializesFormDatatoRecord<string, string[]>, navigates with query params, and firesonSubmitCallbackto the server.FormProps/FormSubmitDatatypes tosrc/js/src/types.ts.Python:
form(attributes, *children)insrc/reactpy_router/components.py— mirrors thelink()pattern. Renders a<form>with a unique CSS class and the JSFormlistener as a sibling.use_form_data()hook insrc/reactpy_router/hooks.py— returns the most recently submitted form data (dict[str, list[str]]).FormDataStatecontext wired into the router inrouters.pyso form data is available to all routed components.formanduse_form_dataexported from__init__.py.Tests (6 new):
test_simple_form— basic form submission navigates to action URLtest_form_no_page_reload— verifies no full page reload on submittest_form_action_url_with_query_string— action URL with pre-existing query paramstest_form_use_form_data— submitted data retrievable viause_form_data()test_form_with_multiple_values— multiple checkbox values serialized correctlytest_form_class_name—classNameattribute pass-throughChecklist
Please update this checklist as you complete each item:
By submitting this pull request I agree that all contributions comply with this project's open source license(s).