Multiple action button propagations within table rows?? #4349
-
Im trying to find out the best way to add a few buttons to the end of each row of a table. its clear that a 'row' action is unsuitable as there is, lets say '3' possible actions. My first impression is to add a new column for each button so each button has its own 'cell action' and via the mapping of items to columns in the table implementation i can concatenate the item id with the specific column name so if i have a list of products, in the onCellAction i could search for something along the lines of Heres a full snippet of a mock storybook implementation im currently working with. a few cells needed custom styling, hence the switch statement on the column key. export const Table: StoryObj<typeof TableComponent> = {
args: {
"aria-label": "System Faults Table",
selectionMode: "multiple",
selectionBehavior: "toggle",
},
render: (args) => {
return (
<div className="flex w-full justify-center">
<div className="w-full max-w-7xl">
<TableComponent
{...args}
onCellAction={(key) => console.log(`${key} has been triggered`)}
>
<TableHeader>
<Column key="siteName">Site Name</Column>
<Column key="severityName">Severity</Column>
<Column key="faultTypeName">Type</Column>
<Column key="triggerTime">Trigger Time</Column>
<Column key="statusName">Status</Column>
<Column key="description">Description</Column>
<Column key="action1">Action 1</Column>
<Column key="action2">Action 2</Column>
</TableHeader>
<TableBody items={faultsData}>
{(item) => {
return (
<Row key={item.id}>
{(columnKey) => {
const data = item[columnKey as FaultsTableKey];
const cellKey = `${item.id}-${columnKey}`;
switch (columnKey) {
case "siteName":
return (
<Cell key={cellKey}>
<p className="font-bold">{data}</p>
</Cell>
);
case "action1":
// onCellAction = [fault-id]-action1 has been triggered
return (
<Cell key={cellKey}>
<button>Do Something?</button>
</Cell>
);
case "action2":
// onCellAction = [fault-id]-action2 has been triggered
return (
<Cell key={cellKey}>
<button>Do Something Else?</button>
</Cell>
);
default:
return <Cell key={cellKey}>{data}</Cell>;
}
}}
</Row>
);
}}
</TableBody>
</TableComponent>
</div>
</div>
);
},
}; |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 3 replies
-
You can add multiple buttons to a single column. This is actually what we recommend doing, you can navigate a cell with the arrow keys, here's an example based on our Spectrum table https://codesandbox.io/s/condescending-aj-yn3hs6?file=/src/App.js If you can perform it on many rows at once, then we recommend putting that outside the table, as a bulk action. See our ActionBar as an example of one way to accomplish this https://react-spectrum.adobe.com/react-spectrum/ActionBar.html, you could also have a more permanent set of bulk actions that are always visible. |
Beta Was this translation helpful? Give feedback.
-
@snowystinger you good sir, are a gentleman and a scholar, ive been rackin my brain and trawling through accessibility docs tryin to figure out the best approach, i think where that is in the docs flew right over my head. Where i was confused is that i was under the impression that
it would be awesome if a more robust table example using cell actions was provided in the react-aria table page. that aside, cheers again. big help |
Beta Was this translation helpful? Give feedback.
You can add multiple buttons to a single column. This is actually what we recommend doing, you can navigate a cell with the arrow keys, here's an example based on our Spectrum table https://codesandbox.io/s/condescending-aj-yn3hs6?file=/src/App.js
If you can perform it on many rows at once, then we recommend putting that outside the table, as a bulk action. See our ActionBar as an example of one way to accomplish this https://react-spectrum.adobe.com/react-spectrum/ActionBar.html, you could also have a more permanent set of bulk actions that are always visible.