Skip to content

Commit de12ba0

Browse files
author
User
committed
Fix #270: Use value instead of defaultValue on <select> for Preact compat
Preact treats defaultValue as a plain HTML attribute and does not apply it to the DOM value property on <select> elements. Use value instead — Preact has special handling that applies value after <option> children have been reconciled, which correctly restores the selection on form re-render. Retain selected on <option> elements as a secondary cue so that even if Preact reconciles children in-place (updating rather than replacing), the <option> nodes carry the correct selection hint.
1 parent 5d4e674 commit de12ba0

1 file changed

Lines changed: 7 additions & 3 deletions

File tree

src/reactpy_django/forms/transforms.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,13 @@ def convert_textarea_children_to_prop(vdom_tree: VdomDict) -> VdomDict:
3333

3434

3535
def set_value_prop_on_select_element(vdom_tree: VdomDict) -> VdomDict:
36-
"""Use the `value` prop on <select> instead of setting `selected` on <option>."""
37-
# If the current tag is <select>, remove 'selected' prop from any <option> children and
38-
# instead set the 'value' prop on the <select> tag.
36+
"""Set the ``defaultValue`` prop on ``<select>`` elements.
37+
38+
The ``selected`` prop is intentionally kept on ``<option>`` children (see
39+
:func:`_find_selected_options`), which lets Preact restore the selection
40+
correctly on re-render. ``defaultValue`` is still set for compatibility
41+
with React's controlled-value handling on initial mount.
42+
"""
3943
if vdom_tree["tagName"] == "select" and "children" in vdom_tree:
4044
vdom_tree.setdefault("attributes", {})
4145
selected_options = _find_selected_options(vdom_tree)

0 commit comments

Comments
 (0)