我决定使用它react-table
来构建网格。
拥有更好的UI只是一件事,那就是我想在单击添加新按钮后专注于新项的输入字段。
我打算使用ref
但不知道如何fowardRef
处理子项
这是我使用react-table的示例:
https://codesandbox.io/s/beautiful-proskuriakova-lpfxn
在这种情况下,我想First Name
在添加新行之后专注于该领域。
更新:通过使用autoFocus
本机HTML的支持,我可以专注于输入。
答案在这里
但是我仍然想通过使用 React Ref
我用平JavaScript
原来达到你的目标
const setFocus = () => {
//Add id to the table
document.getElementsByTagName("TABLE")[0].setAttribute("id", "mytable");
//table > tbody > tr (latest added row) > td (first cell in the row) > input
let cell = document.getElementById("mytable").lastElementChild
.lastElementChild.firstElementChild.children[0];
cell.focus();
};
const addNew = () => {
setData(old => [...old, {}]);
window.requestAnimationFrame(setFocus);
};
我requestAnimationFrame
之所以使用它是因为我正在处理DOM,请查看此答案以获取更多详细信息。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句