@@ -42,26 +42,26 @@ def _django_form(
4242 top_children : Sequence ,
4343 bottom_children : Sequence ,
4444):
45- # TODO: Test this with django-colorfield, django-ace, django-crispy-forms
4645 uuid_ref = hooks .use_ref (uuid4 ().hex .replace ("-" , "" ))
4746 top_children_count = hooks .use_ref (len (top_children ))
4847 bottom_children_count = hooks .use_ref (len (bottom_children ))
4948 submitted_data , set_submitted_data = hooks .use_state ({} or None )
5049 rendered_form , set_rendered_form = hooks .use_state (cast (Union [str , None ], None ))
5150 uuid = uuid_ref .current
5251
53- # Don't allow the count of top and bottom children to change
52+ # Check the provided arguments
5453 if len (top_children ) != top_children_count .current or len (bottom_children ) != bottom_children_count .current :
5554 msg = "Dynamically changing the number of top or bottom children is not allowed."
5655 raise ValueError (msg )
57-
58- # Ensure the provided form is a Django Form
5956 if not isinstance (form , (type (Form ), type (ModelForm ))):
6057 msg = (
6158 "The provided form must be an uninitialized Django Form. "
6259 "Do NOT initialize your form by calling it (ex. `MyForm()`)."
6360 )
6461 raise TypeError (msg )
62+ if "id" in extra_props :
63+ msg = "The `extra_props` argument cannot contain an `id` key."
64+ raise ValueError (msg )
6565
6666 # Try to initialize the form with the provided data
6767 initialized_form = form (data = submitted_data )
@@ -86,10 +86,6 @@ async def render_form():
8686 if new_form != rendered_form :
8787 set_rendered_form (new_form )
8888
89- def _on_change (_event ):
90- if on_change :
91- on_change (form_event )
92-
9389 def on_submit_callback (new_data : dict [str , Any ]):
9490 """Callback function provided directly to the client side listener. This is responsible for transmitting
9591 the submitted form data to the server for processing."""
@@ -99,15 +95,18 @@ def on_submit_callback(new_data: dict[str, Any]):
9995 if on_submit :
10096 on_submit (FormEvent (form = initialized_form , data = new_data , set_data = set_submitted_data ))
10197
102- # TODO: The `use_state`` hook really should be de-duplicating this by itself. Needs upstream fix.
10398 if submitted_data != new_data :
10499 set_submitted_data (new_data )
105100
106101 if not rendered_form :
107102 return None
108103
109104 return html .form (
110- {"id" : f"reactpy-{ uuid } " , "onSubmit" : event (lambda _ : None , prevent_default = True ), "onChange" : _on_change }
105+ {
106+ "id" : f"reactpy-{ uuid } " ,
107+ "onSubmit" : event (lambda _ : None , prevent_default = True ),
108+ "onChange" : on_change (form_event ) if on_change else lambda _ : None ,
109+ }
111110 | extra_props ,
112111 DjangoForm ({"onSubmitCallback" : on_submit_callback , "formId" : f"reactpy-{ uuid } " }),
113112 * top_children ,
0 commit comments