Keyboard Navigation for Rows
To enable keyboard navigation for rows, specify keyboardNavigation=“row”
.
When row navigation is enabled, clicking a row highlights it and the user can use the arrow keys to navigate the table rows.
Click on the table and use the arrow keys to navigate the rows.
import { InfiniteTable, DataSource, DataSourceData, } from '@infinite-table/infinite-react'; import type { InfiniteTablePropColumns } from '@infinite-table/infinite-react'; import * as React from 'react'; type Developer = { id: number; firstName: string; lastName: string; country: string; city: string; currency: string; preferredLanguage: string; stack: string; canDesign: 'yes' | 'no'; hobby: string; salary: number; age: number; }; const dataSource: DataSourceData<Developer> = () => { return fetch( 'https://infinite-table.com/.netlify/functions/json-server' + `/developers1k-sql?` ) .then((r) => r.json()) .then((data: Developer[]) => data); }; const columns: InfiniteTablePropColumns<Developer> = { preferredLanguage: { field: 'preferredLanguage' }, country: { field: 'country' }, salary: { field: 'salary', type: 'number', }, age: { field: 'age' }, canDesign: { field: 'canDesign' }, firstName: { field: 'firstName' }, stack: { field: 'stack' }, id: { field: 'id' }, hobby: { field: 'hobby' }, city: { field: 'city' }, currency: { field: 'currency' }, }; const domProps = { style: { height: '90vh' } }; export default function KeyboardNavigationForCells() { return ( <> <DataSource<Developer> primaryKey="id" data={dataSource}> <InfiniteTable<Developer> keyboardNavigation="row" columns={columns} /> </DataSource> </> ); }
Note
- Use
ArrowUp
andArrowDown
to navigate to the previous and next row. - Use
PageUp
andPageDown
to navigate the rows vertically by pages (a page is considered equal to the visible row count). - Use
Home
andEnd
to navigate vertically to the first and last row respectively
Other possible values for the keyboardNavigation
prop, besides "row"
, are "cell"
and false
.
Using a default active row
You can also specify an initial active row, by using defaultActiveRowIndex=2
. This tells the table that there should be a default active row, namely the one at index 2 (so the third row).
This example starts with row at index 2
already active.
import { InfiniteTable, DataSource, DataSourceData, } from '@infinite-table/infinite-react'; import type { InfiniteTablePropColumns } from '@infinite-table/infinite-react'; import * as React from 'react'; type Developer = { id: number; firstName: string; lastName: string; country: string; city: string; currency: string; preferredLanguage: string; stack: string; canDesign: 'yes' | 'no'; hobby: string; salary: number; age: number; }; const dataSource: DataSourceData<Developer> = () => { return fetch( 'https://infinite-table.com/.netlify/functions/json-server' + `/developers1k-sql?` ) .then((r) => r.json()) .then((data: Developer[]) => data); }; const columns: InfiniteTablePropColumns<Developer> = { preferredLanguage: { field: 'preferredLanguage' }, country: { field: 'country' }, salary: { field: 'salary', type: 'number', }, age: { field: 'age' }, canDesign: { field: 'canDesign' }, firstName: { field: 'firstName' }, stack: { field: 'stack' }, id: { field: 'id' }, hobby: { field: 'hobby' }, city: { field: 'city' }, currency: { field: 'currency' }, }; const domProps = { style: { height: '90vh' } }; export default function KeyboardNavigationForRows() { return ( <> <DataSource<Developer> primaryKey="id" data={dataSource}> <InfiniteTable<Developer> domProps={domProps} columns={columns} keyboardNavigation="row" defaultActiveRowIndex={2} /> </DataSource> </> ); }
Listening to active row changes
You can easily listen to changes in the row navigation by using the onActiveRowIndexChange
callback.
Note
When you use controlled activeRowIndex
, make sure to use onActiveRowIndexChange
to update the prop value, as otherwise the component will not update on navigation
This example starts with row at index 2
already active and uses onActiveRowIndexChange
to update activeRowIndex
.
import { InfiniteTable, DataSource, DataSourceData, } from '@infinite-table/infinite-react'; import type { InfiniteTablePropColumns } from '@infinite-table/infinite-react'; import * as React from 'react'; type Developer = { id: number; firstName: string; lastName: string; country: string; city: string; currency: string; preferredLanguage: string; stack: string; canDesign: 'yes' | 'no'; hobby: string; salary: number; age: number; }; const dataSource: DataSourceData<Developer> = () => { return fetch( 'https://infinite-table.com/.netlify/functions/json-server' + `/developers1k-sql?` ) .then((r) => r.json()) .then((data: Developer[]) => data); }; const columns: InfiniteTablePropColumns<Developer> = { preferredLanguage: { field: 'preferredLanguage' }, country: { field: 'country' }, salary: { field: 'salary', type: 'number', }, age: { field: 'age' }, canDesign: { field: 'canDesign' }, firstName: { field: 'firstName' }, stack: { field: 'stack' }, id: { field: 'id' }, hobby: { field: 'hobby' }, city: { field: 'city' }, currency: { field: 'currency' }, }; const domProps = { style: { height: '90vh' } }; export default function KeyboardNavigationForRows() { const [activeRowIndex, setActiveRowIndex] = React.useState(2); return ( <> <div style={{ color: 'var(--infinite-cell-color)', }}> Current active row: {activeRowIndex}. </div> <DataSource<Developer> primaryKey="id" data={dataSource}> <InfiniteTable<Developer> keyboardNavigation="row" activeRowIndex={activeRowIndex} onActiveRowIndexChange={setActiveRowIndex} columns={columns} /> </DataSource> </> ); }
Theming
By default, the style of the element that highlights the active row is the same style as that of the element that highlights the active cell.
The easiest is to override the style is via those three CSS variables:
--infinite-active-cell-border-color--r
- thered
component of the border color--infinite-active-cell-border-color--g
- thegreen
component of the border color--infinite-active-cell-border-color--b
- theblue
component of the border colorThe initial values for those are
77
,149
and215
respectively, so the border color isrgb(77, 149, 215)
.In addition, the background color of the element that highlights the active row is set to the same color as the border color (computed based on the above
r
,g
andb
variables), but with an opacity of0.25
, configured via the--infinite-active-row-background-alpha
CSS variable.When the table is not focused, the opacity for the background color is set to
0.1
, which is the default value of the--infinite-active-row-background-alpha--table-unfocused
CSS variable.
Note
--infinite-active-cell-border-color--r
--infinite-active-cell-border-color--g
--infinite-active-cell-border-color--b
to control border and background color of the active row highlight element.
No, it’s not a mistake that the element that highlights the active row is configured via the same CSS variables as the element that highlights the active cell. This is deliberate - so override CSS variables for cell, and those are propagated to the row highlight element.
There are other CSS variables as well, that give you fined-tuned control over both the border and background color for the active row, if you don’t want to use the above three variables to propagate the same color across both border and background.
--infinite-active-cell-background
- the background color. If you use this, you need to set opacity yourself. Applied for both cell and row.--infinite-active-row-background
- the background color. If you use this, you need to set opacity yourself. If this is specified, it takes precendence over--infinite-active-cell-background
--infinite-active-cell-background
- the background color. If you use this, you need to set opacity yourself. Applied for both cell and row.--infinite-active-row-background
- the background color. If this is specified, it takes precedence over--infinite-active-cell-background
--infinite-active-row-border
- border configuration (eg:2px solid magenta
). If you use this, it will not be propagated to the background color.
For more details on the CSS variables, see the CSS Variables documentation.
Use the color picker to configured the desired color for the active row highlight
import { InfiniteTable, DataSource, DataSourceData, debounce, } from '@infinite-table/infinite-react'; import type { InfiniteTablePropColumns } from '@infinite-table/infinite-react'; import * as React from 'react'; import { useState } from 'react'; import { useMemo } from 'react'; import { HTMLProps } from 'react'; import { ChangeEvent } from 'react'; type Developer = { id: number; firstName: string; lastName: string; country: string; city: string; currency: string; preferredLanguage: string; stack: string; canDesign: 'yes' | 'no'; hobby: string; salary: number; age: number; }; const dataSource: DataSourceData<Developer> = () => { return fetch( 'https://infinite-table.com/.netlify/functions/json-server' + `/developers1k-sql?` ) .then((r) => r.json()) .then((data: Developer[]) => data); }; const columns: InfiniteTablePropColumns<Developer> = { preferredLanguage: { field: 'preferredLanguage' }, country: { field: 'country' }, salary: { field: 'salary', type: 'number', }, age: { field: 'age' }, canDesign: { field: 'canDesign' }, firstName: { field: 'firstName' }, stack: { field: 'stack' }, id: { field: 'id' }, hobby: { field: 'hobby' }, city: { field: 'city' }, currency: { field: 'currency' }, }; const domProps = { style: { height: '90vh' } }; const rgb = { r: 77, g: 149, b: 215, }; const defaultColor = `#${rgb.r.toString( 16 )}${rgb.g.toString(16)}${rgb.b.toString(16)}`; export default function KeyboardNavigationForRows() { const [color, setColor] = useState({ ...rgb, }); const domProps = useMemo(() => { return { style: { '--infinite-active-cell-border-color--r': color.r, '--infinite-active-cell-border-color--g': color.g, '--infinite-active-cell-border-color--b': color.b, // for the same of the example being more obvious, // make the opacity of the unfocused table same as the one used on focus '--infinite-active-cell-background-alpha--table-unfocused': '0.25', // but this defaults to 0.1 }, } as HTMLProps<HTMLDivElement>; }, [color]); const onChange = useMemo(() => { const onColorChange = ( event: ChangeEvent<HTMLInputElement> ) => { const color = event.target.value; const r = parseInt(color.substr(1, 2), 16); const g = parseInt(color.substr(3, 2), 16); const b = parseInt(color.substr(5, 2), 16); setColor({ r, g, b, }); }; return debounce(onColorChange, { wait: 200 }); }, []); return ( <> <div style={{ color: 'var(--infinite-cell-color)', }}> Select color{' '} <input type="color" onChange={onChange} defaultValue={defaultColor} /> </div> <DataSource<Developer> primaryKey="id" data={dataSource}> <InfiniteTable<Developer> keyboardNavigation="row" defaultActiveRowIndex={7} domProps={domProps} columns={columns} /> </DataSource> </> ); }