Skip to content

Commit 30e8529

Browse files
committed
fix(HiddenSelect): initial form value
1 parent 291d095 commit 30e8529

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

packages/@react-aria/select/src/HiddenSelect.tsx

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,8 @@ export function HiddenSelect<T, M extends SelectionMode = 'single'>(props: Hidde
145145
let inputRef = useRef(null);
146146
let {containerProps, selectProps} = useHiddenSelect({...props, selectRef: state.collection.size <= 300 ? selectRef : inputRef}, state, triggerRef);
147147

148+
let values: (Key | null)[] = Array.isArray(state.value) ? state.value : [state.value];
149+
148150
// If used in a <form>, use a hidden input so the value can be submitted to a server.
149151
// If the collection isn't too big, use a hidden <select> element for this so that browser
150152
// autofill will work. Otherwise, use an <input type="hidden">.
@@ -167,6 +169,10 @@ export function HiddenSelect<T, M extends SelectionMode = 'single'>(props: Hidde
167169
);
168170
}
169171
})}
172+
{/* The collection may be empty during the initial render. */}
173+
{/* Rendering options for the current values ensures the select has a value immediately, */}
174+
{/* making FormData reads consistent. */}
175+
{state.collection.size === 0 && name && values.map((value, i) => <option key={i} value={value ?? ''} />)}
170176
</select>
171177
</label>
172178
</div>
@@ -176,11 +182,10 @@ export function HiddenSelect<T, M extends SelectionMode = 'single'>(props: Hidde
176182
let {validationBehavior} = data;
177183

178184
// Always render at least one hidden input to ensure required form submission.
179-
let values: (Key | null)[] = Array.isArray(state.value) ? state.value : [state.value];
180185
if (values.length === 0) {
181186
values = [null];
182187
}
183-
188+
184189
let res = values.map((value, i) => {
185190
let inputProps: InputHTMLAttributes<HTMLInputElement> = {
186191
type: 'hidden',

0 commit comments

Comments
 (0)