Guides
Row Actions Guide
How to add row action buttons to each row in Shadcn Table
The row actions feature is simply a pre-built Display Column feature that adds a column as a place to store either a row action menu or other row action buttons.
Example
Using the built-in Row Actions column is optional, as you can simply create your own display columns, but this feature has some built-in conveniences that make it easy to add row actions to your table.
Relevant Table Options
| Prop Name | Type | Description |
|---|---|---|
| displayColumnDefOptions | { [key: string]: ST_DisplayColumnDef<TData> } | Customize and override the column definition options for the built-in display columns. (Select, Expand, Row Actions, etc.) |
| enableRowActions | boolean | |
| positionActionsColumn | 'first' | 'last' | Where the row actions column sits. By default it is the last column, pinned to the end. Set 'first' to put it before the data columns. |
| renderRowActionMenuItems | ({ closeMenu, row, table }) => ReactNode[] | |
| renderRowActions | ({ cell, row, table }) => ReactNode |
Enable Row Actions
You can enable the row actions feature by setting the enableRowActions table option to true.
You can then add your row action components with either the renderRowActions or renderRowActionMenuItems props.
Add Row Actions Menu Items
If you want to embed all of your row actions into a single menu, you can use the renderRowActionMenuItems table option.
const table = useShadcnTable({
columns,
data,
enableRowActions: true,
renderRowActionMenuItems: ({ row }) => [
<MenuItem key="edit" onClick={() => console.info('Edit')}>
Edit
</MenuItem>,
<MenuItem key="delete" onClick={() => console.info('Delete')}>
Delete
</MenuItem>,
],
});
return <ShadcnTable table={table} />;Add Row Action Buttons
If you want to add row action buttons, you can use the renderRowActions table option.
const table = useShadcnTable({
columns,
data,
enableRowActions: true,
renderRowActions: ({ row }) => (
<span>
<IconButton onClick={() => console.info('Edit')}>
<EditIcon />
</IconButton>
<IconButton onClick={() => console.info('Delete')}>
<DeleteIcon />
</IconButton>
</span>
),
});
return <ShadcnTable table={table} />;Customize Row Actions Column
Change Row Actions Display Column Options
By default the row actions column is the last column, 48 px wide, pinned to the end so it stays visible while the table scrolls sideways. Its trigger is a horizontal dots button, and its header label ("Actions") is visually hidden and read by screen readers only.
You can customize all column def options for the row actions column, including the column width, header text, and more using the displayColumnDefOptions table option.
const table = useShadcnTable({
columns,
data,
enableRowActions: true,
displayColumnDefOptions: {
'st-row-actions': {
header: 'Change Account Settings', //the column name the menus show
Header: 'Change Account Settings', //visible header text (by default the label is for screen readers only)
size: 300, //make actions column wider
},
},
renderRowActions: ({ table }) => (
<span>
<Button>Button 1</Button>
<Button>Button 2</Button>
<Button>Button 3</Button>
</span>
),
});Position Row Actions Column
You can position the row actions column at the start or end (first or last column) of the table using the positionActionsColumn table option. The default is 'last', with the column pinned to the end. To put it back before the data columns, set positionActionsColumn: 'first'; the column is then not pinned.
const table = useShadcnTable({
columns,
data,
enableRowActions: true,
positionActionsColumn: 'first',
renderRowActions: ({ table }) => (
//...
),
})The end pinning is the table's default columnPinning while you give none of your own. If you set columnPinning in initialState or state, add 'st-row-actions' to its end array to keep the column pinned, or leave it out to let it scroll with the rest. With enableColumnPinning: false it is not pinned either.
const table = useShadcnTable({
columns,
data,
enableRowActions: true,
initialState: {
columnPinning: { start: ['name'], end: ['st-row-actions'] },
},
});View Extra Storybook Examples