【问题标题】:How to focus on an input field in ReactJS when there are multiple inputs in the component?当组件中有多个输入时,如何关注 ReactJS 中的输入字段?
【发布时间】:2020-01-29 10:54:45
【问题描述】:

我正在开发一个 ReactJS 应用程序。我有一个生成 HTML 表格的组件。使用 Map 一个子组件用于为表生成行。每行包含一定数量的输入字段。

编辑输入字段时,在某些情况下会在父组件中触发模态窗口。模式让用户可以选择三个按钮来点击。当单击模式中的特定按钮时,我希望焦点返回到用户正在编辑的输入字段。

谁能给我一些关于如何实现这一点的建议吗?

我研究过使用 refs,但我看到的所有示例都用一个只有一个输入的组件来演示它。因为模态是从父组件触发的,所以我很难理解如何使用/获取子组件的 ref。

感谢任何帮助。

【问题讨论】:

  • 寻找参考...!

标签: javascript reactjs focus


【解决方案1】:

我希望焦点返回到用户正在编辑的输入字段。

您有多个输入字段,并且您希望专注于某个输入。为了能够做到这一点,您需要多个 refs --12 refs 在您的案例中--。

  1. 像这样定义你的参考:

    const inputRef = useRef([]);
    
  2. 您需要将输入节点分配给 ref 数组。如果您使用某种循环来生成输入字段,您可以这样做:

    <input ref={el => inputRef.current[i] = el} />
    
  3. 现在,您需要一个变量来存储当前正在编辑的输入的索引。

  4. 最后,你可以使用这个变量,它保存最后编辑的输入字段的索引,来实现你想要的:

    inputRef.current[i].focus()
    

如果您使用的是类组件:

  1. 在你的构造函数中定义一个数组:

    this.myRefs = []
    
  2. 像这样分配 HTML 节点:

    <input ref={el => this.myRefs[i] = el} />
    
  3. 这一步没有改变。

  4. 现在您可以像这样聚焦特定的输入元素:

    this.myRefs[i].focus()
    

【讨论】:

  • 我正在使用类组件
  • 更新了我的答案。 @MrB
  • 谢谢,如何将 ref 传递给父组件?我正在从父组件触发一个模态窗口。
【解决方案2】:

使用@user9408899 的回答作为指导并阅读,我想出了以下解决方案(仅显示与解决方案直接相关的部分代码)。

父组件

  • 实现了一个inputRefToFocus 属性来存储我们的引用 想要添加焦点。
  • 实现了setInputRefToFocus(ref)在状态中分配上面的属性,也将this和inputRefToFocus传递给子组件。
    export class Parent extends React.Component {
      constructor(props) {
        super(props);
        this.state = {
          ...
          // this is the ref for the input field to focus on
          // when modal window appears, depending on button clicked,
          // we want to focus on the field user was editing
          inputRefToFocus: null
        };

        this.setInputRefToFocus = this.setInputRefToFocus.bind(this);
      }

      setInputRefToFocus(ref) {
        this.setState({ inputRefToFocus: ref });
      }

      render() {
        { rows.map(elem => (
          <PivotTableRow
            setInputRefToFocus={this.setInputRefToFocus}
            inputRefToFocus={this.state.inputRefToFocus}
          />
        )) }
      }
    }

子组件

  • 当输入元素被创建时,每个元素都被添加到 refs 数组中。

  • 当输入获得焦点时,我们在父组件的inputRefToFocus属性中设置它的ref。

  • 在componentDidUpdate()中设置焦点
export class Child extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      ...
    };

    this.inputRef = [];

  }

  componentDidUpdate(prevProps) {
    if (this.props !== prevProps) {
      if (this.inputRef[this.props.inputRefToFocus] !== undefined) {
        this.inputRef[this.props.inputRefToFocus].focus();
      }
    }
  }

  render() {
    return (
      // loop to generate input elements
      <Input
        ref={el => { this.inputRef[dummyvalue] = el; }
        onChange={...}
        onFocus={() => this.props.setInputRefToFocus(dummyvalue)}
      /> 
    );
  }
}

我觉得代码可以显着改进,因为这是我的第一次尝试

【讨论】:

    猜你喜欢
    • 2021-11-06
    • 2018-09-18
    • 2021-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-02
    相关资源
    最近更新 更多