question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Select with prop multiple=true is buggy when value is Arr<Obj>

See original GitHub issue

If 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:

  1. Sandbox link - https://codesandbox.io/s/create-react-app-5grs7
  2. 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:closed
  • Created 4 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
joshwoodingcommented, Jul 27, 2019

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

1reaction
joshwoodingcommented, Jul 27, 2019

@akshatnitd I was just speaking about the code.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found