-
-
Notifications
You must be signed in to change notification settings - Fork 387
/
MRT_EditActionButtons.tsx
87 lines (82 loc) · 2.28 KB
/
MRT_EditActionButtons.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import Box from '@mui/material/Box';
import Button from '@mui/material/Button';
import IconButton from '@mui/material/IconButton';
import Tooltip from '@mui/material/Tooltip';
import { type MRT_Row, type MRT_TableInstance } from '../types';
interface Props<TData extends Record<string, any> = {}> {
row: MRT_Row<TData>;
table: MRT_TableInstance<TData>;
variant?: 'icon' | 'text';
}
export const MRT_EditActionButtons = <TData extends Record<string, any> = {}>({
row,
table,
variant = 'icon',
}: Props<TData>) => {
const {
getState,
options: {
icons: { CancelIcon, SaveIcon },
localization,
onEditingRowSave,
onEditingRowCancel,
},
refs: { editInputRefs },
setEditingRow,
} = table;
const { editingRow } = getState();
const handleCancel = () => {
onEditingRowCancel?.({ row, table });
setEditingRow(null);
};
const handleSave = () => {
//look for auto-filled input values
Object.values(editInputRefs?.current)?.forEach((input) => {
if (
input.value !== undefined &&
Object.hasOwn(editingRow?._valuesCache as object, input.name)
) {
// @ts-ignore
editingRow._valuesCache[input.name] = input.value;
}
});
onEditingRowSave?.({
exitEditingMode: () => setEditingRow(null),
row: editingRow ?? row,
table,
values: editingRow?._valuesCache ?? { ...row.original },
});
};
return (
<Box
onClick={(e) => e.stopPropagation()}
sx={{ display: 'flex', gap: '0.75rem' }}
>
{variant === 'icon' ? (
<>
<Tooltip arrow title={localization.cancel}>
<IconButton aria-label={localization.cancel} onClick={handleCancel}>
<CancelIcon />
</IconButton>
</Tooltip>
<Tooltip arrow title={localization.save}>
<IconButton
aria-label={localization.save}
color="info"
onClick={handleSave}
>
<SaveIcon />
</IconButton>
</Tooltip>
</>
) : (
<>
<Button onClick={handleCancel}>{localization.cancel}</Button>
<Button onClick={handleSave} variant="contained">
{localization.save}
</Button>
</>
)}
</Box>
);
};