Skip to content

fix: prevent form submission when required Select has more than 300 options and no selection #8280

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
38 changes: 31 additions & 7 deletions packages/@react-aria/select/src/HiddenSelect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
*/

import {FocusableElement, RefObject} from '@react-types/shared';
import React, {JSX, ReactNode, useCallback, useRef} from 'react';
import React, {InputHTMLAttributes, JSX, ReactNode, useCallback, useRef} from 'react';
import {selectData} from './useSelect';
import {SelectState} from '@react-stately/select';
import {useFormReset} from '@react-aria/utils';
Expand Down Expand Up @@ -145,13 +145,37 @@ export function HiddenSelect<T>(props: HiddenSelectProps<T>): JSX.Element | null
</div>
);
} else if (name) {
let data = selectData.get(state) || {};
let {validationBehavior} = data;

let inputProps: InputHTMLAttributes<HTMLInputElement> = {
type: 'hidden',
autoComplete: selectProps.autoComplete,
name,
disabled: isDisabled,
value: state.selectedKey ?? ''
};

if (validationBehavior === 'native') {
// Use a hidden <input type="text"> rather than <input type="hidden">
// so that an empty value blocks HTML form submission when the field is required.
return (
<input
{...inputProps}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Noticed in the story that keyboard tab navigation is broken, we'll want to put a tabIndex={-1} on this element I think so it can't be tabbed to

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ddb1e00 Thanks

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this would mean it's still in the accessibility tree though. shouldn't it be hidden (i.e. display: none)? That would avoid the need for containerProps as well since it wouldn't be focusable or visible in the first place.

Copy link
Contributor Author

@lucasweng lucasweng Jun 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2c4b1dc Thanks! aria-hidden: true in containerProps also removes the input from the accessibility tree, but keeping it simple is better.

Edited: It works as expected: prevents submission without breaking keyboard navigation.

test.mov

style={{display: 'none'}}
type="text"
required={selectProps.required}
onChange={() => {/** Ignore react warning. */}}
onInvalid={(e) => {
// Prevent native browser error popup from appearing.
e.preventDefault();
triggerRef.current?.focus();
}} />
);
}

return (
<input
type="hidden"
autoComplete={selectProps.autoComplete}
name={name}
disabled={isDisabled}
value={state.selectedKey ?? ''} />
<input {...inputProps} />
);
}

Expand Down
36 changes: 34 additions & 2 deletions packages/react-aria-components/stories/Select.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,13 @@ import {UNSTABLE_ListBoxLoadingSentinel} from '../src/ListBox';
import {useAsyncList} from 'react-stately';

export default {
title: 'React Aria Components'
title: 'React Aria Components',
argTypes: {
validationBehavior: {
control: 'select',
options: ['native', 'aria']
}
}
};

export const SelectExample = () => (
Expand Down Expand Up @@ -64,7 +70,11 @@ export const SelectRenderProps = () => (
</Select>
);

let manyItems = [...Array(100)].map((_, i) => ({id: i, name: `Item ${i}`}));
let makeItems = (length: number) => Array.from({length}, (_, i) => ({
id: i,
name: `Item ${i}`
}));
let manyItems = makeItems(100);

export const SelectManyItems = () => (
<Select>
Expand Down Expand Up @@ -212,3 +222,25 @@ export const SelectSubmitExample = () => (
<Button type="reset">Reset</Button>
</Form>
);

// Test case for https://github.com/adobe/react-spectrum/issues/8034
// Required select validation cannot currently be tested in the jsdom environment.
// In jsdom, forms are submitted even when required fields are empty.
// See: https://github.com/jsdom/jsdom/issues/2898
export const RequiredSelectWithManyItems = (props) => (
<form>
<Select {...props} name="select" isRequired>
<Label style={{display: 'block'}}>Required Select with many items</Label>
<Button>
<SelectValue />
<span aria-hidden="true" style={{paddingLeft: 5}}>▼</span>
</Button>
<Popover>
<ListBox items={makeItems(301)} className={styles.menu}>
{item => <MyListBoxItem>{item.name}</MyListBoxItem>}
</ListBox>
</Popover>
</Select>
<Button type="submit">Submit</Button>
</form>
);