2022-01-04 12:16:09 +00:00
|
|
|
import clsx from 'clsx';
|
|
|
|
import { Menu, MenuButton, MenuList } from '@reach/menu-button';
|
|
|
|
import { ColumnInstance } from 'react-table';
|
2022-06-23 06:32:18 +00:00
|
|
|
import { Columns } from 'react-feather';
|
2022-01-04 12:16:09 +00:00
|
|
|
|
2022-06-17 16:18:42 +00:00
|
|
|
import { Checkbox } from '@@/form-components/Checkbox';
|
2022-01-04 12:16:09 +00:00
|
|
|
|
|
|
|
import { useTableContext } from './TableContainer';
|
|
|
|
|
2022-01-23 19:48:04 +00:00
|
|
|
interface Props<D extends object> {
|
|
|
|
columns: ColumnInstance<D>[];
|
2022-01-04 12:16:09 +00:00
|
|
|
onChange: (value: string[]) => void;
|
|
|
|
value: string[];
|
|
|
|
}
|
|
|
|
|
2022-01-23 19:48:04 +00:00
|
|
|
export function ColumnVisibilityMenu<D extends object>({
|
|
|
|
columns,
|
|
|
|
onChange,
|
|
|
|
value,
|
|
|
|
}: Props<D>) {
|
2022-01-04 12:16:09 +00:00
|
|
|
useTableContext();
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Menu className="setting">
|
|
|
|
{({ isExpanded }) => (
|
|
|
|
<>
|
|
|
|
<MenuButton
|
|
|
|
className={clsx('table-setting-menu-btn', {
|
|
|
|
'setting-active': isExpanded,
|
|
|
|
})}
|
|
|
|
>
|
2022-06-23 06:32:18 +00:00
|
|
|
<Columns
|
|
|
|
size="13"
|
|
|
|
className="space-right"
|
|
|
|
strokeWidth="3px"
|
|
|
|
aria-hidden="true"
|
|
|
|
aria-label="Columns"
|
|
|
|
/>
|
2022-01-04 12:16:09 +00:00
|
|
|
</MenuButton>
|
|
|
|
<MenuList>
|
|
|
|
<div className="tableMenu">
|
|
|
|
<div className="menuHeader">Show / Hide Columns</div>
|
|
|
|
<div className="menuContent">
|
|
|
|
{columns.map((column) => (
|
|
|
|
<div key={column.id}>
|
|
|
|
<Checkbox
|
|
|
|
checked={column.isVisible}
|
|
|
|
label={column.Header as string}
|
|
|
|
id={`visibility_${column.id}`}
|
|
|
|
onChange={(e) =>
|
|
|
|
handleChangeColumnVisibility(
|
|
|
|
column.id,
|
|
|
|
e.target.checked
|
|
|
|
)
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
))}
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</MenuList>
|
|
|
|
</>
|
|
|
|
)}
|
|
|
|
</Menu>
|
|
|
|
);
|
|
|
|
|
|
|
|
function handleChangeColumnVisibility(colId: string, visible: boolean) {
|
|
|
|
if (visible) {
|
|
|
|
onChange(value.filter((id) => id !== colId));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
onChange([...value, colId]);
|
|
|
|
}
|
|
|
|
}
|