Repository navigation
Commit 7cbd46b
User
Fix #270: preserve multi-select form values across submission
The `django_form` component lost multi-select (MultipleChoiceField) values
after a successful submission because two separate issues prevented the
re-rendered form from restoring the selection on the client:
1. The `onSubmit` ReactPy event handler on the `<form>` element was
redundant with the client-side `DjangoForm` submit listener (which already
calls `preventDefault()` and forwards the submitted data). When a form was
submitted a second time (after re-rendering validation errors), this extra
handler caused the WebSocket to close and remount the component, dropping
the submission before it reached the server. Removing the redundant
`onSubmit` prop lets the client-side listener reliably deliver the data.
2. ReactPy's built-in `select_element_to_reactjs` transform strips the
`selected` attribute from `<option>` elements and stores it as
`defaultValue` on the parent `<select>`. Preact does not apply a `<select>`'s
`defaultValue` prop to its `<option>` children, so the selection was lost on
re-render. `set_value_prop_on_select_element` now also re-applies `selected`
on the matching `<option>` elements, which Preact applies as the option's DOM
`selected` property.
Also fixes the multi-select assertions in `test_form_basic` to read the
selected `<option>` values, since `input_value()` returns only the first
selection on a multi-select.1 parent 86ca94f commit 7cbd46b
3 files changed
Lines changed: 46 additions & 35 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
8 | 8 | | |
9 | 9 | | |
10 | 10 | | |
11 | | - | |
12 | 11 | | |
13 | 12 | | |
14 | 13 | | |
| |||
126 | 125 | | |
127 | 126 | | |
128 | 127 | | |
129 | | - | |
| 128 | + | |
130 | 129 | | |
131 | 130 | | |
132 | | - | |
133 | | - | |
134 | 131 | | |
135 | 132 | | |
136 | 133 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
33 | 33 | | |
34 | 34 | | |
35 | 35 | | |
36 | | - | |
37 | | - | |
38 | | - | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
39 | 49 | | |
40 | 50 | | |
41 | | - | |
42 | | - | |
43 | | - | |
44 | | - | |
45 | | - | |
46 | | - | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
47 | 62 | | |
48 | 63 | | |
49 | 64 | | |
| |||
87 | 102 | | |
88 | 103 | | |
89 | 104 | | |
90 | | - | |
91 | | - | |
92 | | - | |
| 105 | + | |
| 106 | + | |
93 | 107 | | |
94 | | - | |
95 | | - | |
96 | | - | |
97 | | - | |
98 | | - | |
99 | | - | |
100 | | - | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
101 | 114 | | |
102 | | - | |
| 115 | + | |
103 | 116 | | |
104 | | - | |
105 | 117 | | |
106 | | - | |
107 | | - | |
108 | | - | |
109 | | - | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
110 | 121 | | |
111 | 122 | | |
112 | | - | |
113 | | - | |
114 | | - | |
| 123 | + | |
115 | 124 | | |
116 | 125 | | |
117 | 126 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
812 | 812 | | |
813 | 813 | | |
814 | 814 | | |
815 | | - | |
816 | | - | |
| 815 | + | |
| 816 | + | |
| 817 | + | |
| 818 | + | |
| 819 | + | |
| 820 | + | |
| 821 | + | |
817 | 822 | | |
818 | 823 | | |
819 | | - | |
| 824 | + | |
820 | 825 | | |
821 | 826 | | |
822 | 827 | | |
| |||
0 commit comments