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

[Select] Should not crash when an empty array is passed with multiple enabled #29957

merged 8 commits into from
Dec 6, 2021

Conversation

Domino987
Copy link
Contributor

@Domino987 Domino987 commented Nov 29, 2021

Fixes #29836

📦 https://codesandbox.io/s/mui-issue-latest-forked-4pg71

For the case that an empty list is passed for multiple, a fallback needs to be passed to the reduce.
Otherwise the first element is selected, which is undefined in this case ( empty list ) and it crashes the code.

References

For the case that an empty list is passed for multiple, a fallback needs to be passed to the reduce.
Otherwise the first element is selected, which is undefined in this case ( empty list ) and it crashes the code.
@mui-pr-bot
Copy link

mui-pr-bot commented Nov 29, 2021

Details of bundle changes

Generated by 🚫 dangerJS against d5d99df

@siriwatknp siriwatknp added the PR: needs test The pull request can't be merged label Nov 30, 2021
@siriwatknp
Copy link
Member

@Domino987 Can you refer to the issue number please, add #issue-number (eg. #29898) to the description of the PR

@Domino987
Copy link
Contributor Author

I just did. Thanks

Copy link
Member

@hbjORbj hbjORbj left a comment

Choose a reason for hiding this comment

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

Hi, thanks for opening the PR.

The following test fails due to the change of this PR.
Can you make sure it doesn't break any existing test? You can use this command to test: yarn test:unit --grep Select.

1) <Select />
       prop: multiple
         should serialize multiple select display value:

      AssertionError: expected div.MuiSelect-select.MuiSelect-outlined.MuiSelect-multiple.MuiOutlinedInput-input.MuiInputBase-input.emotion-client-render-11u53oe-MuiSelect-select-MuiInputBase-input-MuiOutlinedInput-input[tabindex="0"][role="button"][aria-expanded="false"][aria-haspopup="listbox"] to have text 'Ten, Twenty, Thirty', but the text was ', Ten, Twenty, Thirty'
      + expected - actual

      -, Ten, Twenty, Thirty
      +Ten, Twenty, Thirty
      
      at Context.<anonymous> (packages/mui-material/src/Select/Select.test.js:819:43)
      at processImmediate (internal/timers.js:461:21)

Also, you should add a test to confirm that what this PR is trying to fix (which handles properly when an empty list is passed with multiple enabled) is indeed fixed.

@hbjORbj hbjORbj added bug 🐛 Something doesn't work component: select This is the name of the generic UI component, not the React module! labels Nov 30, 2021
@hbjORbj hbjORbj changed the title Provide fallback for reduce [Select] Should not crash when an empty array is passed with multiple enabled Nov 30, 2021
@@ -418,7 +418,7 @@ const SelectInput = React.forwardRef(function SelectInput(props, ref) {

if (computeDisplay) {
if (multiple) {
display = displayMultiple.reduce((prev, curr) => [prev, ', ', curr]);
display = displayMultiple.reduce((prev, curr) => [prev, ', ', curr], "");
Copy link

Choose a reason for hiding this comment

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

@Domino987

I don't know the style guide:

displayMultiple.reduce((prev, curr, idx) => idx === 0 ? [curr] : [prev, ', ', curr], '');

Copy link
Member

Choose a reason for hiding this comment

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

@mnovati I think this works :)

@Domino987 It seems like I can't push to your branch. I think I need your permission. remote: Permission to Domino987/material-ui.git denied to hbjORbj. Would you make this change and as I said above add a test?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorry, was afk for a few days due to work. I will add the test

@mnovati
Copy link

mnovati commented Dec 1, 2021

displayMultiple is an array of nodes and this will stringify them I think and might cause some weird side effects. I'm not familiar with the code base but just something I noticed

@Domino987
Copy link
Contributor Author

Yes, after a more deep dive, I am now short circuiting the aggregation and also added a test with a zero-width space that gets added for the empty value.
It should work now.

Copy link
Member

@siriwatknp siriwatknp left a comment

Choose a reason for hiding this comment

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

@oliviertassinari Can you have a quick look at the Sandbox (in the description) if this is the expected behavior? It looks okay to me.

Copy link
Member

@hbjORbj hbjORbj left a comment

Choose a reason for hiding this comment

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

@mnovati @Domino987 Thanks for your contribution! :)

@oliviertassinari As can be seen in here, we already render react elements in case of Select with multiple disabled. I think we should keep the consistency so this looks good to me.

@hbjORbj hbjORbj added package: material-ui Specific to @mui/material and removed PR: needs test The pull request can't be merged labels Dec 2, 2021
@mnajdova mnajdova merged commit 4c98d4a into mui:master Dec 6, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug 🐛 Something doesn't work component: select This is the name of the generic UI component, not the React module! package: material-ui Specific to @mui/material
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants