Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 7371a45

Browse files
committedDec 7, 2024
better input transform
1 parent 9360e51 commit 7371a45

File tree

2 files changed

+12
-6
lines changed

2 files changed

+12
-6
lines changed
 

‎src/reactpy_django/forms/components.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@
1313
from reactpy_django.forms.transforms import (
1414
convert_html_props_to_reactjs,
1515
convert_textarea_children_to_prop,
16-
ensure_input_elements_are_controlled,
1716
intercept_anchor_links,
1817
set_value_prop_on_select_element,
18+
transform_value_prop_on_input_element,
1919
)
2020
from reactpy_django.forms.utils import convert_boolean_fields, convert_multiple_choice_fields
2121
from reactpy_django.types import FormEvent
@@ -122,7 +122,7 @@ def on_submit_callback(new_data: dict[str, Any]):
122122
convert_html_props_to_reactjs,
123123
convert_textarea_children_to_prop,
124124
set_value_prop_on_select_element,
125-
ensure_input_elements_are_controlled,
125+
transform_value_prop_on_input_element,
126126
intercept_anchor_links,
127127
*extra_transforms,
128128
strict=False,

‎src/reactpy_django/forms/transforms.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,17 @@ def set_value_prop_on_select_element(vdom_tree: VdomDict) -> VdomDict:
4646
return vdom_tree
4747

4848

49-
def ensure_input_elements_are_controlled(vdom_tree: VdomDict) -> VdomDict:
49+
def transform_value_prop_on_input_element(vdom_tree: VdomDict) -> VdomDict:
5050
"""Adds an onChange handler on form <input> elements, since ReactJS doesn't like uncontrolled inputs."""
51-
vdom_tree.setdefault("eventHandlers", {})
52-
if vdom_tree["tagName"] == "input" and "onChange" not in vdom_tree["eventHandlers"]:
53-
vdom_tree["eventHandlers"]["onChange"] = EventHandler(to_event_handler_function(_do_nothing_event))
51+
# vdom_tree.setdefault("eventHandlers", {})
52+
# vdom_tree.setdefault("attributes", {})
53+
# if vdom_tree["tagName"] == "input" and "onChange" not in vdom_tree["eventHandlers"]:
54+
# vdom_tree["eventHandlers"]["onChange"] = EventHandler(to_event_handler_function(_do_nothing_event))
55+
# vdom_tree["attributes"].setdefault("value", "")
56+
57+
vdom_tree.setdefault("attributes", {})
58+
if vdom_tree["tagName"] == "input":
59+
vdom_tree["attributes"].setdefault("defaultValue", vdom_tree["attributes"].pop("value", ""))
5460

5561
return vdom_tree
5662

0 commit comments

Comments
 (0)
Please sign in to comment.