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.

Problem with multiple select when onChange event we update a state

See original GitHub issue

Description

I have a problem with multiple select when onChange event we update a state. handleFilterChange is called but the dropdown is automatically closing. that I don’t want.

Steps to Reproduce

const [filterType, setFilterType] = useState<string[]>([]);

const handleFilterChange = (e: React.ChangeEvent<HTMLSelectElement>) => {
        let arr = [];
        for (let i = 0; i < e.target.selectedOptions.length; i++) {
            arr.push(e.target.selectedOptions[i].value);
        }
        setFilterType(arr);
    }
<Select
                                    label="filters"
                                    onChange={e => handleFilterChange(e)}
                                    id="filter"
                                    icon={<Icon>filters</Icon>}
                                    s={12}
                                    m={6}
                                    multiple
                                    options={{
                                        classes: 'browser-default',
                                        dropdownOptions: {
                                            alignment: 'left',
                                            autoTrigger: true,
                                            closeOnClick: false,
                                            constrainWidth: true,
                                            coverTrigger: true,
                                            hover: false,
                                            inDuration: 150,
                                            outDuration: 250
                                        }
                                    }}
                                    value={}
                                >
                                    <option disabled value="">Choose your option</option>
                                    <option value="1">Choose your option</option>
                                    <option value="2">Choose your option</option>
                                </Select>

Expected behavior: [What you expect to happen]

the dropdown should not close.

Actual behavior: [What actually happens]

Versions

“react-materialize”: “^3.9.3”, “react”: “^16.10.2”, “materialize-css”: “^1.0.0-rc.2”,

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
alextrasterocommented, Mar 30, 2021

As you said this is an issue with materializecss. When calling set state, the component is rerendered and the select is created again, therefor returning to its original closed state. If Select had a open prop that we could then pass to materializejs this could be solved… but it doesn’t.

A workaround could be to only set state when the select is closed… maybe

const [filterType, setFilterType] = useState();

  const handleFilterClosed = (e) => {
    setFilterType(e.value.split(","));
  };

  return (
    <Select
      label="filters"
      // onChange={(e) => handleFilterChange(e)}
      id="filter"
      s={12}
      m={6}
      multiple
      options={{
        classes: "browser-default",
        dropdownOptions: {
          alignment: "left",
          autoTrigger: true,
          closeOnClick: false,
          constrainWidth: true,
          coverTrigger: true,
          hover: false,
          inDuration: 150,
          outDuration: 250,
          onCloseEnd: handleFilterClosed, // <- handle here
        }
      }}>
      <option disabled value="">Choose your option</option>
      <option value="1">Choose your option</option>
      <option value="2">Choose your option</option>
    </Select>
  );
1reaction
Tchekdacommented, Mar 29, 2021

I am having this problem too! Was anyone able to find a workaround? @cstavroudis

I had to switch for something else until the issue is fixed… multiselect-react-dropdown does the job pretty well and has a better integration in React than react-materialize.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Select value doesnt change the first time I trigger onChange ...
The problem is that when you provide onChange prop to select component it become a controlled component. For more information: React Docs ...
Read more >
SCR19: Using an onchange event on a select element ... - W3C
The objective of this technique is to demonstrate how to correctly use an onchange event with a select element to update other elements...
Read more >
Integrating with Other Libraries - React
We subscribe to the change event (and unsubscribe on unmounting), and when it happens, we update the state with the model's current attributes....
Read more >
How can I handle onchange event for multiple select elements ...
Coding example for the question How can I handle onchange event for multiple select elements from map items-Reactjs.
Read more >
How to change a select's options based on another dropdown ...
Select in HTML allows us to choose among multiple values using the dropdown. Here, we are talking about changing select options based on...
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