Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

react-select : show only the matched options on typing on the select input

I have started using react-select very recently. (https://github.com/JedWatson/react-select)

If the select input is blank and 1. I focus on the select input OR, 2. I click on the inverted arrow situated at the right side of the select input, I get to see all the options in the result list that I have supplied to the Select component by default.

Instead of the above mentioned behavior, I should only be able to see the matched text results when I type something on the input. How can I achieve that?

like image 301
Wenger Avatar asked Oct 22 '25 19:10

Wenger


1 Answers

Control the rendering of the menu with menuIsOpen prop and add custom event handlers for onInputChange, onChange and onBlur: onInputChange you just check if the action performed is input-change and then set openMenu (in the component state) to true; onChange and onBlur events you just hide the menu.

<Select
    inputValue={value}
    onInputChange={this.handleInputChange}
    onChange={this.hideMenu}
    onBlur={this.hideMenu}
    options={dataSource}
    menuIsOpen={openMenu}
/>

    handleInputChange = (query, { action }) => {
        if (action === "input-change") {
            this.setState({ openMenu: true });
        }
    };

hideMenu = () => {
    this.setState({ openMenu: false });
};

Source : https://github.com/JedWatson/react-select/issues/2014#issuecomment-448549849

like image 140
rajesh kumar Avatar answered Oct 24 '25 08:10

rajesh kumar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!