keyboard navigation #2752
Replies: 3 comments 12 replies
-
This feature is not natively supported but I was able to simulate it using following function: ...
const tbodyRef = React.useRef(null);
// Function to handle key press on a row
const handleKeyDown = ( event, row ) => {
event.stopPropagation();
const currentRow = tbodyRef.current?.children.namedItem(row.id);
switch (event.key) {
case "ArrowUp":
currentRow?.previousElementSibling?.focus();
break;
case "ArrowDown":
currentRow?.nextElementSibling?.focus();
break;
default: break;
}
};
...
//Then find the function with TableRow `tr`
<tbody {...getTableBodyProps()} ref={tbodyRef}>
{rows.map((row) => {
prepareRow(row);
return (
<tr {...row.getRowProps()} id={row.id} tabIndex={0} onKeyDown={(e) => handleKeyDown(e, row)} >
...
</tr>)}
</tbody>
... To see selected rows, CSS must added to focus state of the row: tr:focus {
background-colour: LightGray;
} CodeSandBox: https://codesandbox.io/s/laughing-antonelli-yvsyl |
Beta Was this translation helpful? Give feedback.
-
Are there any future plans to support this natively? The closest solution I could get to this was by using the https://www.npmjs.com/package/accessible-react-table package, but it wasn't consistent across browsers, as well as the different pages on my app. |
Beta Was this translation helpful? Give feedback.
-
Hey folks, I recently released a library to work with table navigation. It works quite smoothly with We use it in Neo4j Design system for table navigation, and works quite well for our use cases. If docs are confusing please ping me to help. Library (npm): https://www.npmjs.com/package/@table-nav/react Also example with |
Beta Was this translation helpful? Give feedback.
-
Wondering if there is any way to setup navigation of a table's rows through up/down arrow keys or is this something not available through react-table? getTrProps only works with onClick events
Beta Was this translation helpful? Give feedback.
All reactions