2017-06-14 80 views
3

我的代碼如何關閉菜單onNewOptionClick?

<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 
    }) 
} 

之前,我點擊新的選擇:

enter image description here

我按新的選項後:

enter image description here

期望中的UI狀態點擊新選項後:

enter image description here

沒有是否將此問題作爲Github發佈,因爲我不確定使用onNewOptionClick的確切方式。

回答

3

我能夠通過添加ref

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

,然後調用它,以便

this.productSelect.select.closeMenu(); 

這(https://github.com/JedWatson/react-select/issues/1262)來解決這個提供的線索,最終這讓我解決這個問題。謝謝。

0

不知道是否已經出現了重大更改以來第一個答案庫中,但我不得不這樣做:

ref={input => { 
    if (input) this.productSelectMenu = input._selectRef.select 
}} 

然後:

this.productSelectMenu.closeMenu();