@wonderflow/react-components
Version:
UI components from Wonderflow's Wanda design system
39 lines (38 loc) • 2.39 kB
JavaScript
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
/*
* Copyright 2022-2023 Wonderflow Design Team
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { useCallback, useMemo } from 'react';
import { Button, Menu, Popover } from '../../..';
export const ToggleColumnsControl = ({ columns, visibleColumns, }) => {
const hasSomeVisibleColums = useMemo(() => visibleColumns.length !== 0, [visibleColumns]);
const filteredColumns = useMemo(() => columns.filter(col => !col.isToggable), [columns]);
const handleToggleAll = useCallback(() => {
/**
* If there are visible columns (excluding artificial ones),
* hide them all by calling `toggleHidden(true)` for each column
*/
if (hasSomeVisibleColums) {
visibleColumns.forEach(col => col.toggleHidden(true));
return;
}
/**
* If there are no visible columns (excluding artificial ones), call
* toggleHidden(false) for each column
*/
filteredColumns.forEach(col => col.toggleHidden(false));
}, [hasSomeVisibleColums, filteredColumns, visibleColumns]);
return (_jsx(Popover, { placement: "bottom-start", trigger: _jsx(Button, { kind: "secondary", children: "Toggle columns" }), children: _jsxs(Menu, { maxHeight: "350px", children: [_jsx(Menu.ItemCheckbox, { value: "toggle-all", onClick: () => handleToggleAll(), checked: true, icon: visibleColumns.length === 0 ? 'check' : 'minus', "data-testid": "ColumnCheckbox", children: "Toggle All" }), columns.filter(col => !col.isToggable).map((column, i) => (_jsx(Menu.ItemCheckbox, { value: column.id, autoFocus: i === 0, checked: column.isVisible, icon: column.isVisible ? 'check' : undefined, onClick: () => column.toggleHidden(), "data-testid": "ColumnCheckboxInner", children: column.render('Header') }, column.id)))] }) }));
};