File size: 6,490 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 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 |
/* eslint-disable no-unused-vars */
import * as React from "react";
import TableCell from "@mui/material/TableCell";
import PropTypes from "prop-types";
import parseISO from "date-fns/parseISO";
import * as CommonValues from "../utils/common-values";
/* eslint-enable no-unused-vars */
/* eslint-disable no-useless-escape */
const isoDateRegex = /^\d{4}-(0[1-9]|1[0-2])-([12]\d|0[1-9]|3[01])([T\s](([01]\d|2[0-3])\:[0-5]\d|24\:00)(\:[0-5]\d([\.,]\d+)?)?([zZ]|([\+-])([01]\d|2[0-3])\:?([0-5]\d)?)?)?$/;
/* eslint-enable no-useless-escape */
export default class MTableCell extends React.Component {
getRenderValue() {
const dateLocale =
this.props.columnDef.dateSetting &&
this.props.columnDef.dateSetting.locale
? this.props.columnDef.dateSetting.locale
: undefined;
if (
this.props.columnDef.emptyValue !== undefined &&
(this.props.value === undefined || this.props.value === null)
) {
return this.getEmptyValue(this.props.columnDef.emptyValue);
}
if (this.props.columnDef.render) {
if (this.props.rowData) {
return this.props.columnDef.render(this.props.rowData, "row");
} else if (this.props.value) {
return this.props.columnDef.render(this.props.value, "group");
}
} else if (this.props.columnDef.type === "boolean") {
const style = { textAlign: "left", verticalAlign: "middle", width: 48 };
if (this.props.value) {
return <this.props.icons.Check style={style} />;
} else {
return <this.props.icons.ThirdStateCheck style={style} />;
}
} else if (this.props.columnDef.type === "date") {
if (this.props.value instanceof Date) {
return this.props.value.toLocaleDateString(dateLocale);
} else if (isoDateRegex.exec(this.props.value)) {
return parseISO(this.props.value).toLocaleDateString(dateLocale);
} else {
return this.props.value;
}
} else if (this.props.columnDef.type === "time") {
if (this.props.value instanceof Date) {
return this.props.value.toLocaleTimeString();
} else if (isoDateRegex.exec(this.props.value)) {
return parseISO(this.props.value).toLocaleTimeString(dateLocale);
} else {
return this.props.value;
}
} else if (this.props.columnDef.type === "datetime") {
if (this.props.value instanceof Date) {
return this.props.value.toLocaleString();
} else if (isoDateRegex.exec(this.props.value)) {
return parseISO(this.props.value).toLocaleString(dateLocale);
} else {
return this.props.value;
}
} else if (this.props.columnDef.type === "currency") {
return this.getCurrencyValue(
this.props.columnDef.currencySetting,
this.props.value
);
} else if (typeof this.props.value === "boolean") {
// To avoid forwardref boolean children.
return this.props.value.toString();
}
return this.props.value;
}
getEmptyValue(emptyValue) {
if (typeof emptyValue === "function") {
return this.props.columnDef.emptyValue(this.props.rowData);
} else {
return emptyValue;
}
}
getCurrencyValue(currencySetting, value) {
if (currencySetting !== undefined) {
return new Intl.NumberFormat(
currencySetting.locale !== undefined ? currencySetting.locale : "en-US",
{
style: "currency",
currency:
currencySetting.currencyCode !== undefined
? currencySetting.currencyCode
: "USD",
minimumFractionDigits:
currencySetting.minimumFractionDigits !== undefined
? currencySetting.minimumFractionDigits
: 2,
maximumFractionDigits:
currencySetting.maximumFractionDigits !== undefined
? currencySetting.maximumFractionDigits
: 2,
}
).format(value !== undefined ? value : 0);
} else {
return new Intl.NumberFormat("en-US", {
style: "currency",
currency: "USD",
}).format(value !== undefined ? value : 0);
}
}
handleClickCell = (e) => {
if (this.props.columnDef.disableClick) {
e.stopPropagation();
}
};
getStyle = () => {
const width = CommonValues.reducePercentsInCalc(
this.props.columnDef.tableData.width,
this.props.scrollWidth
);
let cellStyle = {
color: "inherit",
width,
maxWidth: this.props.columnDef.maxWidth,
minWidth: this.props.columnDef.minWidth,
boxSizing: "border-box",
fontSize: "inherit",
fontFamily: "inherit",
fontWeight: "inherit",
};
if (typeof this.props.columnDef.cellStyle === "function") {
cellStyle = {
...cellStyle,
...this.props.columnDef.cellStyle(this.props.value, this.props.rowData),
};
} else {
cellStyle = { ...cellStyle, ...this.props.columnDef.cellStyle };
}
if (this.props.columnDef.disableClick) {
cellStyle.cursor = "default";
}
return { ...this.props.style, ...cellStyle };
};
render() {
const {
icons,
columnDef,
rowData,
errorState,
cellEditable,
onCellEditStarted,
scrollWidth,
...cellProps
} = this.props;
const cellAlignment =
columnDef.align !== undefined
? columnDef.align
: ["numeric", "currency"].indexOf(this.props.columnDef.type) !== -1
? "right"
: "left";
let renderValue = this.getRenderValue();
if (cellEditable) {
renderValue = (
<div
style={{
borderBottom: "1px dashed grey",
cursor: "pointer",
width: "max-content",
}}
onClick={(e) => {
e.stopPropagation();
onCellEditStarted(this.props.rowData, this.props.columnDef);
}}
>
{renderValue}
</div>
);
}
return (
<TableCell
size={this.props.size}
{...cellProps}
style={this.getStyle()}
align={cellAlignment}
onClick={this.handleClickCell}
>
{this.props.children}
{renderValue}
</TableCell>
);
}
}
MTableCell.defaultProps = {
columnDef: {},
value: undefined,
};
MTableCell.propTypes = {
columnDef: PropTypes.object.isRequired,
value: PropTypes.any,
rowData: PropTypes.object,
errorState: PropTypes.oneOfType([PropTypes.object, PropTypes.bool]),
};
|