Select with prop multiple=true is buggy when value is Arr<Obj>
See original GitHub issueIf we pass value as Array of Objects to Select
component when multiple=true
, it tends to create duplicate entry for same MenuItem in the drop-down.
- This is not a v0.x issue.
- I have searched the issues of this repository and believe that this is not a duplicate.
Expected Behavior 🤔
Suppose if we pass initialise state as -
state= {
value: [{name: 'Item1'}]
}
And suppose I click on the same item on the drop-down, the item should be unselected and the onChange method should receive an empty array instead.
Current Behavior 😯
I click on the same item on the drop-down, and the onChange method should receive an array of length 2, with the same object repeated twice.
Steps to Reproduce 🕹
I think I found the cause of the bug. Link to the function causing bug https://github.com/mui-org/material-ui/blob/master/packages/material-ui/src/Select/SelectInput.js#L125
The handleItemClick function implementation is as follows -
const handleItemClick = child => event => {
if (!multiple) {
update(false, event);
}
if (onChange) {
let newValue;
if (multiple) {
newValue = Array.isArray(value) ? [...value] : [];
/*
Here, suppose newValue is [{name: 'Item1'}] and child.props.value is {name: 'Item1'}.
The itemIndex is computed as -1 and it results to insertion of duplicate element, rather than splicing it.
*/
const itemIndex = value.indexOf(child.props.value);
if (itemIndex === -1) {
newValue.push(child.props.value);
} else {
newValue.splice(itemIndex, 1);
}
} else {
newValue = child.props.value;
}
event.persist();
event.target = { value: newValue, name };
onChange(event, child);
}
};
Link:
- Sandbox link - https://codesandbox.io/s/create-react-app-5grs7
- Probable Issue in codebase - https://github.com/mui-org/material-ui/blob/master/packages/material-ui/src/Select/SelectInput.js#L125
Context 🔦
I was trying to use it for my Project
Your Environment 🌎
Tech | Version |
---|---|
Material-UI | v3.9.2 |
React | 16.5.2 |
Browser | Chrome Version 75.0.3770.142 (Official Build) (64-bit) |
Issue Analytics
- State:
- Created 4 years ago
- Comments:6 (4 by maintainers)
Top Results From Across the Web
Select with prop multiple=true is buggy when value is Arr<Obj>
I click on the same item on the drop-down, and the onChange method should receive an array of length 2, with the same...
Read more >react-select typescript not working properly when I substitute ...
Why is this happening and is there any good workaround? CustomSelect.tsx import Select, { components, OptionProps, ValueContainerProps, Props as ...
Read more >block-editor.js - WPSeek Mobile
We select the prior 488 // path whose position in the new string is the ... prop + which[i]; 1098 } 1099 value...
Read more >IRC logs - Fluid Powered TYPO3
12:30:17 <Lirrec> NamelessCoder: in flux/fluidpages the clear checkbox on field values seems to be broken for all settings in "Page Configuration" ...
Read more >https://git.troplo.com/Troplo/pleroma-fe/commit/b3...
2 : 0, + 'vue/require-prop-types': 0 } } diff --git a/.gitlab-ci.yml ... CSS Sourcemaps off by default because relative paths are "buggy" diff...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
This is related to https://github.com/mui-org/material-ui/issues/10845 and a lot of other issues. I’m tempted to close this as a duplicate. You can kind of fix this by using the reference of the object as the default value e.g. https://codesandbox.io/s/create-react-app-vgyz6
@akshatnitd I was just speaking about the code.