fleet/frontend/components/TableContainer/DataTable/TextCell/TextCell.tsx
Jacob Shandling bb56e288e5
UI – 14415 frontend - host details (#15437)
## Addresses the first major part of #15011 (item 2) – Host Details >
Queries tab

<img width="1274" alt="Screenshot 2023-12-04 at 1 09 31 PM"
src="https://github.com/fleetdm/fleet/assets/61553566/47075ebb-eb98-48f5-82ab-af4022932376">
<img width="678" alt="Screenshot 2023-12-04 at 1 09 57 PM"
src="https://github.com/fleetdm/fleet/assets/61553566/db48ca6d-e73b-4a90-b782-f9ee265927f1">

- [x] Added/updated tests
- [x] Manual QA for all new/changed functionality

---------

Co-authored-by: Jacob Shandling <jacob@fleetdm.com>
2023-12-05 13:58:02 -08:00

60 lines
1.5 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) => JSX.Element | string; // string, number, or null
greyed?: boolean;
classes?: string;
emptyCellTooltipText?: JSX.Element | string;
}
const TextCell = ({
value,
formatter = (val) => val, // identity function if no formatter is provided
greyed,
classes = "w250",
emptyCellTooltipText,
}: ITextCellProps): JSX.Element => {
let val = value;
if (typeof value === "boolean") {
val = value.toString();
}
if (!val) {
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;
};
return (
<span className={`text-cell ${classes} ${greyed && "grey-cell"}`}>
{formatter(val) || renderEmptyCell()}
</span>
);
};
export default TextCell;