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.

using custom components when isMulti and !closeMenuOnSelect click outside do not close menu

See original GitHub issue

Summary

I’m using react-select version 3.1.0. When I use a custom ValueContainer and closeMenuOnSelect={false}, the menu doesn’t close when you click outside the menu.

2020-08-20 11 35 19

Details

When I first open the menu and click away, the menu closes. After I select items and click away, the menu doesn’t close. If I then unselect items, the menu still doesn’t close when I click away. I checked out issue 2810 from v2, but I am defining the custom component outside of the render and it doesn’t solve the problem.

Code

const ValueContainer = ({ children, ...props }) => {
  const { getValue, hasValue, isMulti, selectProps: { placeholder } } = props;
  const numberOfValues = getValue().length;
  if (!hasValue || !isMulti) {
    return (
      <components.ValueContainer {...props}>
        {children}
      </components.ValueContainer>
    );
  }
  return (
    <components.ValueContainer {...props}>
      {`${numberOfValues} ${placeholder}`}
    </components.ValueContainer>
  );
};

const Select = (props) => {
  return (
    <span>
      <ReactSelect
        {...props}
        components={{
          ValueContainer
        }}
        closeMenuOnSelect={true}
        isMulti={true}
      />
    </span>
  );
};

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:13

github_iconTop GitHub Comments

6reactions
chkalcommented, Sep 8, 2020

I experimented a bit with your sample. Just move your custom components out of the App component. This should fix this issue.

https://codesandbox.io/s/react-codesandboxer-example-forked-m05z9?file=/example.js

Not completely sure why this happens. But if you declare the custom components inside another component, you will get new components on each render, which is most likely a bad idea. 😃

5reactions
chkalcommented, Aug 28, 2020

@taylorstyers Thanks a lot for sharing your code.

In the meantime we also fixed the issue by adding {children[1]} as the last child of <components.ValueContainer ...>.

Thanks a lot! 🍻

Read more comments on GitHub >

github_iconTop Results From Across the Web

react-select wont close when clicking outside - Stack Overflow
The problem was with how I sendt in the custom components. You shouldn't pass the props there. I think that might ruin some...
Read more >
How to detect click outside in a React component
In this tutorial, we will display a dropdown and close the dropdown when the user clicks outside it.
Read more >
API - React Select
A flexible and beautiful Select Input control for ReactJS with multiselect, autocomplete and ajax support.
Read more >
Dismissing a React Dropdown Menu by Clicking ... - Medium
If it does, hide the dropdown menu, since we need to use ref to save the DOM node in React in order to...
Read more >
react-functional-select - npm
Property Type Default inputId string undefined selectId string undefined menuId string undefined
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