--- title: Table description: "`Table` is a table component equipped with column sorting, row selection, and click event features." links: - source: https://github.com/yamada-ui/yamada-ui/tree/main/packages/react/src/components/table - storybook: https://yamada-ui.github.io/yamada-ui?path=/story/components-table--basic --- # Table `Table` is a table component equipped with column sorting, row selection, and click event features. ```tsx interface Data { id: string age: number email: string firstName: string lastName: string } const columnHelper = createColumnHelper() const columns = useMemo( () => [ columnHelper.accessor("id", { header: "ID", cellProps: { numeric: true } }), columnHelper.accessor("firstName", { header: "First name", lineClamp: 1 }), columnHelper.accessor("lastName", { header: "Last name", lineClamp: 1 }), columnHelper.accessor("age", { header: "Age", cellProps: { numeric: true }, }), columnHelper.accessor("email", { header: "Email", lineClamp: 1 }), ], [], ) const data = useMemo( () => Array.from({ length: 10 }, (_, index) => ({ id: index.toString(), age: faker.number.int({ max: 65, min: 18 }), email: faker.internet.email(), firstName: faker.person.firstName(), lastName: faker.person.lastName(), })), [], ) return ``` ## Usage ```tsx import { Table } from "@yamada-ui/react" ``` ```tsx import { Table } from "@/components/ui" ``` ```tsx import { Table } from "@workspaces/ui" ``` ```tsx
``` :::note `Table` internally uses [tanstack-table](https://tanstack.com/table) and [NativeTable](https://yamada-ui.com/docs/components/native-table.md). ::: :::tip For a simple table without column sorting or row selection, use [NativeTable](https://yamada-ui.com/docs/components/native-table.md). ::: ### Change Variant ```tsx const data = useMemo(() => createTableData(), []) return ( {(variant, index) => (
)} ) ``` ### Change Size ```tsx const data = useMemo(() => createTableData(), []) return ( {(size, index) => (
)} ) ``` ### Change Color Scheme ```tsx const data = useMemo(() => createTableData(), []) return ( {(colorScheme, index) => (
)} ) ``` ### Add Outer Border To add an outer border, set `withBorder` to `true`. ```tsx const data = useMemo(() => createTableData(), []) return
``` ### Add Column Separators To add column separators, set `withColumnBorders` to `true`. ```tsx const data = useMemo(() => createTableData(), []) return
``` ### Highlight on Row Hover To highlight rows when hovered, set `highlightOnHover` to `true`. ```tsx const data = useMemo(() => createTableData(), []) return
``` ### Striped To enable striped rows, set `striped` to `true`. ```tsx const data = useMemo(() => createTableData(), []) return
``` ### Scroll Area To enable scroll area, set `withScrollArea` to `true`. ```tsx const data = useMemo(() => createTableData(), []) return
``` ### Header Groups ```tsx const columns = useMemo( () => [ columnHelper.accessor("id", { footer: "ID", header: "ID", cellProps: { numeric: true }, }), columnHelper.group({ id: "user", columns: [ columnHelper.group({ id: "name", columns: [ columnHelper.accessor("firstName", { footer: "First name", header: "First name", lineClamp: 1, }), columnHelper.accessor("lastName", { footer: "Last name", header: "Last name", lineClamp: 1, }), ], footer: "Name", header: "Name", }), columnHelper.accessor("age", { footer: "Age", header: "Age", cellProps: { numeric: true }, }), columnHelper.accessor("email", { footer: "Email", header: "Email", lineClamp: 1, }), ], footer: "User", header: "User", }), ], [], ) const data = useMemo(() => createTableData(), []) return (
) ``` ### Footer Groups ```tsx const columns = useMemo( () => [ columnHelper.accessor("id", { footer: "ID", header: "ID", cellProps: { numeric: true }, }), columnHelper.group({ id: "user", columns: [ columnHelper.group({ id: "name", columns: [ columnHelper.accessor("firstName", { footer: "First name", header: "First name", lineClamp: 1, }), columnHelper.accessor("lastName", { footer: "Last name", header: "Last name", lineClamp: 1, }), ], footer: "Name", header: "Name", }), columnHelper.accessor("age", { footer: "Age", header: "Age", cellProps: { numeric: true }, }), columnHelper.accessor("email", { footer: "Email", header: "Email", lineClamp: 1, }), ], footer: "User", header: "User", }), ], [], ) const data = useMemo(() => createTableData(), []) return (
) ``` ### Disable Keyboard Navigation To disable keyboard navigation, set `enableKeyboardNavigation` to `false`. The default is `true`. ```tsx const data = useMemo(() => createTableData(), []) return (
) ``` ### Set Initial Focusable Cell To set the initial focusable cell, set `initialFocusableCell` to an object with the column and row indices (`{ colIndex: number, rowIndex: number }`). The default is `{ colIndex: 0, rowIndex: 0 }`. ```tsx const data = useMemo(() => createTableData(), []) return (
) ``` ### Handle Row Click Event To handle the row click event, use `onRowClick`. ```tsx const data = useMemo(() => createTableData(), []) return (
console.log(row)} withScrollArea /> ) ``` ### Handle Row Double Click Event To handle the row double click event, use `onRowDoubleClick`. ```tsx const data = useMemo(() => createTableData(), []) return (
console.log(row)} withScrollArea /> ) ``` ### Set Default Sorting To set the default sorting, set `defaultSorting` to an array of objects with the column `id` and direction. ```tsx const data = useMemo(() => createTableData(), []) return (
) ``` ### Disable Sorting To disable sorting, set `enableSorting` to `false`. The default is `true`. ```tsx const data = useMemo(() => createTableData(), []) return (
) ``` ### Disable Multi Sorting To disable multi sorting, set `enableMultiSort` to `false`. The default is `true`. ```tsx const data = useMemo(() => createTableData(), []) return (
) ``` ### Set Maximum Sortable Columns To set the maximum sortable columns, set `maxMultiSortColCount` to a number. ```tsx const data = useMemo(() => createTableData(), []) return (
) ``` ### Manual Sorting To use manual sorting, set `manualSorting` to `true`. The default is `false`. ```tsx const defaultData = useMemo(() => createTableData(), []) const [data, setData] = useState(defaultData) return (
{ if (sorting.length) { const { id, desc } = sorting[0]! setData((prev) => prev.toSorted((a, b) => { if (isNumber(a[id]) && isNumber(b[id])) { return desc ? a[id] - b[id] : b[id] - a[id] } else if (isString(a[id]) && isString(b[id])) { return desc ? a[id].localeCompare(b[id]) : b[id].localeCompare(a[id]) } return 0 }), ) } else { setData(defaultData) } console.log(sorting) }} /> ) ``` ### Change Sorting Toggle By default, when sorting is enabled, it starts in ascending order. If you want to start in descending order, set `sortDescFirst` to `true`. If you want to set it only for specific columns, set `sortDescFirst` to `true` for each column in `columns`. The default is `false`. ```tsx const data = useMemo(() => createTableData(), []) return
``` ### Control Sorting ```tsx const [sorting, setSorting] = useState>([ { id: "age", desc: true }, ]) const data = useMemo(() => createTableData(), []) return
``` ### Enable Pagination To enable pagination, set `enablePagination` to `true` and set the component that controls the pagination to `header` or `footer`. The default is `false`. ```tsx const { t } = useI18n("table") const data = useMemo(() => createTableData(40), []) return (
{ const page = table.getState().pagination.pageIndex + 1 const pageSize = table.getState().pagination.pageSize const total = table.getPageCount() return ( table.setPageIndex(page - 1)} /> table.setPageSize(Number(value))} /> ) }} withScrollArea /> ) ``` ### Set Default Page Index and Page Size To set the default page index and page size, set `defaultPagination` to an object with the `pageIndex` and `pageSize`. ```tsx const { t } = useI18n("table") const data = useMemo(() => createTableData(20), []) return (
{ const page = table.getState().pagination.pageIndex + 1 const pageSize = table.getState().pagination.pageSize const total = table.getPageCount() return ( table.setPageIndex(page - 1)} /> table.setPageSize(Number(value))} /> ) }} withScrollArea /> ) ``` ### Use Manual Pagination To use manual pagination, set `manualPagination` to `true`. The default is `false`. ```tsx const { t } = useI18n("table") const [pagination, setPagination] = useState({ pageIndex: 0, pageSize: 20, }) const rowCount = 40 const defaultData = useMemo(() => createTableData(rowCount), []) const data = useMemo( () => defaultData.slice( pagination.pageIndex * pagination.pageSize, (pagination.pageIndex + 1) * pagination.pageSize, ), [defaultData, pagination], ) return (
{ const page = table.getState().pagination.pageIndex + 1 const pageSize = table.getState().pagination.pageSize const total = table.getPageCount() return ( table.setPageIndex(page - 1)} /> table.setPageSize(Number(value))} /> ) }} withScrollArea /> ) ``` ### Control Pagination ```tsx const [pagination, setPagination] = useState({ pageIndex: 0, pageSize: 20, }) const { t } = useI18n("table") const data = useMemo(() => createTableData(40), []) return (
{ const page = table.getState().pagination.pageIndex + 1 const pageSize = table.getState().pagination.pageSize const total = table.getPageCount() return ( table.setPageIndex(page - 1)} /> table.setPageSize(Number(value))} /> ) }} withScrollArea /> ) ``` ### Enable Row Selection To enable row selection, set `enableRowSelection` to `true`. The default is `false`. ```tsx const data = useMemo(() => createTableData(), []) return
``` ### Set Default Selected Rows To set the default selected rows, set `defaultRowSelection` to an object with the index of the `data` as the key. ```tsx const data = useMemo(() => createTableData(), []) return (
) ``` ### Enable Row Selection on Click To enable row selection on click, set `selectOnClickRow` to `true`. The default is `false`. ```tsx const data = useMemo(() => createTableData(), []) return (
) ``` ### Hide Checkbox To hide the checkbox, set `withCheckbox` to `false`. The default is `true`. ```tsx const data = useMemo(() => createTableData(), []) return (
) ``` ### Disable Row To disable a row, set `enableRowSelection` to a function that returns a boolean. ```tsx const data = useMemo(() => createTableData(), []) return (
row.id !== "1"} withScrollArea /> ) ``` ### Control Row Selection ```tsx const [rowSelection, setRowSelection] = useState({}) const data = useMemo(() => createTableData(), []) return (
) ``` ### Use Filter To use a filter, set the component that controls the filter to `header` or `footer`. ```tsx const data = useMemo(() => createTableData(), []) return (
{ const value = (table.getColumn("email")?.getFilterValue() as string | undefined) ?? "" return ( table.getColumn("email")?.setFilterValue(ev.target.value) } /> ) }} withScrollArea /> ) ``` ### Set Default Filter To set the default filter, set `defaultColumnFilters` to an array of objects with the `id` and value of the column to filter. ```tsx const data = useMemo(() => createTableData(), []) return (
{ const value = (table.getColumn("email")?.getFilterValue() as string | undefined) ?? "" return ( table.getColumn("email")?.setFilterValue(ev.target.value) } /> ) }} withScrollArea /> ) ``` ### Control Filter ```tsx const [columnFilters, setColumnFilters] = useState([ { id: "email", value: "" }, ]) const data = useMemo(() => createTableData(), []) return (
{ const value = (table.getColumn("email")?.getFilterValue() as string | undefined) ?? "" return ( table.getColumn("email")?.setFilterValue(ev.target.value) } /> ) }} withScrollArea /> ) ``` ### Enable Column Resizing To enable column resizing, set `enableColumnResizing` to `true`. The default is `false`. ```tsx const data = useMemo(() => createTableData(), []) return (
) ``` ### Change Column Resizing Method By default, the column resizing timing is changed while dragging. If you want to change it when the drag is finished, set `columnResizeMode` to `"onEnd"`. ```tsx const data = useMemo(() => createTableData(), []) return (
) ``` ## Props | Prop | Default | Type | Description | | ---------------------------- | ---------------------------- | ------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------ | | `colorScheme` | - | `"amber" \| "black" \| "blackAlpha" \| "blue" \| "cyan" \| "danger" \| "emerald" \| "error" \| "flashy" \| "fuchsia" ...` | Set color scheme variables. | | `size` | - | `"lg" \| "md" \| "sm"` | The size of the component. | | `variant` | - | `"line" \| "outline"` | The variant of the component. | | `cellProps` | - | `TransformProps>` | The props for the table cell. | | `checkboxProps` | - | `CheckboxProps` | The props for the checkbox. | | `columnFilters` | - | `ColumnFiltersState` | The column filters state of the table. | | `defaultColumnFilters` | - | `ColumnFiltersState` | The default column filters state of the table. | | `defaultPagination` | - | `PaginationState` | The default pagination state of the table. | | `defaultRowSelection` | - | `RowSelectionState` | The default row selection state of the table. | | `defaultSorting` | - | `SortingState` | The default sorting state of the table. | | `enableAutoResizeTableWidth` | `false` | `boolean` | If `true`, the table width will be automatically resized to the content. | | `enableKeyboardNavigation` | `true` | `boolean` | If `true`, the keyboard navigation will be enabled. | | `enablePagination` | `false` | `boolean` | If `true`, the pagination will be enabled. | | `footer` | - | `ReactNodeOrFunction>` | The footer of the table. | | `footerGroupProps` | - | `TransformProps>` | The props for the table footer group. | | `footerProps` | - | `TransformProps>` | The props for the table footer. | | `header` | - | `ReactNodeOrFunction>` | The header of the table. | | `headerCheckboxProps` | - | `TransformProps>` | The props for the table header checkbox. | | `headerGroupProps` | - | `TransformProps>` | The props for the table header group. | | `headerProps` | - | `TransformProps>` | The props for the table header. | | `initialFocusableCell` | `{rowIndex: 0, colIndex: 0}` | `{ colIndex: number; rowIndex: number }` | The initial focusable cell of the table. | | `layout` | - | `"-moz-initial" \| "auto" \| "fixed" \| "inherit" \| "initial" \| "revert-layer" \| "revert" \| "unset" \| AnyString ...` | The CSS `table-layout` property. | | `onColumnFiltersChange` | - | `(columnFilters: ColumnFiltersState) => void` | The callback invoked when the column filters state changes. | | `onPaginationChange` | - | `(pagination: PaginationState) => void` | The callback invoked when the pagination state changes. | | `onRowClick` | - | `(row: Row) => void` | The callback invoked when the row is clicked. | | `onRowDoubleClick` | - | `(row: Row) => void` | The callback invoked when the row is double clicked. | | `onSortingChange` | - | `(sorting: SortingState) => void` | The callback invoked when the sorting state changes. | | `pagination` | - | `PaginationState` | The pagination state of the table. | | `resizableTriggerProps` | - | `TransformProps< | Omit, Header > `| The props for the resizable trigger. | |`rowCheckboxProps`| - |`TransformProps>`| The props for the table row checkbox. | |`rowProps`| - |`TransformProps>`| The props for the table row. | |`rowSelection`| - |`RowSelectionState`| The row selection state of the table. | |`scrollAreaProps`| - |`NativeTableAreaProps`| The props for the scroll area. | |`selectOnClickRow`|`false`|`boolean`| If`true`, the row will be selected when the row is clicked. | | `sorting`| - |`SortingState`| The sorting state of the table. | |`sortingIcon`| - |`ReactNodeOrFunction`| The sorting icon of the table. | |`sortingIconProps`| - |`TransformProps< Omit, Header > `| The props for the sorting icon. | |`tableProps`| - |`NativeTable.RootProps`| The props for the table. | |`tbodyProps`| - |`NativeTable.TbodyProps`| The props for the table tbody. | |`tfootProps`| - |`NativeTable.TfootProps`| The props for the table tfoot. | |`theadProps`| - |`NativeTable.TheadProps`| The props for the table thead. | |`withCheckbox`|`true`|`boolean`| If`true`, the table will have a checkbox column. | | `withFooterCheckbox`|`false`|`boolean`| If`true`, the table will have a checkbox in the footer. | | `withFooterGroups`|`false`|`boolean`| If`true`, the table will have footer groups. | | `withScrollArea`|`false`|`boolean` | Whether to enable the scroll area. | ## Accessibility The `Table` follows the [WAI-ARIA - Table Pattern](https://www.w3.org/WAI/ARIA/apg/patterns/table/) for accessibility. Setting an `aria-label` on the `tableProps` allows it to be read by screen readers. ```tsx const data = useMemo(() => createTableData(), []) return (
) ``` ### Keyboard Navigation | Key | Description | State | | ------------ | ------------------------------------------------------------------------------------------------------ | ------------------------- | | `ArrowLeft` | Moves focus one cell to the left. If focus is on the left-most cell in the row, focus does not move. | - | | `ArrowRight` | Moves focus one cell to the right. If focus is on the right-most cell in the row, focus does not move. | - | | `ArrowUp` | Moves focus one cell Up. If focus is on the top cell in the column, focus does not move. | - | | `ArrowDown` | Moves focus one cell down. If focus is on the bottom cell in the column, focus does not move. | - | | `Home` | Moves focus to the first cell in the row that contains focus. | - | | `End` | Moves focus to the last cell in the row that contains focus. | - | | `PageUp` | Moves to the previous page. | `enablePagination={true}` | | `PageDown` | Moves to the next page. | `enablePagination={true}` | ### ARIA Roles and Attributes | Element | Roles and Attributes | Usage | | --------------------------------- | ---------------------- | ------------------------------------------------------------------------------------------------------------------------------------------ | | `table.ui-native-table__root` | `role="grid"` | Indicates that the element is a grid. | | | `aria-rowcount` | Indicates the total number of rows in the table. | | | `aria-colcount` | Indicates the total number of columns in the table. | | | `aria-multiselectable` | Set to `"true"` if `enableRowSelection` is set. | | `thead.ui-native-table__thead` | `role="rowgroup"` | Indicates that the element is a rowgroup. | | `tr.ui-native-table__tr` | `role="row"` | Indicates that the element is a row. | | | `aria-rowindex` | Indicates which row the element is in the table. | | | `aria-disabled` | When `enableRowSelection` is provided as a function, set `"true"` for rows that return `false`. | | | `aria-selected` | Set to `"true"` if the row is selected, `"false"` if not selected. | | `th.ui-native-table__th` | `role="columnheader"` | Indicates that the element is a columnheader. | | | `aria-rowindex` | Indicates which row the element is in the table. | | | `aria-colindex` | Indicates which column the element is in the table. | | | `aria-sort` | Set to `"ascending"` for ascending order, `"descending"` for descending order, and `"none"` if no order is specified. | | `tbody.ui-native-table__tbody` | `role="rowgroup"` | Indicates that the element is a rowgroup. | | `td.ui-native-table__td` | `role="gridcell"` | Indicates that the element is a gridcell. | | | `aria-colindex` | Indicates which column the element is in the table. | | | `aria-rowindex` | Indicates which row the element is in the table. | | `tfoot.ui-native-table__tfoot` | `role="rowgroup"` | Indicates that the element is a rowgroup. | | `label.ui-checkbox__root > input` | `aria-label` | Set to `"Select all rows"` for the header, and `"Select row"` for body rows. | | `th.ui-native-table__th > button` | `aria-label` | Set to `"Sort ascending"` for ascending order, `"Sort descending"` for descending order, and `"Clear sorting"` when no order is specified. | ## Similar Components - [NativeTable](https://yamada-ui.com/docs/components/native-table.md): `NativeTable` is a component for efficiently organizing and displaying data. - [Stat](https://yamada-ui.com/docs/components/stat.md): `Stat` is used to display numbers or data within a box. - [AreaChart](https://yamada-ui.com/docs/components/area-chart.md): `AreaChart` is a component for drawing area charts to compare multiple sets of data. - [Badge](https://yamada-ui.com/docs/components/badge.md): `Badge` is a component that emphasizes the status of an item to make it immediately recognizable. - [BarChart](https://yamada-ui.com/docs/components/bar-chart.md): `BarChart` is a component for drawing bar charts to compare multiple sets of data. - [Bubble](https://yamada-ui.com/docs/components/bubble.md): `Bubble` is a component that displays conversational content in a message bubble. - [Card](https://yamada-ui.com/docs/components/card.md): `Card` is a component that groups and displays related information. By default, it renders a `article` element. - [ComposedChart](https://yamada-ui.com/docs/components/composed-chart.md): `ComposedChart` is a component for drawing composed charts to compare multiple sets of data. ## Uses Components & Hooks - [Checkbox](https://yamada-ui.com/docs/components/checkbox.md): `Checkbox` is a component used for allowing users to select multiple values from multiple options. - [Icon](https://yamada-ui.com/docs/components/icon.md): `Icon` is a general icon component that can be used in your projects. - [NativeTable](https://yamada-ui.com/docs/components/native-table.md): `NativeTable` is a component for efficiently organizing and displaying data.