import clsx from 'clsx'; import classes from './MRT_ShowHideColumnsMenu.module.css'; import { useMemo, useState } from 'react'; import { Button, Flex, Menu } from '@mantine/core'; import { MRT_ShowHideColumnsMenuItems } from './MRT_ShowHideColumnsMenuItems'; import { type MRT_Column, type MRT_RowData, type MRT_TableInstance, } from '../../types'; import { getDefaultColumnOrderIds } from '../../utils/displayColumn.utils'; interface Props { table: MRT_TableInstance; } export const MRT_ShowHideColumnsMenu = ({ table, }: Props) => { 'use no memo'; const { getAllColumns, getAllLeafColumns, getCenterLeafColumns, getIsAllColumnsVisible, getIsSomeColumnsPinned, getIsSomeColumnsVisible, getLeftLeafColumns, getRightLeafColumns, getState, options: { enableColumnOrdering, enableColumnPinning, enableHiding, localization, }, } = table; const { columnOrder, columnPinning } = getState(); const handleToggleAllColumns = (value?: boolean) => { getAllLeafColumns() .filter((col) => col.columnDef.enableHiding !== false) .forEach((col) => col.toggleVisibility(value)); }; const allColumns = useMemo(() => { const columns = getAllColumns(); if ( columnOrder.length > 0 && !columns.some((col) => col.columnDef.columnDefType === 'group') ) { return [ ...getLeftLeafColumns(), ...Array.from(new Set(columnOrder)).map((colId) => getCenterLeafColumns().find((col) => col?.id === colId), ), ...getRightLeafColumns(), ].filter(Boolean); } return columns; }, [ columnOrder, columnPinning, getAllColumns(), getCenterLeafColumns(), getLeftLeafColumns(), getRightLeafColumns(), ]) as MRT_Column[]; const [hoveredColumn, setHoveredColumn] = useState | null>( null, ); return ( {enableHiding && ( )} {enableColumnOrdering && ( )} {enableColumnPinning && ( )} {enableHiding && ( )} {allColumns.map((column, index) => ( ))} ); };