fleet/frontend/components/TableContainer/DataTable/TextCell/TextCell.tsx
jacobshandling 0b45afcaa8
UI - update empty styles in 5 places (#20079)
## Addresses #19557 
<img width="143" alt="Screenshot 2024-06-27 at 1 44 12 PM"
src="https://github.com/fleetdm/fleet/assets/61553566/e9ad83b2-dda4-4972-b8c2-412389e45823">
<img width="177" alt="Screenshot 2024-06-27 at 1 44 25 PM"
src="https://github.com/fleetdm/fleet/assets/61553566/bb5f808e-0d5a-47cc-84ed-f55797caebfe">
<img width="210" alt="Screenshot 2024-06-27 at 1 49 47 PM"
src="https://github.com/fleetdm/fleet/assets/61553566/2dff046b-6cfa-45fa-8a0c-93ab46e2c8a9">
<img width="194" alt="Screenshot 2024-06-27 at 1 56 56 PM"
src="https://github.com/fleetdm/fleet/assets/61553566/fdaaeac8-e944-427b-917e-87b98ae763b9">
<img width="238" alt="Screenshot 2024-06-27 at 4 39 22 PM"
src="https://github.com/fleetdm/fleet/assets/61553566/bb774af5-07c0-44b6-994f-dc787c5bf606">

- [x] Changes file added for user-visible changes in `changes/`,
- [x] Manual QA for all new/changed functionality

---------

Co-authored-by: Jacob Shandling <jacob@fleetdm.com>
2024-07-03 09:40:03 -07:00

80 lines
2.1 KiB
TypeScript

import classnames from "classnames";
import { uniqueId } from "lodash";
import React from "react";
import ReactTooltip from "react-tooltip";
import { COLORS } from "styles/var/colors";
import { DEFAULT_EMPTY_CELL_VALUE } from "utilities/constants";
const baseClass = "text-cell";
interface ITextCellProps {
value?: string | number | boolean | { timeString: string } | null;
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) {
const tooltipId = uniqueId();
return (
<>
<span data-tip data-for={tooltipId}>
{DEFAULT_EMPTY_CELL_VALUE}
</span>
<ReactTooltip
place="top"
effect="solid"
backgroundColor={COLORS["tooltip-bg"]}
id={tooltipId}
>
{emptyCellTooltipText}
</ReactTooltip>
</>
);
}
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;