fleet/frontend/components/TableContainer/DataTable/TextCell/TextCell.tsx
Gabriel Hernandez 917e83e2ff
change TextCell so that we are rendering '0' value as a number and re… (#19441)
relates to #19433

Makes the rendering of empty text cell values consistent. We also want
to render the '0' value as a number instead of the default value `---`
with greyed styles.

**Before:**


![image](https://github.com/fleetdm/fleet/assets/1153709/7c0ecb99-409d-4698-bb6f-083245fb3919)

**After:**


![image](https://github.com/fleetdm/fleet/assets/1153709/d7da74a7-3492-4672-98ea-f810dc0038d7)

- [x] Changes file added for user-visible changes in `changes/`,
`orbit/changes/` or `ee/fleetd-chrome/changes`.
- [x] Added/updated tests
- [x] Manual QA for all new/changed functionality

---------

Co-authored-by: Sarah Gillespie <73313222+gillespi314@users.noreply.github.com>
2024-06-11 12:54:24 +01:00

80 lines
2 KiB
TypeScript

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";
interface ITextCellProps {
value?: string | number | boolean | { timeString: string } | null;
formatter?: (val: any) => React.ReactNode; // string, number, or null
/** adds a greyed styling to the cell. This will italicise and add a grey
* color to the cell text.
* @default false
*/
greyed?: boolean;
classes?: string;
emptyCellTooltipText?: React.ReactNode;
}
const TextCell = ({
value,
formatter = (val) => val, // identity function if no formatter is provided
greyed = false,
classes = "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) {
greyed = 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;
return (
<span className={`text-cell ${classes} ${greyed ? "grey-cell" : ""}`}>
{cellText}
</span>
);
};
export default TextCell;