Skip to content
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

[Select] Set aria-required & aria-invalid on combobox instead of hidden input #44731

Merged
merged 5 commits into from
Dec 16, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
30 changes: 30 additions & 0 deletions packages/mui-material/src/Select/Select.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,36 @@ describe('<Select />', () => {
expect(getByRole('combobox')).not.to.have.attribute('aria-disabled');
});

it('sets aria-required="true" when component is required', () => {
const { getByRole } = render(<Select required value="" />);

expect(getByRole('combobox')).to.have.attribute('aria-required', 'true');
});

it('aria-required is not present if component is not required', () => {
const { getByRole } = render(<Select required={false} value="" />);

expect(getByRole('combobox')).not.to.have.attribute('aria-required');
});

it('sets required attribute in input when component is required', () => {
const { container } = render(<Select required value="" />);

expect(container.querySelector('input')).to.have.property('required', true);
});

it('sets aria-invalid="true" when component is in the error state', () => {
const { getByRole } = render(<Select error value="" />);

expect(getByRole('combobox')).to.have.attribute('aria-invalid', 'true');
});

it('aria-invalid is not present if component is not in an error state', () => {
const { getByRole } = render(<Select value="" />);

expect(getByRole('combobox')).not.to.have.attribute('aria-invalid');
});

it('indicates that activating the button displays a listbox', () => {
const { getByRole } = render(<Select value="" />);

Expand Down
8 changes: 8 additions & 0 deletions packages/mui-material/src/Select/SelectInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ const SelectInput = React.forwardRef(function SelectInput(props, ref) {
open: openProp,
readOnly,
renderValue,
required,
SelectDisplayProps = {},
tabIndex: tabIndexProp,
// catching `type` from Input which makes no sense for SelectInput
Expand Down Expand Up @@ -503,6 +504,8 @@ const SelectInput = React.forwardRef(function SelectInput(props, ref) {
aria-label={ariaLabel}
aria-labelledby={[labelId, buttonId].filter(Boolean).join(' ') || undefined}
aria-describedby={ariaDescribedby}
aria-required={required ? 'true' : undefined}
aria-invalid={error ? 'true' : undefined}
onKeyDown={handleKeyDown}
onMouseDown={disabled || readOnly ? null : handleMouseDown}
onBlur={handleBlur}
Expand Down Expand Up @@ -534,6 +537,7 @@ const SelectInput = React.forwardRef(function SelectInput(props, ref) {
disabled={disabled}
className={classes.nativeInput}
autoFocus={autoFocus}
required={required}
{...other}
ownerState={ownerState}
/>
Expand Down Expand Up @@ -700,6 +704,10 @@ SelectInput.propTypes = {
* @returns {ReactNode}
*/
renderValue: PropTypes.func,
/**
* If `true`, the component is required.
*/
required: PropTypes.bool,
/**
* Props applied to the clickable div element.
*/
Expand Down
Loading