import clsx from "clsx"; import { useTranslation } from "react-i18next"; import { useNavigate, useSearchParams } from "react-router"; import { PaginationDto } from "~/application/dtos/data/PaginationDto"; import { RowHeaderDisplayDto } from "~/application/dtos/data/RowHeaderDisplayDto"; import RowDisplayValueHelper from "~/utils/helpers/RowDisplayValueHelper"; import ButtonTertiary from "../buttons/ButtonTertiary"; import TablePagination from "./TablePagination"; import { ReactNode, useEffect, useLayoutEffect, useMemo, useRef, useState } from "react"; import { RowHeaderActionDto } from "~/application/dtos/data/RowHeaderActionDto"; import DownArrow from "../icons/DownArrow"; import UpArrow from "../icons/UpArrow"; import ConfirmModal, { RefConfirmModal } from "../modals/ConfirmModal"; import { Checkbox } from "../checkbox"; import { translateLabel } from "~/utils/i18n/translateLabel"; interface Props { headers: RowHeaderDisplayDto[]; items: T[]; actions?: RowHeaderActionDto[]; pagination?: PaginationDto; onClickRoute?: (idx: number, item: T) => string | undefined; selectedRows?: T[]; onSelected?: (item: T[]) => void; className?: (idx: number, item: T) => string; padding?: string; noRecords?: ReactNode; emptyState?: { title: string; description: string; icon?: ReactNode }; darkMode?: boolean; } export default function TableSimple(props: Props) { const [showChild, setShowChild] = useState(false); // Wait until after client-side hydration to show useEffect(() => { setShowChild(true); }, []); if (!showChild) { // You can show some kind of placeholder UI here return null; } return ; } function Table({ headers, items, actions = [], pagination, onClickRoute, selectedRows, onSelected, className, // Subframe Cell padding: px-4 py-1 (was px-2 py-2). padding = "px-4 py-1", noRecords, emptyState, darkMode, }: Props) { const { t, i18n } = useTranslation(); const navigate = useNavigate(); const [searchParams, setSearchParams] = useSearchParams(); const [sortBy, setSortBy] = useState<{ by: string; order: "asc" | "desc" }[]>(); const visibleHeaders = useMemo(() => headers.filter((header) => !header.hidden), [headers]); const firstColumnActions = useMemo(() => actions.filter((action) => action.firstColumn), [actions]); const trailingActions = useMemo(() => actions.filter((action) => !action.firstColumn), [actions]); const selectedRowsSet = useMemo(() => new Set(selectedRows ?? []), [selectedRows]); useEffect(() => { let sort = searchParams.get("sort"); const sortArray = sort?.split(",") ?? []; const sortObject = sortArray.map((s) => { let order: "asc" | "desc" = "asc"; if (s.startsWith("-")) { order = "desc"; } return { by: s.replace("-", ""), order }; }); setSortBy(sortObject); }, [searchParams]); // const [selectedRows, setSelectedRows] = useState([]); // useEffect(() => { // if (onSelected) { // onSelected(selectedRows); // } // }, [selectedRows]); function toggleSelected(_: number, item: T) { if (!selectedRows || !onSelected) { return; } if (selectedRowsSet.has(item)) { onSelected(selectedRows.filter((i) => i !== item)); } else { onSelected([...selectedRows, item]); } } const checkbox = useRef(null); const [checked, setChecked] = useState(false); const [indeterminate, setIndeterminate] = useState(false); useLayoutEffect(() => { if (!selectedRows || !onSelected) { return; } const isIndeterminate = selectedRows.length > 0 && selectedRows.length < items.length; setChecked(selectedRows.length === items.length && items.length > 0); setIndeterminate(isIndeterminate); // @ts-ignore checkbox.current.indeterminate = isIndeterminate; // eslint-disable-next-line react-hooks/exhaustive-deps }, [onSelected, selectedRows]); function toggleAll() { if (!selectedRows || !onSelected) { return; } onSelected(checked || indeterminate ? [] : items); setChecked(!checked && !indeterminate); setIndeterminate(false); } function onHeaderClick(header: RowHeaderDisplayDto) { if (!header.sortBy) { return; } let currentSort = sortBy?.find((s) => s.by === header.sortBy); let newSort = header.sortBy; if (currentSort?.order === "asc") { newSort = `-${header.sortBy}`; } searchParams.set("sort", newSort); setSearchParams(searchParams); } function getSortDirection(header: RowHeaderDisplayDto) { if (!header.sortBy) { return; } let currentSort = sortBy?.find((s) => s.by === header.sortBy); if (!currentSort) { return; } return currentSort.order; } return (
{/* tabIndex makes the horizontally-scrollable region keyboard-reachable (axe scrollable-region-focusable) when the table overflows on mobile. */}
{/* Subframe Table root layout: full-width, fit height, collapsed borders. */}
{/* Subframe header row typo: extra-small medium label on secondary text. */} {firstColumnActions.length > 0 && } {onSelected && ( )} {visibleHeaders.map((header, idxHeader) => { return ( ); })} {trailingActions.length > 0 && } {items.length === 0 ? ( ) : ( items.map((item, idxRow) => { const href = onClickRoute?.(idxRow, item); return ( { if (e.ctrlKey || e.metaKey) { window.open(href, "_blank"); } else { navigate(href); } } : undefined } className={clsx( // Subframe Cell minimum row height (min-h-[48px]). "group h-12", href && "hover:bg-surface-neutral-secondary cursor-pointer", darkMode && "" )} > {onSelected && ( )} {visibleHeaders.map((header, idxHeader) => { return ( ); })} ); }) )}
e.stopPropagation()} /> onHeaderClick(header)} className={clsx( idxHeader === 0 && firstColumnActions.length === 0 && "pl-4", // Subframe HeaderCell measures: h-10 row, px-4 cell padding. "h-10 px-4 tracking-wider whitespace-nowrap", header.breakpoint === "sm" && "hidden sm:table-cell", header.breakpoint === "md" && "hidden md:table-cell", header.breakpoint === "lg" && "hidden lg:table-cell", header.breakpoint === "xl" && "hidden xl:table-cell", header.breakpoint === "2xl" && "hidden 2xl:table-cell", header.sortBy && "cursor-pointer" )} >
{translateLabel(t, i18n, header.title)}
{header.sortBy && (
{getSortDirection(header) === "desc" ? ( ) : ( getSortDirection(header) === "asc" && )}
)}
{noRecords ?? (
{!emptyState ? ( {t("shared.noRecords")} ) : (
{emptyState.title}
{emptyState.description}
)}
)}
{selectedRowsSet.has(item) &&
}
{ toggleSelected(idxRow, item); }} onClick={(e) => e.stopPropagation()} />
{RowDisplayValueHelper.displayRowValue(t, header, item, idxRow)}
{pagination && ( )} ); } function ActionsCells({ item, actions, idxRow, className, }: { item: T; actions: RowHeaderActionDto[]; idxRow: number; className?: (idx: number, item: T) => string; }) { const { t } = useTranslation(); const refConfirm = useRef(null); function onConfirmed({ action, item }: { action: RowHeaderActionDto; item: T }) { if (action.onClick) { action.onClick(idxRow, item); } } return ( <> {actions && actions.length > 0 && (
{actions .filter((f) => !f.hidden || !f.hidden(item)) .map((action, idx) => { return ( { e.stopPropagation(); e.preventDefault(); if (action.onClick) { if (action.confirmation) { const confirmation = action.confirmation(item); refConfirm.current?.setDestructive(action.destructive || false); refConfirm.current?.setValue({ action: action, item: item, }); refConfirm.current?.show(confirmation.title, t("shared.confirm"), t("shared.cancel"), confirmation.description); } else { action.onClick(idxRow, item); } } }} to={action.onClickRoute && action.onClickRoute(idxRow, item)} target={action.onClickRouteTarget} > {action.renderTitle ? action.renderTitle(item) : action.title} ); })}
)} ); }