Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to close menu onNewOptionClick?

Tags:

react-select

My Code

<Creatable
   name="productType"=
   options = {this.state.productOptions}
   value = {this.state.productType}
   onNewOptionClick = {this.createProductType}
   onChange = {this.handleProductChange}  
 />

createProductType(option) {
    var options = this.state.productOptions;
    var label = option.label.charAt(0).toUpperCase() + option.label.slice(1);
    options.push({
        label: label,
        value: option.value
    })
    this.setState({
        productOptions: options,
        productType: option.value
    })
}

Before I click new option:

enter image description here

After I click new option:

enter image description here

Desired UI state after clicking new option:

enter image description here

Did not whether to post this as issues on Github as I am not sure of the exact way of using onNewOptionClick.

like image 990
Sai Krishna Avatar asked Jun 14 '17 15:06

Sai Krishna


2 Answers

I was able to solve this by adding a ref

ref={input => this.productSelect = input }

and then calling it so

this.productSelect.select.closeMenu();

This (https://github.com/JedWatson/react-select/issues/1262) provided the final clue which helped me solve this. Thanks.

like image 52
Sai Krishna Avatar answered Oct 19 '22 06:10

Sai Krishna


closeMenu() has been depreciated in v2 of React-Select. It has been replaced by blur() The following worked for me:

// Assign the ref in your Select object
<Select ref={input => this.selectRef = input } ... />

// Later in your code when you are trying to close the menu
this.selectRef.select.blur();
like image 30
eqwert Avatar answered Oct 19 '22 06:10

eqwert