2025-09-23 23:30:27 +02:00
|
|
|
import { memo } from 'react';
|
|
|
|
|
import { flexRender } from '@tanstack/react-table';
|
|
|
|
|
import type { TableColumn } from './DataTable.types';
|
2025-09-26 18:51:45 +02:00
|
|
|
import type { Row } from '@tanstack/react-table';
|
2025-09-25 22:13:39 +02:00
|
|
|
import { TableCell, TableRow } from '../Table';
|
|
|
|
|
import { Checkbox } from '../Checkbox';
|
|
|
|
|
import { Skeleton } from '../Skeleton';
|
2025-09-23 23:30:27 +02:00
|
|
|
import { cn } from '~/utils';
|
|
|
|
|
|
|
|
|
|
export const SelectionCheckbox = memo(
|
|
|
|
|
({
|
|
|
|
|
checked,
|
|
|
|
|
onChange,
|
|
|
|
|
ariaLabel,
|
|
|
|
|
}: {
|
|
|
|
|
checked: boolean;
|
|
|
|
|
onChange: (value: boolean) => void;
|
|
|
|
|
ariaLabel: string;
|
|
|
|
|
}) => (
|
|
|
|
|
<div
|
|
|
|
|
role="button"
|
|
|
|
|
tabIndex={0}
|
|
|
|
|
onKeyDown={(e) => {
|
|
|
|
|
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);
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
<Checkbox checked={checked} onCheckedChange={onChange} aria-label={ariaLabel} />
|
|
|
|
|
</div>
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
SelectionCheckbox.displayName = 'SelectionCheckbox';
|
|
|
|
|
|
|
|
|
|
const TableRowComponent = <TData extends Record<string, unknown>>({
|
|
|
|
|
row,
|
|
|
|
|
virtualIndex,
|
|
|
|
|
}: {
|
|
|
|
|
row: Row<TData>;
|
|
|
|
|
virtualIndex?: number;
|
|
|
|
|
}) => (
|
|
|
|
|
<TableRow
|
|
|
|
|
data-state={row.getIsSelected() ? 'selected' : undefined}
|
|
|
|
|
data-index={virtualIndex}
|
|
|
|
|
className="border-none hover:bg-surface-secondary"
|
|
|
|
|
>
|
|
|
|
|
{row.getVisibleCells().map((cell) => {
|
2025-09-25 00:24:57 +02:00
|
|
|
const meta = cell.column.columnDef.meta as
|
|
|
|
|
| { className?: string; desktopOnly?: boolean }
|
|
|
|
|
| undefined;
|
|
|
|
|
const isDesktopOnly = meta?.desktopOnly;
|
2025-09-23 23:30:27 +02:00
|
|
|
return (
|
|
|
|
|
<TableCell
|
|
|
|
|
key={cell.id}
|
2025-09-25 00:24:57 +02:00
|
|
|
className={cn(
|
|
|
|
|
'truncate p-3',
|
|
|
|
|
cell.column.id === 'select' && 'p-1',
|
|
|
|
|
meta?.className,
|
|
|
|
|
isDesktopOnly && 'hidden md:table-cell',
|
|
|
|
|
)}
|
2025-09-23 23:30:27 +02:00
|
|
|
>
|
|
|
|
|
{flexRender(cell.column.columnDef.cell, cell.getContext())}
|
|
|
|
|
</TableCell>
|
|
|
|
|
);
|
|
|
|
|
})}
|
|
|
|
|
</TableRow>
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
export const MemoizedTableRow = memo(
|
|
|
|
|
TableRowComponent,
|
|
|
|
|
(prev, next) =>
|
|
|
|
|
prev.row.original === next.row.original &&
|
2025-09-26 18:51:45 +02:00
|
|
|
prev.row.getIsSelected() === next.row.getIsSelected(),
|
2025-09-23 23:30:27 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
export const SkeletonRows = memo(
|
|
|
|
|
<TData extends Record<string, unknown>, TValue>({
|
|
|
|
|
count = 10,
|
|
|
|
|
columns,
|
|
|
|
|
}: {
|
|
|
|
|
count?: number;
|
|
|
|
|
columns: TableColumn<TData, TValue>[];
|
|
|
|
|
}) => (
|
|
|
|
|
<>
|
|
|
|
|
{Array.from({ length: count }, (_, index) => (
|
|
|
|
|
<TableRow key={`skeleton-${index}`} className="h-[56px] border-b border-border-light">
|
|
|
|
|
{columns.map((column) => {
|
|
|
|
|
const columnKey = String(
|
|
|
|
|
column.id ?? ('accessorKey' in column && column.accessorKey) ?? '',
|
|
|
|
|
);
|
2025-09-25 00:24:57 +02:00
|
|
|
const meta = column.meta as { className?: string; desktopOnly?: boolean } | undefined;
|
2025-09-23 23:30:27 +02:00
|
|
|
return (
|
2025-09-25 00:24:57 +02:00
|
|
|
<TableCell
|
|
|
|
|
key={columnKey}
|
|
|
|
|
className={cn(
|
|
|
|
|
'px-2 py-2 md:px-3',
|
|
|
|
|
meta?.className,
|
|
|
|
|
meta?.desktopOnly && 'hidden md:table-cell',
|
|
|
|
|
)}
|
|
|
|
|
>
|
2025-09-23 23:30:27 +02:00
|
|
|
<Skeleton className="h-6 w-full" />
|
|
|
|
|
</TableCell>
|
|
|
|
|
);
|
|
|
|
|
})}
|
|
|
|
|
</TableRow>
|
|
|
|
|
))}
|
|
|
|
|
</>
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
SkeletonRows.displayName = 'SkeletonRows';
|