javascript - 如何在功能组件中将下一个输入集中在按 Enter 键上?(TAB 行为)
问题描述
我想实现 TAB 行为,但要按 ENTER。
这是我解决它的尝试,但由于不使用Refs
.
我的想法是在每个输入中声明,接下来应该关注哪个元素,以及 Enter keyup 事件处理程序将该值设置为新的焦点字段。
我已经看到使用示例,useHook
但我不知道如何使用它,而不会useState
为每个输入发送大量的 's。
import React, { useState, useEffect } from "react";
function Form(props) {
// Holds the ID of the focused element
const [focusedElementId, setFocusedElementId] = useState("input-one");
// The actual focusing, after each re-render
useEffect(() => {
document.getElementById(focusedElementId).focus();
}, []);
useEffect(() => {
console.log("STATE:", focusedElementId);
}, [focusedElementId]);
// When Enter is pressed, set the focused state to the next element ID provided in each input
function handleKeyUp(e) {
e.which = e.which || e.keyCode;
if (e.which == 13) {
let nextElementId = e.target.attributes["data-focus"].value;
console.log(`HANDLER: Enter - focusing ${nextElementId}`);
setFocusedElementId(nextElementId);
}
}
return (
<div>
<div className="form-items" style={{ display: "flex" }}>
<div className="input-group">
<label>{"Input One"}</label>
<br />
<input
type={"text"}
id={"input-one"}
onKeyUp={handleKeyUp}
data-focus={"input-two"}
/>
</div>
<div className="input-group">
<label>{"Input Two"}</label>
<br />
<input
type={"text"}
id={"input-two"}
onKeyUp={handleKeyUp}
data-focus={"input-three"}
/>
</div>
<div className="input-group">
<label>{"Input Three"}</label>
<br />
<input
type={"text"}
id={"input-three"}
onKeyUp={handleKeyUp}
data-focus={"input-one"}
/>
</div>
</div>
</div>
);
}
export default Form;
解决方案
我设法让它与refs
.
这是沙盒。
它看起来足够简单和干净,但我仍然对你的意见感到好奇。
import React from "react";
function Form() {
let one = React.createRef();
let two = React.createRef();
let three = React.createRef();
// When Enter is pressed, set the focused state to the next element ID provided in each input
function handleKeyUp(e) {
e.which = e.which || e.keyCode;
// If the key press is Enter
if (e.which == 13) {
switch (e.target.id) {
case "input-one":
two.current.focus();
break;
case "input-two":
three.current.focus();
break;
case "input-three":
one.current.focus();
break;
default:
break;
}
}
}
return (
<div>
<div className="form-items" style={{ display: "flex" }}>
<div className="input-group">
<label>{"Input One"}</label>
<br />
<input
type={"text"}
id={"input-one"}
onKeyUp={handleKeyUp}
ref={one}
/>
</div>
<div className="input-group">
<label>{"Input Two"}</label>
<br />
<input
type={"text"}
id={"input-two"}
onKeyUp={handleKeyUp}
ref={two}
/>
</div>
<div className="input-group">
<label>{"Input Three"}</label>
<br />
<input
type={"text"}
id={"input-three"}
onKeyUp={handleKeyUp}
ref={three}
/>
</div>
</div>
</div>
);
}
export default Form;
推荐阅读
- python - 如何将 Python 脚本转换为 Android 应用程序
- python - 如何用python设置桌面背景
- sql - 将 JSON 对象添加到 Postgres 中的特定键
- nginx - 带有 tracd 和 subs_filter 的 nginx
- drupal - 使用 graphQL 查询从 drupal 获取视频文件
- python - Flask App 中的 Dash Plotly 不会更新/刷新数据
- syntax - Ada 语言理解:重命名包的子代
- node.js - Node中等效的Java同步块
- swift - SwiftUI 按钮点击重置 SpriteKit 动画 - 这是一个错误吗?
- java - cursor.getString 不能与字符串变量相等