From f1ad828bab7210e48fe9ec3e0383b4c0690121aa Mon Sep 17 00:00:00 2001 From: John Rassa Date: Tue, 10 Jun 2025 11:46:03 -0400 Subject: [PATCH] fix: use single call for hiding/showing all columns within show/hide menu Reworked to toggle visibility with single call while preserving the behavior of the previous implementation. With this change the `onColumnVisibilityChange` callback will only be called once. Fixes: #1126 --- .../components/menus/MRT_ShowHideColumnsMenu.tsx | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/packages/material-react-table/src/components/menus/MRT_ShowHideColumnsMenu.tsx b/packages/material-react-table/src/components/menus/MRT_ShowHideColumnsMenu.tsx index b6df6ead9..543da4011 100644 --- a/packages/material-react-table/src/components/menus/MRT_ShowHideColumnsMenu.tsx +++ b/packages/material-react-table/src/components/menus/MRT_ShowHideColumnsMenu.tsx @@ -8,6 +8,7 @@ import { type MRT_Column, type MRT_RowData, type MRT_TableInstance, + type MRT_VisibilityState } from '../../types'; import { getDefaultColumnOrderIds } from '../../utils/displayColumn.utils'; @@ -47,9 +48,15 @@ export const MRT_ShowHideColumnsMenu = ({ const { columnOrder, columnPinning, density } = getState(); const handleToggleAllColumns = (value?: boolean) => { - getAllLeafColumns() - .filter((col) => col.columnDef.enableHiding !== false) - .forEach((col) => col.toggleVisibility(value)); + const updates = + getAllLeafColumns() + .filter((column) => column.columnDef.enableHiding !== false) + .reduce((acc, column) => { + acc[column.id] = value ?? !column.getIsVisible() + return acc; + }, {} as MRT_VisibilityState); + + table.setColumnVisibility((old) => ({ ...old, ...updates })); }; const allColumns = useMemo(() => {