|
| 1 | +import React, { useMemo, useState } from 'react'; |
| 2 | +import { DataGrid, GridColDef, GridRowParams } from '@mui/x-data-grid'; |
| 3 | +import { Box, Button, Paper, TextField } from '@mui/material'; |
| 4 | +import type { SxProps } from '@mui/system'; |
| 5 | +import type { Theme } from '@mui/material/styles'; |
| 6 | + |
| 7 | +export type MapRowResult<T> = T & { id: string | number; raw?: T }; |
| 8 | + |
| 9 | +interface NERDataGridProps<T> { |
| 10 | + items: T[]; |
| 11 | + // map an item to a row object (must include `id` and may include `raw`) |
| 12 | + mapRow: (item: T) => MapRowResult<T>; |
| 13 | + columns: GridColDef[]; |
| 14 | + pageSizeDefault?: number; |
| 15 | + rowsPerPageOptions?: number[]; |
| 16 | + onAdd: () => void; |
| 17 | + onRowClick?: (item: T) => void; |
| 18 | + // optional simple search fields (keys of mapped row) or a custom filter function |
| 19 | + searchFields?: (keyof MapRowResult<T>)[]; |
| 20 | + searchFilter?: (term: string, row: MapRowResult<T>) => boolean; |
| 21 | + // optional sort model to apply initially |
| 22 | + initialSortModel?: { field: string; sort: 'asc' | 'desc' }[]; |
| 23 | + headerHeight?: number; |
| 24 | + rowHeight?: number; |
| 25 | + paperSx?: SxProps<Theme>; |
| 26 | + canEditRow?: (row: MapRowResult<T>) => boolean; |
| 27 | +} |
| 28 | + |
| 29 | +function NERDataGrid<T>({ |
| 30 | + items, |
| 31 | + mapRow, |
| 32 | + columns, |
| 33 | + pageSizeDefault = 10, |
| 34 | + rowsPerPageOptions, |
| 35 | + onAdd, |
| 36 | + onRowClick, |
| 37 | + searchFields, |
| 38 | + searchFilter, |
| 39 | + initialSortModel = [{ field: 'name', sort: 'asc' }], |
| 40 | + headerHeight = 56, |
| 41 | + rowHeight = 52, |
| 42 | + paperSx, |
| 43 | + canEditRow |
| 44 | +}: NERDataGridProps<T>) { |
| 45 | + const [searchTerm, setSearchTerm] = useState(''); |
| 46 | + const [pageSize, setPageSize] = useState<number>(pageSizeDefault); |
| 47 | + |
| 48 | + // compute standard pagination options if caller didn't pass any |
| 49 | + const effectiveRowsPerPageOptions = useMemo(() => { |
| 50 | + const base = [5, 10, 25, 50, 100]; |
| 51 | + const itemsCount = (items ?? []).length; |
| 52 | + if (rowsPerPageOptions && rowsPerPageOptions.length > 0) { |
| 53 | + const provided = Array.from(new Set(rowsPerPageOptions)) |
| 54 | + .sort((a, b) => a - b) |
| 55 | + .filter((opt) => opt <= itemsCount); |
| 56 | + if (provided.length > 0) return provided; |
| 57 | + return itemsCount > 0 ? [itemsCount] : base; |
| 58 | + } |
| 59 | + |
| 60 | + // default behavior: include standard options up to the total number of items |
| 61 | + if (itemsCount === 0) return base; |
| 62 | + const opts = base.filter((opt) => opt <= itemsCount); |
| 63 | + if (itemsCount < 100 && !opts.includes(itemsCount)) opts.push(itemsCount); |
| 64 | + return Array.from(new Set(opts)).sort((a, b) => a - b); |
| 65 | + }, [rowsPerPageOptions, items]); |
| 66 | + |
| 67 | + const rows = useMemo(() => items.map(mapRow), [items, mapRow]); |
| 68 | + |
| 69 | + const filteredRows = useMemo(() => { |
| 70 | + const term = searchTerm.trim().toLowerCase(); |
| 71 | + if (!term) return rows; |
| 72 | + if (searchFilter) return rows.filter((r) => searchFilter(term, r)); |
| 73 | + if (searchFields && searchFields.length > 0) { |
| 74 | + return rows.filter((r) => |
| 75 | + searchFields.some((f) => |
| 76 | + String(((r as MapRowResult<T>)[f] as unknown) ?? '') |
| 77 | + .toLowerCase() |
| 78 | + .includes(term) |
| 79 | + ) |
| 80 | + ); |
| 81 | + } |
| 82 | + return rows.filter((r) => JSON.stringify(r).toLowerCase().includes(term)); |
| 83 | + }, [rows, searchTerm, searchFields, searchFilter]); |
| 84 | + |
| 85 | + return ( |
| 86 | + <Box> |
| 87 | + <Paper |
| 88 | + sx={{ borderRadius: '10px 10px 0 0', overflow: 'hidden', display: 'flex', flexDirection: 'column', ...paperSx }} |
| 89 | + > |
| 90 | + <Box sx={{ display: 'flex', alignItems: 'center', gap: 2, px: 2, py: 0.5, height: 48 }}> |
| 91 | + <TextField |
| 92 | + value={searchTerm} |
| 93 | + onChange={(e) => setSearchTerm(e.target.value)} |
| 94 | + size="small" |
| 95 | + placeholder="Search" |
| 96 | + sx={{ flex: 1 }} |
| 97 | + /> |
| 98 | + <Button variant="contained" size="small" onClick={onAdd} sx={{ ml: 1 }}> |
| 99 | + Add |
| 100 | + </Button> |
| 101 | + </Box> |
| 102 | + |
| 103 | + <Box sx={{ flex: 1, minHeight: 0 }}> |
| 104 | + <DataGrid |
| 105 | + rows={filteredRows} |
| 106 | + columns={columns} |
| 107 | + initialState={{ sorting: { sortModel: initialSortModel } }} |
| 108 | + pageSize={pageSize} |
| 109 | + onPageSizeChange={(newSize) => setPageSize(newSize)} |
| 110 | + rowsPerPageOptions={effectiveRowsPerPageOptions} |
| 111 | + pagination |
| 112 | + disableSelectionOnClick |
| 113 | + headerHeight={headerHeight} |
| 114 | + rowHeight={rowHeight} |
| 115 | + onRowClick={(params: GridRowParams<MapRowResult<T>>) => { |
| 116 | + if (!onRowClick) return; |
| 117 | + const row = params.row as MapRowResult<T>; |
| 118 | + const editable = canEditRow ? canEditRow(row) : true; |
| 119 | + if (!editable) return; // do not call onRowClick for non-editable rows |
| 120 | + const raw = params.row.raw as T | undefined; |
| 121 | + if (raw) onRowClick(raw); |
| 122 | + }} |
| 123 | + getRowClassName={(params) => { |
| 124 | + const row = params.row as MapRowResult<T>; |
| 125 | + const editable = canEditRow ? canEditRow(row) : true; |
| 126 | + return editable ? 'editable-row' : 'non-editable-row'; |
| 127 | + }} |
| 128 | + sx={{ |
| 129 | + height: '100%', |
| 130 | + '& .MuiDataGrid-columnHeaders': { |
| 131 | + backgroundColor: '#ef4345', |
| 132 | + color: 'white', |
| 133 | + fontWeight: 'bold' |
| 134 | + }, |
| 135 | + '& .MuiDataGrid-columnHeader': { |
| 136 | + borderTopLeftRadius: 10, |
| 137 | + borderTopRightRadius: 10 |
| 138 | + }, |
| 139 | + // per-row hover: mark editable rows with pointer and non-editable rows with default |
| 140 | + '& .editable-row:hover': { |
| 141 | + cursor: onRowClick ? 'pointer' : 'default' |
| 142 | + }, |
| 143 | + '& .non-editable-row:hover': { |
| 144 | + cursor: 'default' |
| 145 | + }, |
| 146 | + '& .non-editable-row': { |
| 147 | + opacity: 0.7 |
| 148 | + }, |
| 149 | + '& .MuiDataGrid-columnSeparator': { |
| 150 | + display: 'none' |
| 151 | + } |
| 152 | + }} |
| 153 | + /> |
| 154 | + </Box> |
| 155 | + </Paper> |
| 156 | + </Box> |
| 157 | + ); |
| 158 | +} |
| 159 | + |
| 160 | +export default NERDataGrid; |
0 commit comments