Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import { TableProps } from "../types";
import { tableComfortStyles, tableCompactStyles } from "../styles";
import { Box, PaginationItem, SvgIconProps, useTheme } from "@mui/material";
import { EmptyState, Link, OverflowTooltip, TooltipSize } from "@/components";
import { withHeaderHelpTooltips } from "../utils/helpers";

/**
* `Table` Component
Expand Down Expand Up @@ -109,7 +110,7 @@ export const CreateTableInstance = <TData extends MRT_RowData>({
enableFilters: !!data.length,
enableFullScreenToggle: !!data.length,
enableHiding: !!data.length,
columns,
columns: withHeaderHelpTooltips(columns),
data,
enableColumnActions: false,
enableTopToolbar,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,18 +118,21 @@ const data = {
accessorKey: "id",
header: "ID",
size: 350,
meta: { headerTooltip: "The unique identifier of the record" },
},
{
header: "Name",
accessorKey: "name",
enableColumnOrdering: false,
size: 200,
meta: { headerTooltip: "Full name of the person" },
},
{
header: "Phone",
accessorKey: "phone",
enableSorting: false,
enableColumnOrdering: false,
meta: { headerTooltip: "Primary phone number" },
},
{
header: "firstName",
Expand Down
5 changes: 5 additions & 0 deletions packages/open-ui-kit/src/components/table/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,16 @@ interface TableRow {
subRows?: ReactNode;
}

interface HeaderTooltipMeta {
headerTooltip?: ReactNode;
}

export {
AtomicTypes,
ExportProps,
TableProps,
TableRow,
TopToolbarProps,
TableTitle,
HeaderTooltipMeta,
};
51 changes: 51 additions & 0 deletions packages/open-ui-kit/src/components/table/utils/helpers.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,14 @@

import {
MRT_Column,
MRT_ColumnDef,
MRT_RowData,
MRT_TableInstance,
} from "material-react-table";
import { Box, IconButton, Stack } from "@mui/material";
import { Tooltip, TooltipSize } from "@/components";
import { InfoCircleOutline } from "@/custom-icons";
import { HeaderTooltipMeta } from "../types";

// Check rightmost left-pinned column / leftmost right-pinned column
export const isOuterPinnedColumn = <TData extends MRT_RowData>(
Expand Down Expand Up @@ -38,3 +43,49 @@ export const parseFromValuesOrFunc = <T, U>(
fn: ((arg: U) => T) | T | undefined,
arg: U,
): T | undefined => (fn instanceof Function ? fn(arg) : fn);

// Enhance column headers to optionally show an info tooltip icon.
// Usage: column.meta?.headerTooltip?: React.ReactNode
export const withHeaderHelpTooltips = <
TData extends MRT_RowData,
TValue = unknown,
>(
columns: MRT_ColumnDef<TData, TValue>[],
): MRT_ColumnDef<TData, TValue>[] => {
return columns.map((col) => {
const meta = col.meta as HeaderTooltipMeta;
const hasHeaderTooltip =
meta?.headerTooltip !== undefined && meta?.headerTooltip !== null;

const updated: MRT_ColumnDef<TData, TValue> = {
...col,
};

if (hasHeaderTooltip) {
updated.Header = (
<Stack direction="row" alignItems="flex-start" gap="2px">
<Box component="span">{col.header}</Box>
<Tooltip
size={TooltipSize.Medium}
title={meta?.headerTooltip}
placement="top"
>
<IconButton
sx={{
color: (theme) => theme.palette.vars.controlIconDefault,
"&:hover": {
color: (theme) => theme.palette.vars.controlIconHover,
},
}}
aria-label="Column info"
>
<InfoCircleOutline sx={{ width: "16px", height: "16px" }} />
</IconButton>
</Tooltip>
</Stack>
);
}

return updated;
});
};