import { type FocusEvent, type KeyboardEvent, useState } from 'react'; import { MultiSelect, type MultiSelectProps, Select, type SelectProps, TextInput, type TextInputProps, } from '@mantine/core'; import { type HTMLPropsRef, type MRT_Cell, type MRT_CellValue, type MRT_RowData, type MRT_TableInstance, } from '../../types'; import { parseFromValuesOrFunc } from '../../utils/utils'; interface PropsTextInput extends TextInputProps { cell: MRT_Cell; table: MRT_TableInstance; } interface PropsSelect extends SelectProps { cell: MRT_Cell; table: MRT_TableInstance; } interface PropsMultiSelect extends MultiSelectProps { cell: MRT_Cell; table: MRT_TableInstance; } type MRT_TextInputProps = HTMLPropsRef & TextInputProps; type MRT_SelectProps = HTMLPropsRef & SelectProps; type MRT_MultiSelectProps = HTMLPropsRef & MultiSelectProps; export const MRT_EditCellTextInput = ({ cell, table, ...rest }: PropsMultiSelect | PropsSelect | PropsTextInput) => { 'use no memo'; const { getState, options: { createDisplayMode, editDisplayMode, mantineEditSelectProps, mantineEditTextInputProps, }, refs: { editInputRefs }, setCreatingRow, setEditingCell, setEditingRow, } = table; const { column, row } = cell; const { columnDef } = column; const { creatingRow, editingRow } = getState(); const isCreating = creatingRow?.id === row.id; const isEditing = editingRow?.id === row.id; const isSelectEdit = columnDef.editVariant === 'select'; const isMultiSelectEdit = columnDef.editVariant === 'multi-select'; const [value, setValue] = useState(() => cell.getValue()); const arg = { cell, column, row, table }; const textInputProps = { ...parseFromValuesOrFunc(mantineEditTextInputProps, arg), ...parseFromValuesOrFunc(columnDef.mantineEditTextInputProps, arg), ...rest, } as MRT_TextInputProps; const selectProps = { ...parseFromValuesOrFunc(mantineEditSelectProps, arg), ...parseFromValuesOrFunc(columnDef.mantineEditSelectProps, arg), ...rest, }; const saveInputValueToRowCache = (newValue: null | string) => { //@ts-ignore row._valuesCache[column.id] = newValue; if (isCreating) { setCreatingRow(row); } else if (isEditing) { setEditingRow(row); } }; const handleBlur = (event: FocusEvent) => { textInputProps.onBlur?.(event); saveInputValueToRowCache(value); setEditingCell(null); }; const handleEnterKeyDown = (event: KeyboardEvent) => { textInputProps.onKeyDown?.(event); if (event.key === 'Enter') { editInputRefs.current[cell.id]?.blur(); } }; if (columnDef.Edit) { return columnDef.Edit?.({ cell, column, row, table }); } const commonProps = { disabled: parseFromValuesOrFunc(columnDef.enableEditing, row) === false, label: ['custom', 'modal'].includes( (isCreating ? createDisplayMode : editDisplayMode) as string, ) ? column.columnDef.header : undefined, name: cell.id, onClick: (e: any) => { e.stopPropagation(); textInputProps?.onClick?.(e); }, placeholder: !['custom', 'modal'].includes( (isCreating ? createDisplayMode : editDisplayMode) as string, ) ? columnDef.header : undefined, value, variant: editDisplayMode === 'table' ? 'unstyled' : 'default', } as const; if (isSelectEdit) { return (