import type { Table } from "@tanstack/react-table"; import type { Table as TableType } from "@tanstack/table-core/build/lib/types"; import { AnimatePresence } from "framer-motion"; import { Fragment } from "react"; import type { IconName } from "../.."; import { Button } from "../button"; export type ActionItem = | { type: "action"; label: string; onClick: () => void; icon?: IconName; needsXSelected?: number; } | { type: "render"; render: (table: Table) => React.ReactNode; needsXSelected?: number; }; interface DataTableSelectionBarProps { table: Table; actions?: ActionItem[]; renderAboveSelection?: (table: TableType) => React.ReactNode; } export function DataTableSelectionBar({ table, actions, renderAboveSelection, }: DataTableSelectionBarProps) { const numberOfSelectedRows = table.getSelectedRowModel().rows.length; const isVisible = numberOfSelectedRows > 0; // Hacky left % to center const actionsVisible = actions?.filter((a) => { if (!a.needsXSelected) return true; return a.needsXSelected <= numberOfSelectedRows; }); return ( {isVisible ? (
{renderAboveSelection && renderAboveSelection(table)}

{numberOfSelectedRows} selected

{actionsVisible?.map((action, index) => { return ( {action.type === "action" ? ( ) : action.type === "render" ? ( action.render(table) ) : null} ); })}
) : null}
); }