-
-
Notifications
You must be signed in to change notification settings - Fork 98
Expand file tree
/
Copy pathDataTableHeaderCellFilter.tsx
More file actions
49 lines (46 loc) · 1.88 KB
/
Copy pathDataTableHeaderCellFilter.tsx
File metadata and controls
49 lines (46 loc) · 1.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import { ActionIcon, Popover, PopoverDropdown, PopoverTarget } from '@mantine/core';
import { useClickOutside, useDisclosure } from '@mantine/hooks';
import type { RefObject } from 'react';
import { IconFilter } from './icons/IconFilter';
import { IconFilterFilled } from './icons/IconFilterFilled';
import type { DataTableColumn } from './types';
type DataTableHeaderCellFilterProps<T> = {
children: DataTableColumn<T>['filter'];
filterPopoverProps: DataTableColumn<T>['filterPopoverProps'];
filterPopoverDisableClickOutside?: DataTableColumn<T>['filterPopoverDisableClickOutside'];
isActive: boolean;
};
export function DataTableHeaderCellFilter<T>({
children,
isActive,
filterPopoverProps,
filterPopoverDisableClickOutside,
}: DataTableHeaderCellFilterProps<T>) {
const [isOpen, { close, toggle }] = useDisclosure(false);
const Icon = isActive ? IconFilterFilled : IconFilter;
let ref: RefObject<HTMLDivElement | null> | undefined = useClickOutside(close);
if (filterPopoverDisableClickOutside) ref = undefined;
return (
// Keep the expanded props before the open/close, or they could be overridden by the client
<Popover withArrow shadow="md" trapFocus {...filterPopoverProps} opened={isOpen} onClose={close} onDismiss={close}>
<PopoverTarget>
<ActionIcon
className="mantine-datatable-header-cell-filter-action-icon"
data-active={isActive || undefined}
size="sm"
variant="default"
onClick={(e) => {
e.preventDefault();
toggle();
}}
onKeyDown={(e) => e.stopPropagation()}
>
<Icon />
</ActionIcon>
</PopoverTarget>
<PopoverDropdown ref={ref} onClick={(e) => e.stopPropagation()} onKeyDown={(e) => e.stopPropagation()}>
{typeof children === 'function' ? children({ close }) : children}
</PopoverDropdown>
</Popover>
);
}