/* Copyright 2026 Marimo. All rights reserved. */ "use no memo"; import type { RowSelectionState, Table } from "@tanstack/react-table"; import { useLocale } from "react-aria"; import type { GetRowIds } from "@/plugins/impl/DataTablePlugin"; import { cn } from "@/utils/cn"; import { Events } from "@/utils/events"; import { prettyNumber } from "@/utils/numbers"; import { Button } from "../ui/button"; import { toast } from "../ui/use-toast"; import { DataTablePagination, prettifyRowColumnCount } from "./pagination"; import { CellSelectionStats } from "./range-focus/cell-selection-stats"; import type { DataTableSelection } from "./types"; interface TableBottomBarProps { pagination: boolean; totalColumns: number; selection?: DataTableSelection; onRowSelectionChange?: (value: RowSelectionState) => void; table: Table; getRowIds?: GetRowIds; showPageSizeSelector?: boolean; tableLoading?: boolean; part?: string; className?: string; } export const TableBottomBar = ({ pagination, totalColumns, selection, onRowSelectionChange, table, getRowIds, showPageSizeSelector, tableLoading, part, className, }: TableBottomBarProps) => { const { locale } = useLocale(); const handleSelectAllRows = (value: boolean) => { if (!onRowSelectionChange) { return; } // Clear all selections if (!value) { onRowSelectionChange({}); return; } const selectAllRowsByIndex = () => { const allKeys = Array.from( { length: table.getRowCount() }, (_, i) => [i, true] as const, ); onRowSelectionChange(Object.fromEntries(allKeys)); }; if (!getRowIds) { selectAllRowsByIndex(); return; } getRowIds({}).then((data) => { if (data.error) { toast({ title: "Not available", description: data.error, variant: "danger", }); return; } if (data.all_rows) { selectAllRowsByIndex(); } else { onRowSelectionChange( Object.fromEntries(data.row_ids.map((id) => [id, true])), ); } }); }; const renderTotal = () => { const { rowSelection, cellSelection } = table.getState(); let selected = Object.keys(rowSelection).length; let isAllPageSelected = table.getIsAllPageRowsSelected(); const numRows = table.getRowCount(); let isAllSelected = selected === numRows; const isCellSelection = selection === "single-cell" || selection === "multi-cell"; if (isCellSelection) { selected = cellSelection.length; isAllPageSelected = false; isAllSelected = false; } if (isAllPageSelected && !isAllSelected) { return ( <> {prettyNumber(selected, locale)} selected ); } if (selected) { return ( <> {prettyNumber(selected, locale)} selected ); } return ( {prettifyRowColumnCount({ numRows: table.getRowCount(), totalColumns, locale, })} ); }; return (
{renderTotal()}
{pagination && ( )}
); };