|
| 1 | +import { Box, ButtonBase, Typography, useTheme } from '@mui/material'; |
| 2 | +import { ReactElement, useMemo, useRef, useState } from 'react'; |
| 3 | +import { ColumnFilterDropdown } from './ColumnFilterDropDown'; |
| 4 | +import { TableColumnConfig } from './model/table-model'; |
| 5 | +import { FilterColumns } from './TableFilters'; |
| 6 | + |
| 7 | +interface Props<TableData> extends FilterColumns { |
| 8 | + id: string; |
| 9 | + width?: number | 'auto'; |
| 10 | + filters: Array<string | number>; |
| 11 | + borderRight: string; |
| 12 | + column: TableColumnConfig<TableData>; |
| 13 | + columnUniqueValues: Record<string, Array<string | number>>; |
| 14 | + openFilterColumn?: string; |
| 15 | + setOpenFilterColumn: (columnId?: string) => void; |
| 16 | +} |
| 17 | + |
| 18 | +export function ColumnFilter<TableData>({ |
| 19 | + id, |
| 20 | + width, |
| 21 | + filters, |
| 22 | + column, |
| 23 | + setColumnFilters, |
| 24 | + columnFilters, |
| 25 | + borderRight, |
| 26 | + columnUniqueValues, |
| 27 | + openFilterColumn, |
| 28 | + setOpenFilterColumn, |
| 29 | +}: Props<TableData>): ReactElement { |
| 30 | + const theme = useTheme(); |
| 31 | + const dropdownId = id.concat('-dropdown'); |
| 32 | + |
| 33 | + const [filterAnchorEl, setFilterAnchorEl] = useState<{ [key: string]: HTMLElement | undefined }>({}); |
| 34 | + const [calculatedWidth, setCalculatedWidth] = useState<string>('0px'); |
| 35 | + |
| 36 | + const handleFilterClick = (event: React.MouseEvent<HTMLButtonElement>, columnId: string): void => { |
| 37 | + event.preventDefault(); |
| 38 | + event.stopPropagation(); |
| 39 | + setFilterAnchorEl({ ...filterAnchorEl, [columnId]: event.currentTarget }); |
| 40 | + setOpenFilterColumn(columnId); |
| 41 | + }; |
| 42 | + |
| 43 | + const handleFilterClose = (): void => { |
| 44 | + setFilterAnchorEl({}); |
| 45 | + setOpenFilterColumn(undefined); |
| 46 | + }; |
| 47 | + |
| 48 | + const updateColumnFilter = (columnId: string, values: Array<string | number>): void => { |
| 49 | + const newFilters = columnFilters.filter((f) => f.id !== columnId); |
| 50 | + if (values.length) { |
| 51 | + newFilters.push({ id: columnId, value: values }); |
| 52 | + } |
| 53 | + setColumnFilters(newFilters); |
| 54 | + }; |
| 55 | + |
| 56 | + const mainContainerRef = useRef<HTMLDivElement>(null); |
| 57 | + const [mainContainerDimension, setMainContainerDimension] = useState<{ width: number; height: number }>({ |
| 58 | + width: 0, |
| 59 | + height: 0, |
| 60 | + }); |
| 61 | + |
| 62 | + const observeDimensionChanges = (htmlElements: ResizeObserverEntry[]): void => { |
| 63 | + if (htmlElements?.length) { |
| 64 | + const targetElement = htmlElements[0]?.target as HTMLElement; |
| 65 | + const width = targetElement.offsetWidth; |
| 66 | + const height = targetElement.offsetHeight; |
| 67 | + setMainContainerDimension({ width, height }); |
| 68 | + } |
| 69 | + }; |
| 70 | + |
| 71 | + /** |
| 72 | + * Width is taken from the optional column.width. Therefore, it could be possibly undefined |
| 73 | + * To handle this, we need the actual width of the container to adjust the width of the dropdown. They need to be perfectly aligned |
| 74 | + * Also, using an observer is necessary due to the effects of the toggle view mode which changes the table dimension |
| 75 | + */ |
| 76 | + const observer = useRef(new ResizeObserver(observeDimensionChanges)); |
| 77 | + if (mainContainerRef.current) { |
| 78 | + observer.current.observe(mainContainerRef.current); |
| 79 | + } |
| 80 | + |
| 81 | + useMemo(() => { |
| 82 | + if (width !== undefined) { |
| 83 | + setCalculatedWidth(typeof width === 'number' ? `${width}px` : width); |
| 84 | + } else if (mainContainerDimension) { |
| 85 | + setCalculatedWidth(`${mainContainerDimension.width}px`); |
| 86 | + } |
| 87 | + }, [width, mainContainerDimension]); |
| 88 | + |
| 89 | + return ( |
| 90 | + <Box |
| 91 | + key={id} |
| 92 | + data-testid={id} |
| 93 | + ref={mainContainerRef} |
| 94 | + sx={{ |
| 95 | + padding: '8px', |
| 96 | + borderRight: borderRight, |
| 97 | + width: width, |
| 98 | + minWidth: width, |
| 99 | + maxWidth: width, |
| 100 | + display: 'flex', |
| 101 | + alignItems: 'center', |
| 102 | + position: 'relative', |
| 103 | + boxSizing: 'border-box', |
| 104 | + flex: typeof width === 'number' ? 'none' : '1 1 auto', |
| 105 | + }} |
| 106 | + > |
| 107 | + <Typography |
| 108 | + variant="body2" |
| 109 | + color="text.secondary" |
| 110 | + noWrap |
| 111 | + component="span" |
| 112 | + sx={{ |
| 113 | + mr: 1, |
| 114 | + flex: 1, |
| 115 | + fontSize: '12px', |
| 116 | + minWidth: '100px', |
| 117 | + }} |
| 118 | + > |
| 119 | + {filters.length ? `${filters.length} items` : 'All'} |
| 120 | + </Typography> |
| 121 | + |
| 122 | + <ButtonBase |
| 123 | + onClick={(e) => handleFilterClick(e, column.accessorKey as string)} |
| 124 | + sx={{ |
| 125 | + border: '1px solid', |
| 126 | + borderColor: 'divider', |
| 127 | + backgroundColor: 'background.paper', |
| 128 | + fontSize: '12px', |
| 129 | + color: filters.length ? 'primary.main' : 'text.secondary', |
| 130 | + px: 1, |
| 131 | + py: 0.5, |
| 132 | + borderRadius: 1, |
| 133 | + minWidth: '20px', |
| 134 | + height: '24px', |
| 135 | + flexShrink: 0, |
| 136 | + transition: (theme) => theme.transitions.create('all', { duration: 200 }), |
| 137 | + '&:hover': { |
| 138 | + backgroundColor: 'action.hover', |
| 139 | + }, |
| 140 | + }} |
| 141 | + > |
| 142 | + ▼ |
| 143 | + </ButtonBase> |
| 144 | + |
| 145 | + {openFilterColumn === column.accessorKey && ( |
| 146 | + <ColumnFilterDropdown |
| 147 | + id={dropdownId} |
| 148 | + width={calculatedWidth} |
| 149 | + allValues={columnUniqueValues[column.accessorKey as string] || []} |
| 150 | + selectedValues={filters} |
| 151 | + onFilterChange={(values) => updateColumnFilter(column.accessorKey as string, values)} |
| 152 | + theme={theme} |
| 153 | + handleFilterClose={handleFilterClose} |
| 154 | + /> |
| 155 | + )} |
| 156 | + </Box> |
| 157 | + ); |
| 158 | +} |
0 commit comments