using custom components when isMulti and !closeMenuOnSelect click outside do not close menu
See original GitHub issueSummary
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.
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:
- Created 3 years ago
- Comments:13
Top 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 >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
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. 😃
@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! 🍻