There is no way to add `iconProps` to Edit/Delete when editable.onRowUpdate or editable.onRowDelete callbacks provided.
Guidelines
Describe the bug
I want to be able to add custom iconProps like [data-testid="users-list-item-${index}-edit"] or [data-testid="users-list-item-${index}-delete] (selectors for Cypress) props to Edit/Delete icon when I provide onRowEdit or onRowDelete callbacks on editable object.
I can't add Edit action through actions array because it'll just add duplicate edit action along with default actions added through getProps function. I looked at the source code and found that in getProps it's adding action with all other props but iconProps.
I also looked at the array of tableIcons where we define all icons to be used; props object is empty and I don't get index of the table row or any other prop which I can use to customise data-testid attribute.
Additional context
We require custom data-testid attribute to target button in order to write complete e2e tests using Cypress.
Even if I override Action component using MTableAction to add iconProps, it doesn't render data-testid attribute.
components={{
Action: props => (
<MTableAction
{...props}
action={rowData => ({ ...props.action(rowData), iconProps: { 'data-testid': 'test-id' } })}
/>
),
}}
Hi @asaivishal, not sure if you managed to solve this, but I've come across a similar issue myself. I had a look into the material-table code base and I found this line which shows how an icon for an action is rendered. It appears that the iconProps are only added to the component if the action.icon property is either a string or a function, however, if it's a React component then these props will not be added. If you look at the function that renders the icon for the Edit action, you can see that an object that resolves to a React component is returned - this explains why your Cypress props aren't being added. A work around to this is by overriding the action.icon property directly. This would make your code look somewhat like the following:
components={{
Action: props => (
<MTableAction
{...props}
action={{ ...props.action(props.data), icon: () => <EditIcon data-testid="test-id" /> }}
/>
),
}}
Note that the EditIcon would just be the Material UI Edit icon:
import EditIcon from '@material-ui/icons/Edit';
Hi @ollyrowe thanks for your comments and explanation. I have not yet managed to fix this issue but I'll try your suggestion and see if I can resolve it.
@ollyrowe I managed to fix above using below code snippet:
components: {{
Action: props => {
const { icon: Icon, tooltip } = props.action(props.data);
return (
<MTableAction
{...props}
action={{
...props.action(props.data),
icon: () => (
<Icon
data-testid={`list-item-${
props.data.tableData.id
}-${tooltip.toLowerCase()}`}
/>
),
}}
/>
);
},
}}
Though I strongly believe that there should be an easy way to add custom props to action icons provided through editable object.
Your above code would render the same EditIcon for both Edit and Delete actions.
@ollyrowe thanks for the pointer in the right direction 👍
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. You can reopen it if it required.