The table renders its UI according to the props, and every change of the UI is an action. Every part of the UI can be customized without replacing the table: add attributes and event handlers to an element or render your own content in it.
Elements
Every part of the UI (Row, GroupRow, Column, HeaderCell, Cell, Editors) is a child component: its attributes and its content are customized by the childComponents prop, see the list of the child components
Attributes and content
A child component has two options: elementAttributes returns the attributes and the event handlers of its element, they are merged with the default ones, and content returns its content, undefined keeps the default one (see Events Demo, Custom Cell).
const childComponents = {
cell: {
elementAttributes: (props) => ({
className: 'my-cell-class', // add custom class to cell element
onClick: (e, extendedEvent) => { // additional onClick handler for cell
const { childProps: { dispatch } } = extendedEvent;
dispatch({ type: 'MY_CELL_onClick', extendedEvent });
},
}),
content: (props) => (props.column.key === 'someKey' ? <div>Custom Content</div> : undefined), // default in case nothing is returned
},
dataRow: {
elementAttributes: (props) => ({
onContextMenu: (e, extendedEvent) => { // additional onContextMenu handler for dataRow
extendedEvent.dispatch({ type: 'MY_CELL_onContextMenu', extendedEvent });
},
})
}
};
Custom editor
cellEditor replaces the editor of a cell. The editor dispatches the actions of the table to save the value and to close itself:
import { closeEditor, updateCellValue } from 'ka-table/actionCreators';
// custom lookup editor example
// define editor component
const CustomLookupEditor: React.FC<ICellEditorProps> = ({
column, dispatch, rowKeyValue, value,
}) => {
const close = () => {
dispatch(closeEditor(rowKeyValue, column.key));
};
// value is unknown: the column has booleans
const [editorValue, setValue] = useState(value === true);
return (
<div>
<select
className='form-control'
autoFocus={true}
defaultValue={String(editorValue)}
onBlur={() => {
dispatch(updateCellValue(rowKeyValue, column.key, editorValue));
close();
}}
onChange={(event) => {
setValue(event.currentTarget.value === 'true');
}}>
<option value={'true'}>True</option>
<option value={'false'}>False</option>
</select>
</div >
);
};
const childComponents = {
cellEditor: {
content: (props) => {
switch (props.column.key) {
case 'passed': return <CustomLookupEditor {...props}/>;
}
}
}
};
// set editor for specific column of tableProps
<Table
columns={[
// ...
{
dataType: DataType.Boolean,
key: 'passed',
title: 'Passed',
}
// ...
]}
childComponents={childComponents}
// ...
/>
Demo: Custom Editor
ChildComponents
All the child components of the table. Every one is a ChildComponent with the props of its element; the types of the props are in props.ts.
Demos: Events Demo, Custom Cell
ChildComponent<T>
| Name | Type | Description |
|---|---|---|
| elementAttributes | (props: PropsWithChildren<T>) => ChildAttributesItem<T> | void |
(Demo: Events Demo) |
| content | (props: PropsWithChildren<T>) => ReactNode | Promise<ReactNode> |
undefined renders the default content. A function component can be passed as content (Demo: Custom Cell) |
With memoizedRows declare childComponents outside of the component (or memoize it), otherwise all the rows are re-rendered.
ChildAttributesItem<T>
This object is an extension for React HTMLAttributes. It contains all attributes and all react Synthetic Events, but in each event it adds a second parameter which contains additional data with AttributeTableData type.
AttributeTableData<T>
A second parameter in each react Synthetic Event. Contains component-related information.
| Name | Type | Description |
|---|---|---|
| baseFunc | (...args: unknown[]) => void |
Contains the default function of the overridden attribute, e.g. the onClick which the table sets: it is easy to add additional logic and execute the default behaviour where you want it |
| childElementAttributes | AllHTMLAttributes<HTMLElement> |
Default HTMLAttributes of the component |
| childProps | T |
Props of the component |
| dispatch | (action: TableAction | CustomAction) => void |
Dispatches an action of the table or of your own code |
UI Actions
There are actions which ka-table UI dispatches in response to user interaction out-of-the-box:
| Action Type | UI event |
|---|---|
'CloseEditor' |
click outside editor or Enter Key press (to apply value) or ESC press (to discard changes) |
'GroupColumn' |
drag and drop the column to the group panel |
'OpenEditor' |
click by data cell, Enter or F2 on the focused cell |
'ReorderColumns' |
drag and drop the column |
'ReorderRows' |
drag and drop the row |
'ResizeColumn' |
drag and drop of the right border of the column |
'ScrollTable' |
scrolling of a table with virtual scrolling to other rows or columns |
'SetFocused' |
a cell or a group row gets the focus: by a click, Tab or the keys of the keyboard navigation |
'UngroupColumn' |
click by the cross of a column in the group panel |
'UpdateCellValue' |
click outside editor or Enter Key press - executes before 'CloseEditor' action |
'UpdateEditorValue' |
click outside editor or Enter Key press after the value is changed, when editingMode is not 'cell' (the editors of openRowEditors and openAllEditors): the value is kept in editableCells |
'UpdateFilterRowValue' |
each change of value in filter row editor |
'UpdateGroupsExpanded' |
click by group arrow cell |
'UpdateHeaderFilterPopupState' |
click by the header filter button of a column: the popup is shown or hidden |
'UpdateHeaderFilterSearchValue' |
each change of the search text in the header filter popup |
'UpdateHeaderFilterValues' |
a value is checked or unchecked in the header filter popup |
'UpdatePageIndex' |
click by page number |
'UpdatePageSize' |
click by page size |
'UpdatePopupPosition' |
the header filter popup is shown |
'UpdateSortDirection' |
click by sort button |
'UpdateTreeGroupsExpanded' |
click by the arrow of a tree row |
Keyboard and accessibility
The keyboard navigation is enabled by default (keyboardNavigation={false} turns it off): the table is a WAI-ARIA grid (a treegrid for a tree) with a single Tab stop.
| Keys | Action |
|---|---|
| Tab | focuses the table: the last focused cell or the first one |
| Arrows | move the focus between the cells, with Ctrl or Cmd to the last one |
| Home, End | the first and the last cell of the row, with Ctrl or Cmd of the page |
| PageUp, PageDown | change the page if paging is enabled |
| Enter, F2 | open the editor of the cell (EditingMode.Cell), the focus returns to the cell when the editor is closed |
| Enter, Space, Left, Right on a group row | expand and collapse the group |
| Enter, Space on the cell with the arrow of a tree row | expand and collapse the row |
The focused cell is kept in focused.cell (a group row in focused.group). A controlled table has to pass SetFocused actions to kaReducer as all the other actions. Sorting, paging, the group panel, the header filter and the validation messages have ARIA roles, states and names.
Demo: Keyboard Navigation
Fixed columns
fixed: 'left' or fixed: 'right' of a column makes it stick to the edge of the table when it is scrolled horizontally. The fixed columns need a numeric width: the offset of a column is the width of the other fixed columns of its edge.
columns={[
{ key: 'name', title: 'Name', width: 200, fixed: 'left' },
// ...
{ key: 'actions', width: 80, fixed: 'right' },
]}
Demo: Fixed Column
Themes
The themes, the CSS custom properties and the styles of Bootstrap and Material UI are described in Themes.