@samitouri / QOSami-HFS / commits / 4e58b16b

admin: data-tables now offer negated version of text filters

Massimo Melina committed Dec 5, 2023 at 11:07 UTC 4e58b16bf939518c4d410a5d45649dd01cd26f9e
1 file changed +21 -1
admin/src/DataTable.ts
+21 -1
@@ -1,4 +1,5 @@
1 -import { DataGrid, DataGridProps, GridColDef, GridValidRowModel, useGridApiRef } from '@mui/x-data-grid'
1 +import { DataGrid, DataGridProps, enUS, getGridStringOperators, GridColDef, GridValidRowModel, useGridApiRef
2 +} from '@mui/x-data-grid'
3 import { Alert, Box, BoxProps, Breakpoint, LinearProgress, useTheme } from '@mui/material'
4 import { useWindowSize } from 'usehooks-ts'
5 import { createElement as h, Fragment, ReactNode, useEffect, useMemo, useRef, useState } from 'react'
@@ -27,7 +28,26 @@ export function DataTable({ columns, initialState={}, actions, actionsProps, ini
28 const apiRef = useGridApiRef()
29 const [actionsLength, setActionsLength] = useState(0)
30 const manipulatedColumns = useMemo(() => {
31 + const { localeText } = enUS.components.MuiDataGrid.defaultProps
32 const ret = columns.map(col => {
33 + const { type } = col
34 + if (!type || type === 'string') // offer negated version of default string operators
35 + col.filterOperators ??= getGridStringOperators().map(op => op.value.includes('Empty') ? op : [ // isEmpty already has isNotEmpty
36 + op,
37 + {
38 + ...op,
39 + value: '!' + op.value,
40 + getApplyFilterFn(item, col) {
41 + const res = op.getApplyFilterFn(item, col)
42 + return res && _.negate(res)
43 + },
44 + ...op.getApplyFilterFnV7 && { getApplyFilterFnV7(item, col) {
45 + const res = op.getApplyFilterFnV7?.(item, col)
46 + return res ? _.negate(res) : null
47 + } },
48 + label: "(not) " + ((localeText as any)['filterOperator' + _.upperFirst(op.value)] || op.value)
49 + } satisfies typeof op
50 + ]).flat()
51 const { mergeRender } = col
52 if (!mergeRender)
53 return col