简体   繁体   中英

React select v2 on space press selects first value

When on Select field I hit space, first value from options is being selected. How to disable such behaviour?

<Select
  ref={r => (this.selectRef = r)}
  className="basic-single"
  classNamePrefix="select"
  onInputChange={val => {
    console.log('va', val)
    this.setState({ inputValue: val })
    }}
  inputValue={this.state.inputValue}
  options={[{ value: 'aaa', label: 'aaa bbb' }, { value: 'bbb', label: 'bbb ccc' }]}
  name="color"
/>

HERE IS A DEMO

I would suggest to use the onKeyDown props and prevent the action when the use hits the space bar so nothing will be selected.

<Select
   ref={r => (this.selectRef = r)}
   className="basic-single"
   classNamePrefix="select"
   onInputChange={val => {
     this.setState({ inputValue: val });
   }}
   onKeyDown={e => {
     if (e.keyCode === 32 && !this.selectRef.state.inputValue) e.preventDefault();
   }}
   inputValue={this.state.inputValue}
   options={[
     { value: "aaa", label: "aaa bbb" },
     { value: "bbb", label: "bbb ccc" }
   ]}
   name="color"
 />

Live example here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM