Allow overriding individual actions
Hi! Thanks for this library, it has been very useful for me.
I think it would be nice to wrap every action instead of the list of actions. Currently only the latter is possible via components.actions. The problem with that is that I would have to rewrite the logic that handles the configuration passed via the actions prop, since it is written as method from MTableActions.
Given that, one alternative solution for my problem would be to subclass the MTableActions class and wrap renderButton() with my wrapper component. However, (i) renderButton() does not seem to be public (i.e. not documented on the official docs) and (ii) composing is preferred over inheritance in React.
Some context:
I'm working on a project that uses Apollo Graphql and I want every action to be enclosed by a <Mutation> element, so that I can trigger an action for the correct record (row) and easily show the operation status by changing the action icon without having to handle state myself.
For my specific case, if the action object supported a property named wrapper so that users could define a wrapper for that single action, that would be enough. Example:
<MaterialTable
...
actions={[
{
tooltip: 'Delete record',
wrapper: MyWrapper,
]}
>
</MaterialTable>
And MyWrapper would be a component like the one below:
const MyWrapper = ({action, renderAction}) => <Mutation
mutation={MY_GRAPHQL_MUTATION}
>{(mutate, result) => {
/* Here I have the opportunity to define specific action configuration */
const icon = result.loading ? OperationInProgressIcon : DeleteIcon;
return renderAction({...action, onClick: mutate, icon: icon});
}}</Mutation>
It could be wrapper or even render. What do you think?
A possible implementation for the above would be what is done in my temporary workaround:
class CustomMTableActions extends MTableActions {
render() {
if (this.props.actions) {
return this.props.actions.map((action, index) => {
if (action.wrapper) {
return <action.wrapper
key={`action-wrapper-${index}`}
action={action}
data={this.props.data}
renderAction={action => this.renderButton(action, index)}
/>
} else {
return this.renderButton(action, index)
}
});
}
return null;
}
}
Edit:
Added data={this.props.data}, this was necessary for my Mutation element.
Hi @guludo ,
This is a great idea. This feature will make material-table more flexible. I will add this feature to next release.
Thanks.
Hi @guludo ,
I think on this feature and this is not necessary for material-table. You can add a custom rendered column to render your own mutations.
Look at examples: https://mbrn.github.io/material-table/#/docz-examples-07-example-custom-render
Hi @mbrn ,
But, with that, wouldn't I miss out on any nice (and also future) features that actions could give me?
ok. i will think again.
Action overriding feature added with 1.26.0
Example:
<MaterialTable
components={{
Action: props => <Button onClick={() => props.onClick()}>My Button</Button>
}}
..............
/>
Thank you!
Action overriding feature allows me to override button but it overrides all the actions buttons. For example, if I have two action buttons for edit and delete and I use the Action overriding, both of my button gets overridden with that custom code. How can I specify different codes for different buttons?
const components = {
Action: props => (
<IconButton aria-label="delete" size="small"
disabled={props.data.email === '[email protected]'}
onClick={(event) => props.action.onClick(event, props.data)}
>
<Icon>delete</Icon>
</IconButton>
)
};
const actions = [
{
icon: 'edit',
tooltip: 'Edit Index',
onClick: (event, rowData) => {
//history.push('/dashboard/index_relation/' + rowData._id + "/edit");
this.onEditClick(null, rowData._id);
}
},
{
icon: 'delete',
tooltip: 'Delete Index',
onClick: (event, rowData) => {
this.onDeleteClick(null, rowData._id);
}
},
];
Action overriding feature allows me to override button but it overrides all the actions buttons. For example, if I have two action buttons for edit and delete and I use the Action overriding, both of my button gets overridden with that custom code. How can I specify different codes for different buttons?
const components = { Action: props => ( <IconButton aria-label="delete" size="small" disabled={props.data.email === '[email protected]'} onClick={(event) => props.action.onClick(event, props.data)} > <Icon>delete</Icon> </IconButton> ) }; const actions = [ { icon: 'edit', tooltip: 'Edit Index', onClick: (event, rowData) => { //history.push('/dashboard/index_relation/' + rowData._id + "/edit"); this.onEditClick(null, rowData._id); } }, { icon: 'delete', tooltip: 'Delete Index', onClick: (event, rowData) => { this.onDeleteClick(null, rowData._id); } }, ];
I'm trying to figure out the same thing...
Action overriding feature allows me to override button but it overrides all the actions buttons. For example, if I have two action buttons for edit and delete and I use the Action overriding, both of my button gets overridden with that custom code. How can I specify different codes for different buttons?
const components = { Action: props => ( <IconButton aria-label="delete" size="small" disabled={props.data.email === '[email protected]'} onClick={(event) => props.action.onClick(event, props.data)} > <Icon>delete</Icon> </IconButton> ) }; const actions = [ { icon: 'edit', tooltip: 'Edit Index', onClick: (event, rowData) => { //history.push('/dashboard/index_relation/' + rowData._id + "/edit"); this.onEditClick(null, rowData._id); } }, { icon: 'delete', tooltip: 'Delete Index', onClick: (event, rowData) => { this.onDeleteClick(null, rowData._id); } }, ];I'm trying to figure out the same thing...
Here is the solution that worked for me. []https://stackoverflow.com/questions/57777516/react-material-table-action-button-markup-overriding-for-multiple-actions/57778218#57778218
Is the solution that you provided @zihad0292 the accepted solution to fix this issue?
Is the solution that you provided @zihad0292 the accepted solution to fix this issue?
@sceptre12 I think there are many ways to solve a problem and you can choose any one of them. In my case, that worked and I moved on with it. Thanks
@mbrn I'm using a custom component with styling to create a button group which I want to display in the actions column. It's really strange to have to add
actions={[{
icon: '',
tooltip: '',
onClick: () => {}
}]
components={{
Action: props => (<CustomRowButtons rowId={props.data.id}/>)
}}
to be able to achieve this, when I am essentially just doing the equivalent of
actions={[{
render: rowData => (<CustomRowButtons rowId={props.data.id}/>)
}]}
It also means that if I want to add a separate custom component to be used as a free action (like add record button) then I have to do some workaround like using the icon property as an id, then checking it in a case switch/series of if statements in the Action override.
It seems that overriding make sense when you want to extend the functionality, but we need a way to completely replace the default style of icons and tooltips and so on. Even in the example here: https://material-table.com/#/docs/features/component-overriding we can see that the tooltip 'Save User' is never being rendered or used because it has been completely replaced by a custom button.
You could render the icon action to be anything like below:
icon: () => (
<Button onClick={(event, rowData) => alert(rowData)} variant="contained" color="primary">
Action
</Button>
)
You could render the icon action to be anything like below:
icon: () => ( <Button onClick={(event, rowData) => alert(rowData)} variant="contained" color="primary"> Action </Button> )
But the problem is that there is a weird hover effect.