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] Should not crash when an empty array is passed with multiple enabled #29957

Merged
merged 8 commits into from
Dec 6, 2021
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
14 changes: 14 additions & 0 deletions packages/mui-material/src/Select/Select.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -819,6 +819,20 @@ describe('<Select />', () => {
expect(getByRole('button')).to.have.text('Ten, Twenty, Thirty');
});

it('should not throw an error for an empty multiple list', () => {
const { getByRole } = render(
<Select multiple value={[]}>
<MenuItem value={10}>Ten</MenuItem>
<MenuItem value={20}>
<strong>Twenty</strong>
</MenuItem>
<MenuItem value={30}>Thirty</MenuItem>
</Select>,
);
// A zero-width string is added for empty values
expect(getByRole('button')).to.have.text('\u200B');
});

it('selects value based on their stringified equality when theyre not objects', () => {
const { getAllByRole } = render(
<Select multiple open value={['10', '20']}>
Expand Down
6 changes: 5 additions & 1 deletion packages/mui-material/src/Select/SelectInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,11 @@ const SelectInput = React.forwardRef(function SelectInput(props, ref) {

if (computeDisplay) {
if (multiple) {
display = displayMultiple.reduce((prev, curr) => [prev, ', ', curr]);
if (value.length === 0) {
display = '';
} else {
display = displayMultiple.reduce((prev, curr) => [prev, ', ', curr]);
}
} else {
display = displaySingle;
}
Expand Down