fleet/frontend/components/TableContainer/DataTable/TextCell/TextCell.tsx
Jahziel Villasana-Espinoza a00559e732
feat: enable multiple ABM and VPP tokens (#21693)
> Related issue: #9956 

# Checklist for submitter

If some of the following don't apply, delete the relevant line.

<!-- Note that API documentation changes are now addressed by the
product design team. -->

- [x] Changes file added for user-visible changes in `changes/`,
`orbit/changes/` or `ee/fleetd-chrome/changes`.
See [Changes
files](https://github.com/fleetdm/fleet/blob/main/docs/Contributing/Committing-Changes.md#changes-files)
for more information.
- [x] Input data is properly validated, `SELECT *` is avoided, SQL
injection is prevented (using placeholders for values in statements)
- [x] Added/updated tests
- [x] If paths of existing endpoints are modified without backwards
compatibility, checked the frontend/CLI for any necessary changes
- [x] If database migrations are included, checked table schema to
confirm autoupdate
- For database migrations:
- [x] Checked schema for all modified table for columns that will
auto-update timestamps during migration.
- [x] Confirmed that updating the timestamps is acceptable, and will not
cause unwanted side effects.
- [x] Ensured the correct collation is explicitly set for character
columns (`COLLATE utf8mb4_unicode_ci`).
- [x] Manual QA for all new/changed functionality

---------

Co-authored-by: Martin Angers <martin.n.angers@gmail.com>
Co-authored-by: Gabriel Hernandez <ghernandez345@gmail.com>
Co-authored-by: Roberto Dip <rroperzh@gmail.com>
Co-authored-by: Sarah Gillespie <73313222+gillespi314@users.noreply.github.com>
Co-authored-by: Dante Catalfamo <43040593+dantecatalfamo@users.noreply.github.com>
Co-authored-by: Roberto Dip <dip.jesusr@gmail.com>
2024-08-29 18:51:46 -04: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?: React.ReactNode | { timeString: string };
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;