"use client"; import { Row } from "@tanstack/react-table"; import { CornerDownRightIcon } from "lucide-react"; import { DataTableExpandToggle } from "./data-table-expand-toggle"; /** * Indentation per nesting level in rem units. * Matches the parent's icon (w-4 = 1rem) + gap-2 (0.5rem) = 1.5rem, * so the child's first icon aligns horizontally with the parent's checkbox. */ const INDENT_PER_LEVEL_REM = 1.5; interface DataTableExpandableCellProps { row: Row; children: React.ReactNode; /** Whether to show the expand/collapse toggle (default: true) */ showToggle?: boolean; /** Explicit expanded state — pass to break React Compiler memoization */ isExpanded?: boolean; /** Hide the CornerDownRight icon even for child rows (e.g. OUs that can expand) */ hideChildIcon?: boolean; /** Optional slot rendered after expand arrows and before children (e.g. checkbox) */ checkboxSlot?: React.ReactNode; } /** * DataTableExpandableCell is a wrapper component for table cells that need * to display content with proper indentation for nested rows. * * Features: * - Automatically indents content based on row depth * - Shows CornerDownRight icon for child rows (depth > 0) * - Optionally includes the expand/collapse toggle for parent rows * - Maintains proper alignment for all nesting levels * * @example * ```tsx * // In column definition: * { * accessorKey: "name", * header: "Name", * cell: ({ row }) => ( * * {row.original.name} * * ), * } * ``` */ export function DataTableExpandableCell({ row, children, showToggle = true, isExpanded, hideChildIcon = false, checkboxSlot, }: DataTableExpandableCellProps) { const isChildRow = row.depth > 0; const canExpand = row.getCanExpand(); return (
{showToggle && ( <> {isChildRow && !hideChildIcon && ( )} {canExpand ? ( ) : !isChildRow ? ( // shrink-0: keep this fixed-width spacer from collapsing under wide content, which would drag the checkbox left
) : null} )} {checkboxSlot && (
{checkboxSlot}
)} {children}
); }