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

Merged
merged 11 commits into from
Jul 9, 2025
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 32 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, useRef} from 'react';
import React, {InputHTMLAttributes, JSX, ReactNode, useRef} from 'react';
import {selectData} from './useSelect';
import {SelectState} from '@react-stately/select';
import {useFormReset} from '@react-aria/utils';
Expand Down Expand Up @@ -141,13 +141,38 @@ 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 visually 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
{...containerProps}
{...inputProps}
type="text"
tabIndex={-1}
required={selectProps.required}
onChange={() => {/** Ignore react warning. */}}
onInvalid={(e) => {
// Prevent native browser error popup from appearing.
e.preventDefault();
triggerRef.current?.focus();
Copy link
Member

Choose a reason for hiding this comment

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

I think useFormValidation should handle this. The trigger shouldn't necessarily be focused on invalid - the first invalid field in the form should be focused, which might not be the select.

We may need to attach the selectRef to the input for this to work.

}} />
);
}

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 @@ -174,3 +184,25 @@ AsyncVirtualizedCollectionRenderSelect.story = {
delay: 50
}
};

// 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>
);