【问题标题】:React.js - how to hit enter without creating an enter space (paragraph space)?React.js - 如何在不创建输入空间(段落空间)的情况下点击输入?
【发布时间】:2020-09-14 14:09:53
【问题描述】:

当您点击输入时,将创建一个新字段,但问题是 focus 并且焦点将移至新创建的字段,但问题是:它离开 an enter space in the previous 字段 `

import React, { useState } from "react";
import "./styles.css";

export default function App() {
  let elements = [
    { id: 0, text: "first", autoFocus: true },
    { id: 1, text: "second", autoFocus: true },
    { id: 2, text: "third", autoFocus: true }
  ];
  const [state, setstate] = useState(elements);
  function handChange(e) {
    if (e.keyCode == 13) {
      const index = state.findIndex((item) => item.id == e.target.id);
      //i'm using autoFocus to move the focus  (I-beam pointer) to the nex field.
      //but i still get errors with it
      Object.assign(state, (state[index].autoFocus = false));

      setstate((pre) => {
        return [
          ...pre.slice(0, index + 1),
          { id: 3, text: "xxx", autoFocus: true },
          ...pre.slice(index + 1, state.length)
        ];
      });
      setTimeout(() => {
        document.getElementById(index + 1).focus();
      }, 0);
    }
  }

  return (
    <div className="App">
      {state.map((e) => (
        <div contentEditable="true" id={e.id} onKeyUp={handChange}>
          {e.text}
        </div>
      ))}
    </div>
  );
}

codesandbox

【问题讨论】:

    标签: javascript reactjs react-hooks focus


    【解决方案1】:

    焦点属性不适用于div 元素。

    您应该在 React 中使用带有 autoFocus 属性的 input 元素。

    <input autoFocus={e.autoFocus} value={e.text} id={e.id} onKeyUp={handChange}/>
    

    【讨论】:

      【解决方案2】:

      尝试克隆状态,然后通过更新 autoFocustext 属性对其进行映射:

       function handChange(e) {
          if (e.keyCode === 13) {
            let _state = [...state];
      
            setstate(
              _state.map((item) => {
                if (+e.target.id === +item.id) {
                  return {
                    ...item,
                    autoFocus: false
                  };
                } else if (+e.target.id + 1 === +item.id + 1) {
                  return {
                    ...item,
                    autoFocus: true,
                    text: "xxx"
                  };
                } else {
                  return item;
                }
              })
            );
          }
        }
      
        return (
          <div className="App">
            {state.map((e) => (
              <input
                focus={e.autoFocus}
                contentEditable="true"
                id={e.id}
                onKeyUp={handChange}
                value={e.text}
              />
            ))}
          </div>
        );
      

      【讨论】:

      • 我使用 `document.getElementById(index + 1).focus();` 解决了这个问题,但需要通过 settimeout(,0) 进行处理
      • 不建议这样操作 DOM,你的组件应该是数据驱动的
      • 你正在使用 reactjs,你应该尊重它的模式
      • 我尊重你的观点,但我编程是为了效率,而不是崇拜框架
      猜你喜欢
      • 2021-07-09
      • 1970-01-01
      • 2019-11-28
      • 1970-01-01
      • 2023-04-04
      • 1970-01-01
      • 2011-02-15
      • 2016-02-01
      • 1970-01-01
      相关资源
      最近更新 更多