import { memo } from 'react';
import { flexRender } from '@tanstack/react-table';
import type { Row, ColumnDef } from '@tanstack/react-table';
import type { TableColumn } from './DataTable.types';
import { TableCell, TableRow } from '../Table';
import { Checkbox } from '../Checkbox';
import { Skeleton } from '../Skeleton';
import { cn } from '~/utils';
export const SelectionCheckbox = memo(
({
checked,
onChange,
ariaLabel,
}: {
checked: boolean;
onChange: (value: boolean) => void;
ariaLabel: string;
}) => (
{
if (e.key === 'Enter' || e.key === ' ') {
e.preventDefault();
onChange(!checked);
}
e.stopPropagation();
}}
className="flex h-full w-[30px] items-center justify-center"
onClick={(e) => {
e.stopPropagation();
onChange(!checked);
}}
>
),
);
SelectionCheckbox.displayName = 'SelectionCheckbox';
const TableRowComponent = >({
row,
virtualIndex,
}: {
row: Row;
columns: ColumnDef[];
index: number;
virtualIndex?: number;
}) => (
{row.getVisibleCells().map((cell) => {
const meta = cell.column.columnDef.meta as
| { className?: string; desktopOnly?: boolean }
| undefined;
const isDesktopOnly = meta?.desktopOnly;
return (
{flexRender(cell.column.columnDef.cell, cell.getContext())}
);
})}
);
export const MemoizedTableRow = memo(
TableRowComponent,
(prev, next) =>
prev.row.original === next.row.original &&
prev.row.getIsSelected() === next.row.getIsSelected() &&
prev.columns === next.columns,
);
export const SkeletonRows = memo(
, TValue>({
count = 10,
columns,
}: {
count?: number;
columns: TableColumn[];
}) => (
<>
{Array.from({ length: count }, (_, index) => (
{columns.map((column) => {
const columnKey = String(
column.id ?? ('accessorKey' in column && column.accessorKey) ?? '',
);
const meta = column.meta as { className?: string; desktopOnly?: boolean } | undefined;
return (
);
})}
))}
>
),
);
SkeletonRows.displayName = 'SkeletonRows';