简体   繁体   English

如何专注于第一个输入字段 - redux-form

[英]How to focus on first input field - redux-form

I'm trying to set the first input field in any form to be focused. 我正在尝试以任何形式设置第一个输入字段以进行聚焦。 So, what is the best practice to implement something like this? 那么,实现这样的事情的最佳做法是什么? And how could I check the type of the field? 我怎么能检查字段的类型? is it text, number, checkbox or etc.? 是文本,数字,复选框等?

Is it by listening to @redux-form actions, and then dispatch a focus action? 是通过听@redux-form动作,然后发出焦点动作?

Can any one suggest a solution ? 任何人都可以提出解决方案吗?

Using this approach, you can configure your autoFocus every time. 使用此方法,您可以每次配置autoFocus

<Field component={renderFirstInput} autoFocus={true/false} />

const renderFirstInput = ({ autoFocus }) => {
  return ( <input autoFocus={autoFocus} /> )

}

I think that for redux-form (v6+) the current state of art is to provide autoFocus property for the <input /> which you would like to be auto focused. 我认为对于redux-form (v6 +),当前的技术状态是为你想要自动聚焦的<input />提供autoFocus属性。

Example: 例:

<Field component={renderFirstInput} type="text" />

const renderFirstInput = field => {
  return ( <input {...field.input} autoFocus /> )

}

Here is the Github issue link 这是Github问题链接

You may of course provide autoFocus prop as a field.input key. 您当然可以将autoFocus prop作为field.input键提供。

在React文档中有一个很好的例子: https//facebook.github.io/react/docs/refs-and-the-dom.html

If you want to use stateless functional components refs will do you no good. 如果你想使用无状态功能组件,refs对你没有好处。 My solution is to pass a first={true} prop to Field and check for it inside the component. 我的解决方案是将第一个= {true}道具传递给Field并在组件内部检查它。 It it exists then set autoFocus on the input field. 它存在然后在输入字段上设置autoFocus。

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

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