-
Notifications
You must be signed in to change notification settings - Fork 843
Expand file tree
/
Copy pathTextCell.tsx
More file actions
73 lines (63 loc) · 1.9 KB
/
TextCell.tsx
File metadata and controls
73 lines (63 loc) · 1.9 KB
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
import classnames from "classnames";
import React from "react";
import TooltipWrapper from "components/TooltipWrapper";
import { DEFAULT_EMPTY_CELL_VALUE } from "utilities/constants";
const baseClass = "text-cell";
interface ITextCellProps {
value?: React.ReactNode | { timeString: string };
// eslint-disable-next-line @typescript-eslint/no-explicit-any
formatter?: (val: any) => React.ReactNode; // string, number, or null
grey?: boolean;
italic?: boolean;
className?: string;
emptyCellTooltipText?: React.ReactNode;
}
const TextCell = ({
value,
formatter = (val) => val, // identity function if no formatter is provided
grey = false,
italic = false,
className = "w250",
emptyCellTooltipText,
}: ITextCellProps) => {
let val = value;
// we want to render booleans as strings.
if (typeof value === "boolean") {
val = value.toString();
}
const formattedValue = formatter(val);
// Check if the given value is empty or if the formatted value is empty.
// 'empty' is defined as null, undefined, or an empty string.
const isEmptyValue =
value === null ||
value === undefined ||
value === "" ||
formattedValue === null ||
formattedValue === undefined ||
formattedValue === "";
if (isEmptyValue) {
[grey, italic] = [true, true];
}
const renderEmptyCell = () => {
if (emptyCellTooltipText) {
return (
<TooltipWrapper
tipContent={emptyCellTooltipText}
position="top"
underline={false}
showArrow
>
<span>{DEFAULT_EMPTY_CELL_VALUE}</span>
</TooltipWrapper>
);
}
return DEFAULT_EMPTY_CELL_VALUE;
};
const cellText = isEmptyValue ? renderEmptyCell() : formattedValue;
const cellClasses = classnames(baseClass, className, {
"grey-cell": grey,
"italic-cell": italic,
});
return <span className={cellClasses}>{cellText}</span>;
};
export default TextCell;