reactjs - 如何聚焦表格行?
问题描述
当用户按下 cmd+rightarrow 时,我尝试将焦点设置在表中的最后一行。以这种方式,用户应该能够在不使用鼠标的情况下选择 8 个行元素中的任何一个。键绑定工作完美(并添加了事件侦听器),但引用有问题。
const selRef = useRef<any>();
const onKeyDown = (e) => {
if ((e.ctrlKey || e.metaKey) && ['ArrowRight'].includes(e.key)) {
selRef.current.focus();
}
return (
<tbody>
{value.map((trip, index) => (
<div>
<tr
onClick={() =>
props.onSelect(trip, 'copy')
}
key={`history-row-${index}`}
className={styles.focused}
ref={selRef}
>
<td id="sr-td-shortcut">{index + 1}</td>
<td id="sr-td-pick">
{genFormattedAddress(trip.start)}
</td>
<td id="sr-td-drop">
{genFormattedAddress(trip.end)}
</td>
</tr>
</div>
))}
</tbody>
)
解决方案
tr
默认情况下元素不能接受焦点。tabindex={0}
要启用焦点行为,请添加<tr>
推荐阅读
- jquery - 如何在 jQuery 的 .match() 中使用正则表达式
- php - 如何在文档准备好后创建元素的 jquery ui 自动完成?
- r - 在 RStudio 中按条件分组
- android - 没有这样的属性:variantManager
- testing - Spark Structured Streaming 测试后期数据
- python - python中十六进制字符串的不同解释
- redis - Stack Exchange Redis 客户端超时
- apache-spark - 使用 Spark 读取 Azure Synapse 表
- reactjs - reactjs中的循环和渲染
- java - Java如何使用Intellij中另一个模块的方法