Skip to content

Commit 89056dd

Browse files
committed
fix: prevent invalid form control error and native error popup for visually hidden required input
1 parent c21313a commit 89056dd

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed

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

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -153,13 +153,21 @@ export function HiddenSelect<T>(props: HiddenSelectProps<T>): JSX.Element | null
153153
};
154154

155155
if (validationBehavior === 'native') {
156-
// Use a hidden <input type="text"> rather than <input type="hidden">
156+
// Use a visually hidden <input type="text"> rather than <input type="hidden">
157157
// so that an empty value blocks HTML form submission when the field is required.
158-
inputProps.type = 'text';
159-
inputProps.hidden = true;
160-
inputProps.required = selectProps.required;
161-
// Ignore react warning.
162-
inputProps.onChange = () => {};
158+
return (
159+
<input
160+
{...containerProps}
161+
{...inputProps}
162+
type="text"
163+
required={selectProps.required}
164+
onChange={() => {/** Ignore react warning. */}}
165+
onInvalid={(e) => {
166+
// Prevent native browser error popup from appearing.
167+
e.preventDefault();
168+
triggerRef.current?.focus();
169+
}} />
170+
);
163171
}
164172

165173
return (

0 commit comments

Comments
 (0)