material-table icon indicating copy to clipboard operation
material-table copied to clipboard

How to make custom button and cellEditable co-exist?

Open RoosterH opened this issue 5 years ago • 3 comments

  • Please include a demo of the issue/behavior/question you have

I originally had a custom button component showing as the following: Screen Shot 2020-12-17 at 9 10 38 PM

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. Screen Shot 2020-12-17 at 9 18 02 PM

  • 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);
			});
		}
	}}

RoosterH avatar Dec 18 '20 05:12 RoosterH

Were you able to find the solution for it ?

AbdurRehman26 avatar Dec 20 '20 20:12 AbdurRehman26

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.

RoosterH avatar Dec 20 '20 22:12 RoosterH

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>
			);
		}
	},
}}

RoosterH avatar Jan 04 '21 05:01 RoosterH