import * as React from "react" import { Columns3Icon } from "lucide-react" import type { Table as TanStackTable } from "@tanstack/react-table" import { Button } from "@/components/ui/button" import { DropdownMenu, DropdownMenuCheckboxItem, DropdownMenuContent, DropdownMenuLabel, DropdownMenuSeparator, DropdownMenuTrigger, } from "@/components/ui/dropdown-menu" import { cn } from "@/lib/utils" export type DataTableColumnVisibilityMenuProps = { table: TanStackTable label?: React.ReactNode triggerLabel?: React.ReactNode align?: "start" | "center" | "end" side?: "top" | "right" | "bottom" | "left" triggerClassName?: string contentClassName?: string emptyLabel?: React.ReactNode showCount?: boolean triggerAriaLabel?: string getColumnLabel?: (columnId: string) => React.ReactNode } function defaultColumnLabel(columnId: string) { return columnId .replace(/_/g, " ") .replace(/-/g, " ") .replace(/\b\w/g, (char) => char.toUpperCase()) } function DataTableColumnVisibilityMenu({ table, label = "Columns", triggerLabel, align = "end", side = "bottom", triggerClassName, contentClassName, emptyLabel = "No hideable columns", showCount = true, triggerAriaLabel, getColumnLabel = defaultColumnLabel, }: DataTableColumnVisibilityMenuProps) { const columns = table .getAllLeafColumns() .filter((column) => column.getCanHide()) const visibleCount = columns.filter((column) => column.getIsVisible()).length return ( } > {triggerLabel ?? label} {showCount && columns.length > 0 ? ( {visibleCount}/{columns.length} ) : null} {label} {showCount ? {visibleCount} visible : null} {columns.length === 0 ? ( {emptyLabel} ) : null} {columns.map((column) => ( column.toggleVisibility(Boolean(value))} > {getColumnLabel(column.id)} ))} ) } export { DataTableColumnVisibilityMenu }