File size: 4,366 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
/* eslint-disable no-unused-vars */
import * as React from "react";
import PropTypes from "prop-types";
import TableCell from "@mui/material/TableCell";
import CircularProgress from "@mui/material/CircularProgress";
import { withTheme } from "@mui/styles";
import { MTable } from "..";
/* eslint-enable no-unused-vars */
class MTableEditCell extends React.Component {
constructor(props) {
super(props);
this.state = {
isLoading: false,
value: this.props.rowData[this.props.columnDef.field],
};
}
getStyle = () => {
let cellStyle = {
boxShadow: "2px 0px 15px rgba(125,147,178,.25)",
color: "inherit",
width: this.props.columnDef.tableData.width,
boxSizing: "border-box",
fontSize: "inherit",
fontFamily: "inherit",
fontWeight: "inherit",
padding: "0 16px",
};
if (typeof this.props.columnDef.cellStyle === "function") {
cellStyle = {
...cellStyle,
...this.props.columnDef.cellStyle(this.state.value, this.props.rowData),
};
} else {
cellStyle = { ...cellStyle, ...this.props.columnDef.cellStyle };
}
if (typeof this.props.cellEditable.cellStyle === "function") {
cellStyle = {
...cellStyle,
...this.props.cellEditable.cellStyle(
this.state.value,
this.props.rowData,
this.props.columnDef
),
};
} else {
cellStyle = { ...cellStyle, ...this.props.cellEditable.cellStyle };
}
return cellStyle;
};
handleKeyDown = (e) => {
if (e.keyCode === 13) {
this.onApprove();
} else if (e.keyCode === 27) {
this.onCancel();
}
};
onApprove = () => {
this.setState({ isLoading: true }, () => {
this.props.cellEditable
.onCellEditApproved(
this.state.value, // newValue
this.props.rowData[this.props.columnDef.field], // oldValue
this.props.rowData, // rowData with old value
this.props.columnDef // columnDef
)
.then(() => {
this.setState({ isLoading: false });
this.props.onCellEditFinished(
this.props.rowData,
this.props.columnDef
);
})
.catch((error) => {
this.setState({ isLoading: false });
});
});
};
onCancel = () => {
this.props.onCellEditFinished(this.props.rowData, this.props.columnDef);
};
renderActions() {
if (this.state.isLoading) {
return (
<div style={{ display: "flex", justifyContent: "center", width: 60 }}>
<CircularProgress size={20} />
</div>
);
}
const actions = [
{
icon: this.props.icons.Check,
tooltip: this.props.localization.saveTooltip,
onClick: this.onApprove,
disabled: this.state.isLoading,
},
{
icon: this.props.icons.Clear,
tooltip: this.props.localization.cancelTooltip,
onClick: this.onCancel,
disabled: this.state.isLoading,
},
];
return (
<this.props.components.Actions
actions={actions}
components={this.props.components}
size="small"
/>
);
}
render() {
return (
<TableCell size={this.props.size} style={this.getStyle()} padding="none">
<div style={{ display: "flex", alignItems: "center" }}>
<div style={{ flex: 1, marginRight: 4 }}>
<this.props.components.EditField
columnDef={this.props.columnDef}
value={this.state.value}
onChange={(value) => this.setState({ value })}
onKeyDown={this.handleKeyDown}
disabled={this.state.isLoading}
autoFocus
/>
</div>
{this.renderActions()}
</div>
</TableCell>
);
}
}
MTableEditCell.defaultProps = {
columnDef: {},
};
MTableEditCell.propTypes = {
cellEditable: PropTypes.object.isRequired,
columnDef: PropTypes.object.isRequired,
components: PropTypes.object.isRequired,
errorState: PropTypes.oneOfType([PropTypes.object, PropTypes.bool]),
icons: PropTypes.object.isRequired,
localization: PropTypes.object.isRequired,
onCellEditFinished: PropTypes.func.isRequired,
rowData: PropTypes.object.isRequired,
size: PropTypes.string,
};
export default withTheme(MTableEditCell);
|