import _ from 'lodash'; import clsx from 'clsx'; import { Menu, MenuButton, MenuList } from '@reach/menu-button'; import { Columns } from 'lucide-react'; import { Table } from '@tanstack/react-table'; import { Checkbox } from '@@/form-components/Checkbox'; interface Props { onChange: (value: string[]) => void; value: string[]; table: Table; } export function ColumnVisibilityMenu({ onChange, value, table, }: Props) { const columnsToHide = table.getAllColumns().filter((col) => col.getCanHide()); if (!columnsToHide.length) { return null; } return ( {({ isExpanded }) => ( <>
Show / Hide Columns
{columnsToHide.map((column) => (
handleChangeColumnVisibility( column.id, e.target.checked ) } />
))}
)}
); function handleChangeColumnVisibility(colId: string, visible: boolean) { const newValue = visible ? value.filter((id) => id !== colId) : [...value, colId]; table.setColumnVisibility( Object.fromEntries(newValue.map((col) => [col, false])) ); onChange(newValue); } } export function getColumnVisibilityState(hiddenColumns: string[]) { return { columnVisibility: Object.fromEntries( hiddenColumns.map((col) => [col, false]) ), }; }