material-table
material-table copied to clipboard
How to make custom button and cellEditable co-exist?
-
Please include a demo of the issue/behavior/question you have
I originally had a custom button component showing as the following:

After adding an EditableCell, table got screwed up. Check and Cross icons disappeared. It seems my original code is not correct EditableCell is getting properties from components.

-
Please try to be as detailed as possible
<MaterialTable
// data={entryList}
data={data}
title={`${eventName} Entry List`}
isLoading={showLoading}
style={{
border: '2px solid gray',
maxWidth: '1450px',
marginTop: '10px',
marginLeft: '20px'
}}
.....
.....
components={{
Action: props => (
<Button
onClick={event => {
// return email back to parent to send request to backend
getEmail(props.data.email);
getPaymentStatus(props.data.paymentStatus);
props.action.onClick(event, props.data);
}}
size={getButtonClassName(props.data.paymentStatus)}
disabled={props.data.paymentStatus === 'Paid'}>
{getButtonText(props.data.paymentStatus)}
</Button>
),
OverlayLoading: props => (
<div className="center">
<LoadingSpinner />
</div>
)
}}
actions={[
{
icon: 'Charge',
tooltip: 'Charge User',
onClick: (event, rowData) => {
setTimeout(() => {
// need to set timeout to have the table load the new value
// console.log('rowData = ', rowData);
}, 2000);
}
}
]}
cellEditable={{
onCellEditApproved: (
newValue,
oldValue,
rowData,
columnDef
) => {
return new Promise((resolve, reject) => {
// rawData is the old data
setTimeout(() => {
console.log('rowData = ', rowData);
rowData.refundFee = newValue;
const dataUpdate = [...data];
const index = rowData.tableData.id;
console.log('index = ', index);
dataUpdate[index] = rowData;
console.log('dataUpdate new = ', dataUpdate);
setData([...dataUpdate]);
resolve();
}, 1000);
});
}
}}
-
You may fork one of the following starter templates if you would like:
Were you able to find the solution for it ?
Not yet. I think I may need to define 2 components, one for button and one for editableCell, but I do not know how to make them yet.
Here is the solution that I use to combine an Action Overriding with cellEditable: cellEditable:
columns={[
{
title: 'Refund Fee',
field: 'refundFee',
filtering: false,
type: 'string',
editable: 'onUpdate'
}
...
]}
cellEditable={{
onCellEditApproved: (
newValue,
oldValue,
rowData,
columnDef
) => {
return new Promise((resolve, reject) => {
if (rowData.paymentStatus !== 'Paid') {
// for non-Paid status, don't allow to change refundFee
resolve();
} else {
// rowData is the old data with old refundFee
setTimeout(() => {
rowData.refundFee = newValue;
updateRefundFee(rowData);
const dataUpdate = [...data];
const index = rowData.tableData.id;
dataUpdate[index] = rowData;
setData([...dataUpdate]);
resolve();
}, 1000);
}
});
}
}}
define Action Overriding:
actions={[
{
icon: 'charge',
tooltip: 'Charge',
onClick: (event, rowData) => {
setTimeout(() => {
// need to set timeout to have the table load the new value
}, 2000);
}
}
]}
components={{
Action: props => {
// this is where the trick is to preserve "Save" and "Cancel" action rending for cellEditable
if (
typeof props.action === typeof Function ||
props.action.tooltip !== 'Charge'
) {
return <MTableAction {...props} />;
} else {
return (
<Button
onClick={event => {
// return email back to parent to send request to backend
getEmailRefundFee(
props.data.email,
props.data.refundFee
);
props.action.onClick(event, props.data);
}}
size={getButtonClassName(
props.data.paymentStatus
)}
disabled={props.data.paymentStatus !== 'Paid'}>
{getButtonText(props.data.paymentStatus)}
</Button>
);
}
},
}}