({ columns, rows, style, disabled = false, hideFooter = false, onChange })
| 34 | })) |
| 35 | |
| 36 | export const DataGrid = ({ columns, rows, style, disabled = false, hideFooter = false, onChange }) => { |
| 37 | const [rowValues, setRowValues] = useState(formatDataGridRows(rows) ?? []) |
| 38 | |
| 39 | const deleteItem = useCallback( |
| 40 | (id) => () => { |
| 41 | let updatedRows = [] |
| 42 | setRowValues((prevRows) => { |
| 43 | let allRows = [...cloneDeep(prevRows)] |
| 44 | allRows = allRows.filter((row) => row.id !== id) |
| 45 | updatedRows = allRows |
| 46 | return allRows |
| 47 | }) |
| 48 | onChange(JSON.stringify(updatedRows)) |
| 49 | }, |
| 50 | // eslint-disable-next-line react-hooks/exhaustive-deps |
| 51 | [] |
| 52 | ) |
| 53 | |
| 54 | const addCols = (columns) => { |
| 55 | return [ |
| 56 | ...columns, |
| 57 | { |
| 58 | field: 'actions', |
| 59 | type: 'actions', |
| 60 | width: 80, |
| 61 | getActions: (params) => [ |
| 62 | <GridActionsCellItem key={'Delete'} icon={<DeleteIcon />} label='Delete' onClick={deleteItem(params.id)} /> |
| 63 | ] |
| 64 | } |
| 65 | ] |
| 66 | } |
| 67 | |
| 68 | const colValues = addCols(columns) |
| 69 | |
| 70 | const handleProcessRowUpdate = (newRow) => { |
| 71 | let updatedRows = [] |
| 72 | setRowValues((prevRows) => { |
| 73 | let allRows = [...cloneDeep(prevRows)] |
| 74 | const indexToUpdate = allRows.findIndex((row) => row.id === newRow.id) |
| 75 | if (indexToUpdate >= 0) { |
| 76 | allRows[indexToUpdate] = { ...newRow } |
| 77 | } |
| 78 | updatedRows = allRows |
| 79 | return allRows |
| 80 | }) |
| 81 | onChange(JSON.stringify(updatedRows)) |
| 82 | return newRow |
| 83 | } |
| 84 | |
| 85 | const getEmptyJsonObj = () => { |
| 86 | const obj = {} |
| 87 | for (let i = 0; i < colValues.length; i += 1) { |
| 88 | obj[colValues[i]?.field] = '' |
| 89 | } |
| 90 | return obj |
| 91 | } |
| 92 | |
| 93 | const addNewRow = () => { |
nothing calls this directly
no test coverage detected