A table displays data in rows and columns and enables a user to navigate its contents via directional navigation keys, and optionally supports row selection and sorting.
Usage
import { Table } from '@console/nimbus-ui/core';Features
A table consists of a container element, with columns and rows of cells containing data inside. The cells within a table may contain focusable elements or plain text content. If the table supports row selection, each row can optionally include a selection checkbox. Additionally, a "select all" checkbox may be displayed in a column header if the table supports multiple row selection.
- Row selection – Single or multiple selection, with optional checkboxes, disabled rows, and both toggle and replace selection behaviors.
- Columns – Support for column sorting and row header columns. Columns may optionally allow user resizing via mouse, touch, and keyboard interactions.
- Interactive children – Table cells may include interactive elements such as buttons, menus, etc.
Examples
Dynamic
Table makes use of Collections, a concept in react-aria-components that allows for a JSX-based interface to help maps
over your data and applies a function for each item to render it. The following example shows how a collection can be
rendered based on dynamic data for both the Table.Column and Table.Row components.
[React Aria Docs | Dynamic Collections](https://react-spectrum.adobe.com/react-aria/collections.html#dynamic-collections)
<br/>
[React Aria Docs | Table Collections](https://react-spectrum.adobe.com/react-aria/Table.html#resize-events)
Is Compact
When isCompact is set to true, the table will have less padding and margins.
This is useful for displaying more data in a smaller space.
Single Selection
By default, Table doesn't allow row selection but this can be enabled using the selectionMode prop.
The selectionMode prop can be set to single, multiple, or none.
When selectionMode is set to single, only one row can be selected at a time.
Disallow Empty Selection
When disallowEmptySelection is set to true, the table will not allow the user to deselect all rows.
Multiple Selection
When selectionMode is set to multiple, the table allows multiple rows to be selected.
The user can select multiple rows by holding down the Ctrl (or Cmd on Mac) key while clicking on rows.
Default Selected Keys
Use defaultSelectedKeys to provide a default set of selected rows.
Please note that the value of the selected keys must match the id prop of the row.
For example, if your is id={"mike"} your defaultSelectedKeys prop much match defaultSelectedKeys={["mike"]}.
Controlled Single Selection
To programmatically control row selection, use the selectedKeys prop paired with the onSelectionChange callback. The id prop from the selected rows will be passed into the callback when the row is pressed, allowing you to update state accordingly.
Sortable
This story demonstrates how to implement client-side sorting in a table.
It uses the useAsyncList hook to fetch data from an API and sort it based on user interaction.
The table allows users to sort by clicking on the column headers.
The sortDescriptor and sort function are used to manage the sorting state.
The items array is populated with data fetched from the API, and the table displays this data.
The allowsSorting prop on the Table.Column components enables sorting functionality for those columns.
Server Side Sort
You can use useAsyncList on the server as well. This function will return a sort fn() and a sortDescriptor.
These properties will trigger when passed to the onSortChange prop and the header is clicked.
Empty State
If you need to display an empty state when there are no results, you can use the renderEmptyState prop on the Table.Body component.
This prop accepts a function that returns a React element to display when there are no items in the body.
If you do not provide a renderEmptyState prop, the default empty state will be displayed.
On Row Action
Use the onRowAction prop to create actions from clicking rows.
In the default "toggle" selection behavior, when nothing is selected, clicking or tapping the row triggers the row action. When at least one item is selected, the table is in selection mode, and clicking or tapping a row toggles the selection. Actions may also be triggered via the Enter key, and selection using the Space key.
This behavior is slightly different in the "replace" selection behavior, where single clicking selects the row and actions are performed via double click.
On Action
Rows may also have a row action specified by directly applying onAction on the Row itself.
This may be especially convenient in static collections.
If onAction is also provided to the Table, both the table's and the row's onAction are called.
Links
Pass the href prop to the <Row> component in order to create link rows.
Disabled Rows
Use the isDisabled prop on Table.Row to disable specific rows.
Resizable
To create a resizable table, you can use the ResizableTableContainer component.
This component wraps the Table component and provides the ability to resize columns by dragging the ColumnResizer.
The isResizable prop is a required prop on Table.Column to make specific columns resizable.
You can also set the minWidth, maxWidth, and width props on Table.Column to control the size of the columns.
The ResizableTableContainer component will automatically handle the resizing logic for you.
API
Table
ResizableTableContainer
| Prop | Type | Default | Description |
|---|---|---|---|
| maxWidth | MaxWidth<string | number> | — | |
| minWidth | MinWidth<string | number> | — | |
| width | Width<string | number> | — |
Table
Attach all subcomponents and set types for React DevTools & Storybook.
| Prop | Type | Default | Description |
|---|---|---|---|
| data-rac-id | string | — | Data attribute for GTM purposes. Pass this property to add a custom id to the root element for GTM tracking. |
| data-test-id | string | — | Data attribute for E2E testing purposes |
| isCompact | boolean | false | Whether the table is compact |
| maxWidth | MaxWidth<string | number> | — | Allows you to set a `maxWidth` on the Table root element component. |
| minWidth | MinWidth<string | number> | — | Allows you to set a `minWidth` on the Table root element component. |
| ref | ForwardedRef<HTMLTableElement> | — | |
| renderEmptyState | () => ReactNode | — | @returns ReactNode to render when the table is empty. If not provided, a default empty state will be rendered. |
| width | Width<string | number> | — | Allows you to set a `width` on the Table root element component. |
React Aria
Figma