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]Bug when the first child is a ListSubheader #27299

Merged
merged 13 commits into from
Apr 11, 2022
Merged
Changes from 2 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
18 changes: 16 additions & 2 deletions packages/material-ui/src/Select/SelectInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ const SelectInput = React.forwardRef(function SelectInput(props, ref) {
}
}

const items = childrenArray.map((child) => {
const items = childrenArray.map((child, index, arr) => {
if (!React.isValidElement(child)) {
return null;
}
Expand Down Expand Up @@ -369,6 +369,20 @@ const SelectInput = React.forwardRef(function SelectInput(props, ref) {
foundMatch = true;
}

const hasOptionSelected = () => {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

And here, when you select an option don't need more to set the second option as selected true to gain the focus.

if (arr.find((item) => areEqualValues(value, item.props.value))) {
return selected;
}
return true;
};

if (child.props.value === undefined) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This returns a different object when the option is a ListSubheader. The role option is because the role listbox of the Menu needs that the children have the role option, but when the option is not a selectable option the aria-selected should be omitted.

return React.cloneElement(child, {
'aria-readonly': true,
role: 'option',
});
}

return React.cloneElement(child, {
'aria-selected': selected ? 'true' : undefined,
onClick: handleItemClick(child),
Expand All @@ -385,7 +399,7 @@ const SelectInput = React.forwardRef(function SelectInput(props, ref) {
}
},
role: 'option',
selected,
selected: index === 1 && arr[0].props.value === undefined ? hasOptionSelected() : selected,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Here when the first child is a ListSubheader it sets the selected true to the second option, to gains the focus when the list of options is open, with the tabindex = 0, to the arrow down works.

value: undefined, // The value is most likely not a valid HTML attribute.
'data-value': child.props.value, // Instead, we provide it as a data attribute.
});
Expand Down