|
| 1 | +import { grey } from '@mui/material/colors'; |
| 2 | +import { alpha } from '@mui/material/styles'; |
| 3 | +import { DataGrid, type DataGridProps, type GridValidRowModel } from '@mui/x-data-grid'; |
| 4 | +import { SkeletonTable } from 'components/loading/SkeletonLoaders'; |
| 5 | +import React, { useCallback } from 'react'; |
| 6 | +import StyledDataGridOverlay from './StyledDataGridOverlay'; |
| 7 | + |
| 8 | +export type ICustomDataGridProps<R extends GridValidRowModel = GridValidRowModel> = DataGridProps<R> & { |
| 9 | + noRowsMessage?: string; |
| 10 | + noRowsOverlay?: React.ReactElement; |
| 11 | +}; |
| 12 | + |
| 13 | +/** |
| 14 | + * Standardized DataGrid wrapper that applies shared table styles while preserving full MUI DataGrid API support. |
| 15 | + */ |
| 16 | +const CustomDataGrid = <R extends GridValidRowModel = GridValidRowModel>(props: ICustomDataGridProps<R>) => { |
| 17 | + const { sx, noRowsMessage, noRowsOverlay, slots, ...rest } = props; |
| 18 | + |
| 19 | + const loadingOverlay = () => <SkeletonTable />; |
| 20 | + |
| 21 | + const defaultNoRowsOverlay = useCallback( |
| 22 | + () => noRowsOverlay ?? <StyledDataGridOverlay message={noRowsMessage} />, |
| 23 | + [noRowsMessage, noRowsOverlay] |
| 24 | + ); |
| 25 | + |
| 26 | + return ( |
| 27 | + <DataGrid |
| 28 | + {...rest} |
| 29 | + disableColumnMenu={true} |
| 30 | + disableColumnResize={true} |
| 31 | + slots={{ |
| 32 | + loadingOverlay, |
| 33 | + noRowsOverlay: defaultNoRowsOverlay, |
| 34 | + ...slots |
| 35 | + }} |
| 36 | + sx={[ |
| 37 | + (theme) => { |
| 38 | + return { |
| 39 | + height: '100%', |
| 40 | + bgcolor: theme.palette.background.paper, |
| 41 | + display: 'flex', |
| 42 | + flexDirection: 'column', |
| 43 | + borderRadius: 2, |
| 44 | + border: 'none !important', |
| 45 | + |
| 46 | + '& *:focus-within': { |
| 47 | + outline: 'none !important' |
| 48 | + }, |
| 49 | + |
| 50 | + '& .MuiDataGrid-columnHeaders': { |
| 51 | + bgcolor: theme.palette.background.paper, |
| 52 | + minHeight: 56, |
| 53 | + height: 56, |
| 54 | + maxHeight: 56 |
| 55 | + }, |
| 56 | + '& .MuiDataGrid-columnHeader': { |
| 57 | + bgcolor: theme.palette.background.paper, |
| 58 | + borderBottom: `1px solid ${theme.palette.divider} !important`, |
| 59 | + minHeight: 56, |
| 60 | + height: 56, |
| 61 | + maxHeight: 56, |
| 62 | + px: 2, |
| 63 | + alignItems: 'center' |
| 64 | + }, |
| 65 | + '& .MuiDataGrid-columnHeaderTitleContainer': { |
| 66 | + minHeight: 56, |
| 67 | + height: 56, |
| 68 | + maxHeight: 56, |
| 69 | + minWidth: 72 |
| 70 | + }, |
| 71 | + '& .MuiDataGrid-columnHeaderTitleContainerContent': { |
| 72 | + justifyContent: 'flex-start', |
| 73 | + width: '100%', |
| 74 | + minWidth: 0 |
| 75 | + }, |
| 76 | + '& .MuiDataGrid-columnHeaderTitle': { |
| 77 | + textTransform: 'uppercase', |
| 78 | + fontWeight: 700, |
| 79 | + fontSize: '0.7rem', |
| 80 | + color: theme.palette.text.secondary |
| 81 | + }, |
| 82 | + '& .MuiDataGrid-columnSeparator': { |
| 83 | + display: 'none' |
| 84 | + }, |
| 85 | + '& .MuiDataGrid-sortIcon, & .MuiDataGrid-iconButtonContainer .MuiSvgIcon-root': { |
| 86 | + color: grey[500] |
| 87 | + }, |
| 88 | + |
| 89 | + '& .MuiDataGrid-cell:not(.MuiDataGrid-cellCheckbox) > *': { |
| 90 | + minWidth: 0, |
| 91 | + overflow: 'hidden', |
| 92 | + whiteSpace: 'nowrap', |
| 93 | + textOverflow: 'ellipsis' |
| 94 | + }, |
| 95 | + |
| 96 | + '& .MuiDataGrid-row': { |
| 97 | + bgcolor: theme.palette.background.paper, |
| 98 | + borderBottom: `1px solid ${theme.palette.divider}`, |
| 99 | + borderTop: 'none', |
| 100 | + borderRight: `1px solid ${theme.palette.divider}`, |
| 101 | + cursor: rest.onRowClick ? 'pointer' : 'default' |
| 102 | + }, |
| 103 | + '& .MuiDataGrid-row.Mui-selected': { |
| 104 | + bgcolor: `${alpha(theme.palette.primary.main, 0.14)} !important` |
| 105 | + }, |
| 106 | + '& .MuiDataGrid-row.Mui-selected:hover': { |
| 107 | + bgcolor: `${alpha(theme.palette.primary.main, 0.18)} !important` |
| 108 | + }, |
| 109 | + '& .MuiDataGrid-row:hover:not(.Mui-selected)': { |
| 110 | + bgcolor: theme.palette.action.hover |
| 111 | + }, |
| 112 | + |
| 113 | + '& .MuiDataGrid-cell': { |
| 114 | + bgcolor: 'inherit', |
| 115 | + border: 'none', |
| 116 | + display: 'flex', |
| 117 | + alignItems: 'center', |
| 118 | + justifyContent: 'flex-start', |
| 119 | + textAlign: 'left', |
| 120 | + px: 2, |
| 121 | + fontSize: '0.85rem !important', |
| 122 | + minHeight: '0 !important', |
| 123 | + overflow: 'hidden' |
| 124 | + }, |
| 125 | + |
| 126 | + '& .MuiDataGrid-cellContent': { |
| 127 | + display: 'flex', |
| 128 | + alignItems: 'center', |
| 129 | + justifyContent: 'flex-start', |
| 130 | + textAlign: 'left', |
| 131 | + width: '100%', |
| 132 | + minWidth: 0, |
| 133 | + overflow: 'hidden', |
| 134 | + whiteSpace: 'nowrap', |
| 135 | + textOverflow: 'ellipsis' |
| 136 | + }, |
| 137 | + |
| 138 | + '& .MuiDataGrid-cell .MuiTypography-root': { |
| 139 | + fontSize: '0.85rem !important', |
| 140 | + minWidth: 0, |
| 141 | + overflow: 'hidden', |
| 142 | + whiteSpace: 'nowrap', |
| 143 | + textOverflow: 'ellipsis' |
| 144 | + }, |
| 145 | + |
| 146 | + '& .MuiDataGrid-cellEmpty': { |
| 147 | + padding: '0 !important' |
| 148 | + }, |
| 149 | + |
| 150 | + '& .MuiDataGrid-footerContainer': { |
| 151 | + bgcolor: theme.palette.background.paper, |
| 152 | + borderTop: `1px solid ${theme.palette.divider}` |
| 153 | + }, |
| 154 | + '& .MuiDataGrid-virtualScroller': { |
| 155 | + bgcolor: theme.palette.background.paper |
| 156 | + }, |
| 157 | + '& .MuiDataGrid-main': { |
| 158 | + overflow: 'hidden' |
| 159 | + }, |
| 160 | + '& .MuiDataGrid-row:last-of-type .MuiDataGrid-cell': { |
| 161 | + borderBottom: 'none' |
| 162 | + }, |
| 163 | + |
| 164 | + '&.MuiDataGrid-root--densityCompact .MuiDataGrid-row': { |
| 165 | + padding: '4px', |
| 166 | + minHeight: 100 |
| 167 | + } |
| 168 | + }; |
| 169 | + }, |
| 170 | + ...(Array.isArray(sx) ? sx : [sx]) |
| 171 | + ]} |
| 172 | + /> |
| 173 | + ); |
| 174 | +}; |
| 175 | + |
| 176 | +export default CustomDataGrid; |
0 commit comments