简体   繁体   English

在 React-Select 中禁用关注第一个选项

[英]Disable Focus on First option in React-Select

I have a dropdown in React Select, i am facing an issue with the focus.我在 React Select 中有一个下拉菜单,我遇到了焦点问题。 The first item in the option list is getting focused whenever we open the dropdown.每当我们打开下拉菜单时,选项列表中的第一项就会成为焦点。 There are couple of props that gets passed, but none of them is used to disable this feature.有几个道具通过了,但没有一个用于禁用此功能。 I saw issues related to autofocus mentioned in their github.我在他们的 github 中看到了与自动对焦相关的问题。 But the prop is still not added in their package.但是他们的package中仍然没有添加道具。 I tried with focus props, but i am not getting the desired result.我尝试使用焦点道具,但没有得到想要的结果。 Here is my code for the Select Dropdown.这是我的 Select 下拉菜单的代码。

<Select 
     className="profile-module-select-container"
     classNamePrefix="profile-module-select"
     defaultValue={{value: 0, label: 0}}
     options={options}
     openMenuOnFocus={false}
     autoFocus={options.isFocused}
     styles={styles}
     onChange={selected => {
        this.setState({
        optionSelect: selected.value
        }, () => {onChange(this.state.optionSelect, formKey)});
      }}
        onMenuOpen={(e, i, o) => {
        this.setState({
        selectMenuOpen: true
        });
       }}
       onMenuClose={() => {
       this.setState({
       selectMenuOpen: false
       });
       }}
       components={
          {
           IndicatorSeparator:() => null,
           DropdownIndicator: DropdownCaret,
           Placeholder: CustomPlaceholder,
           Option: CustomOptionComponent
           }
          }
           placeholder={placeholder}
           isSearchable={false}
           isClearable={false}
           name={name}
           value={options.filter(option => {return option.value === value})}
      />

For a functional component and typescript, implement the solution of @tkamath99 as below:对于功能组件和 typescript,实现@tkamath99 的解决方案如下:

<Select
  ref={selectRef}

  onInputChange={value => {
    selectRef.current.select.getNextFocusedOption = () => false;
  }}
/>

I have finally found an answer to the above question after going their issue tracker on github.在 github 上访问他们的问题跟踪器后,我终于找到了上述问题的答案。 The prop is yet to be merged in their main branch.该道具尚未合并到其主分支中。 Till then here is the work around if anyone tries to find the solution in future.到那时,如果有人试图在未来找到解决方案,这里就是解决方法。 So onInputChange, you have to return null on a function that gets passed to focus the next item in the list.所以 onInputChange,你必须在一个 function 上返回 null,它被传递以关注列表中的下一个项目。

<Select
 ref={ref => {
 this.selectRef = ref;
}}
 onInputChange={(value) => {this.selectRef.select.getNextFocusedOption = () => null }}
/>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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