import Box, { type BoxProps } from '@mui/material/Box'; import Button from '@mui/material/Button'; import CircularProgress from '@mui/material/CircularProgress'; import IconButton from '@mui/material/IconButton'; import Tooltip from '@mui/material/Tooltip'; import { type MRT_Row, type MRT_RowData, type MRT_TableInstance, } from '../../types'; import { parseFromValuesOrFunc } from '../../utils/utils'; export interface MRT_EditActionButtonsProps extends BoxProps { row: MRT_Row; table: MRT_TableInstance; variant?: 'icon' | 'text'; } export const MRT_EditActionButtons = ({ row, table, variant = 'icon', ...rest }: MRT_EditActionButtonsProps) => { const { getState, options: { icons: { CancelIcon, SaveIcon }, localization, onCreatingRowCancel, onCreatingRowSave, onEditingRowCancel, onEditingRowSave, }, refs: { editInputRefs }, setCreatingRow, setEditingRow, } = table; const { creatingRow, editingRow, isSaving } = getState(); const isCreating = creatingRow?.id === row.id; const isEditing = editingRow?.id === row.id; const handleCancel = () => { if (isCreating) { onCreatingRowCancel?.({ row, table }); setCreatingRow(null); } else if (isEditing) { onEditingRowCancel?.({ row, table }); setEditingRow(null); } row._valuesCache = {} as any; //reset values cache }; const handleSubmitRow = () => { //look for auto-filled input values Object.values(editInputRefs.current ?? {}) .filter((inputRef) => row.id === inputRef?.name?.split('_')?.[0]) ?.forEach((input) => { if ( input.value !== undefined && Object.hasOwn(row?._valuesCache as object, input.name) ) { // @ts-expect-error row._valuesCache[input.name] = input.value; } }); if (isCreating) onCreatingRowSave?.({ exitCreatingMode: () => setCreatingRow(null), row, table, values: row._valuesCache, }); else if (isEditing) { onEditingRowSave?.({ exitEditingMode: () => setEditingRow(null), row, table, values: row?._valuesCache, }); } }; return ( e.stopPropagation()} sx={(theme) => ({ display: 'flex', gap: '0.75rem', ...(parseFromValuesOrFunc(rest?.sx, theme) as any), })} > {variant === 'icon' ? ( <> {((isCreating && onCreatingRowSave) || (isEditing && onEditingRowSave)) && ( {isSaving ? : } )} ) : ( <> )} ); };