reactjs - 如何将焦点设置在 FormControl - Select 上? ( react )

标签 reactjs material-ui

我有material-ui - select并希望以编程方式关注此元素。

<FormControl 
className="select100">
<Select
    ref={(formControll) => { this.formControll = formControll; }}                                 
    native
    value={value}
    input={<Input id="integer" />}
>
    {possibleOptions.map((item, key) => {
      return (<option value={item} key={key}>{item}</option>)
    })}
</Select>

我尝试使用ref并编写this.formControll.focus();但 react 告诉我 focus() 不是一个函数。例如,使用按钮,引用就可以工作。

PS:我不需要自动对焦

谢谢

最佳答案

您可以将 Input 传递到 SelectautoFocus 属性中,这将应用于 Select

<Select
  native
  value={value}
  input={<Input id="integer" autoFocus={true} />}
>
  {possibleOptions.map((item, key) => {
    return (<option value={item} key={key}>{item}</option>)
  })}
</Select>

编辑
当我发布答案时,我错过了您不需要autoFocus的部分。

如果您在 Select 中使用输入,则可以使用 inputRef 属性,这会将下划线输入集中“附加”到选择。
Code exampleDocs.

关于reactjs - 如何将焦点设置在 FormControl - Select 上? ( react ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47942598/

相关文章:

javascript - 在不使用 testid 的情况下测试 Material ui Select 组件

css - 从 Material UI (mui) 中的 DataGrid 中移除底部边框

android - 为什么我的 Android 按钮没有显示正确的背景?

reactjs - Material UI 输入与选择对齐

javascript - 检查变量是否设置在全局范围内?

javascript - es2015中的多行赋值

javascript - react native : How to prevent state variables from getting re-set on hot reload?

javascript - 使用CDN时如何调用react logger函数

css - ReactJS+MaterialUI v1 自动嵌套列表填充

javascript - 在reactjs中执行onclick事件