'use client'; 'use no memo'; // fixes rerendering with react compiler, v9 of tanstack table will fix this import { Button } from '@/components/ui/button'; import { Input } from '@/components/ui/input'; import type { Table } from '@tanstack/react-table'; import { X } from 'lucide-react'; import { DataTableExport } from './data-table-export'; import { DataTableFacetedFilter } from './data-table-faceted-filter'; import { DataTableViewOptions } from './data-table-view-options'; export interface DataTableToolbarOptions { enableToolbar?: boolean; } interface DataTableToolbarProps extends DataTableToolbarOptions { table: Table; } const CAPITAL_LETTER_REGEX = /(?=[A-Z])/; /** * Transforms a camelCase identifier into a pretty title case string * @param identifier The identifier to transform * @returns The prettified title */ function prettifyTitle(identifier: string): string { // Split the identifier at capital letters and lowercase everything const words = identifier.split(CAPITAL_LETTER_REGEX).map((word) => word.toLowerCase()); // Capitalize the first letter of each word return words.map((word) => word.charAt(0).toUpperCase() + word.slice(1)).join(' '); } export function DataTableToolbar({ table, enableToolbar = true }: DataTableToolbarProps) { if (!enableToolbar) { return null; } const isFiltered = table.getState().columnFilters.length > 0; const facetedColumns = table .getAllLeafColumns() .filter((column) => column.getCanFilter()) .map((column) => ({ column, title: prettifyTitle(column.id), options: Array.from(column.getFacetedUniqueValues().entries()).map(([value]) => ({ label: String(value), value: String(value), icon: table.options.meta?.icons?.[value], })), })); const globalFilterColumn = table.getAllLeafColumns().find((col) => col.getCanGlobalFilter()); return (
{globalFilterColumn && ( table.setGlobalFilter(event.target.value)} className="h-8 w-[150px] lg:w-[250px]" /> )} {(facetedColumns ?? []).map((facet) => { return ( ); })} {isFiltered && ( )}
); }