* renamed editable field to inline cell in ui folder * renamed table to table-cell in ui folder
68 lines
2.6 KiB
TypeScript
68 lines
2.6 KiB
TypeScript
import { useContext } from 'react';
|
|
import { useSetRecoilState } from 'recoil';
|
|
|
|
import { contextMenuIsOpenState } from '@/ui/context-menu/states/contextMenuIsOpenState';
|
|
import { contextMenuPositionState } from '@/ui/context-menu/states/contextMenuPositionState';
|
|
import { FieldContext } from '@/ui/field/contexts/FieldContext';
|
|
import { isFieldRelation } from '@/ui/field/types/guards/isFieldRelation';
|
|
import { RelationPickerHotkeyScope } from '@/ui/input/relation-picker/types/RelationPickerHotkeyScope';
|
|
import { RecoilScope } from '@/ui/utilities/recoil-scope/components/RecoilScope';
|
|
|
|
import { ColumnContext } from '../contexts/ColumnContext';
|
|
import { ColumnIndexContext } from '../contexts/ColumnIndexContext';
|
|
import { EntityUpdateMutationContext } from '../contexts/EntityUpdateMutationHookContext';
|
|
import { RowIdContext } from '../contexts/RowIdContext';
|
|
import { useCurrentRowSelected } from '../hooks/useCurrentRowSelected';
|
|
import { TableCell } from '../table-cell/components/TableCell';
|
|
import { TableHotkeyScope } from '../types/TableHotkeyScope';
|
|
|
|
export const EntityTableCell = ({ cellIndex }: { cellIndex: number }) => {
|
|
const setContextMenuPosition = useSetRecoilState(contextMenuPositionState);
|
|
const setContextMenuOpenState = useSetRecoilState(contextMenuIsOpenState);
|
|
const currentRowId = useContext(RowIdContext);
|
|
|
|
const { setCurrentRowSelected } = useCurrentRowSelected();
|
|
|
|
const handleContextMenu = (event: React.MouseEvent) => {
|
|
event.preventDefault();
|
|
setCurrentRowSelected(true);
|
|
setContextMenuPosition({
|
|
x: event.clientX,
|
|
y: event.clientY,
|
|
});
|
|
setContextMenuOpenState(true);
|
|
};
|
|
|
|
const columnDefinition = useContext(ColumnContext);
|
|
|
|
const updateEntityMutation = useContext(EntityUpdateMutationContext);
|
|
|
|
if (!columnDefinition || !currentRowId) {
|
|
return null;
|
|
}
|
|
|
|
const customHotkeyScope = isFieldRelation(columnDefinition)
|
|
? RelationPickerHotkeyScope.RelationPicker
|
|
: TableHotkeyScope.CellEditMode;
|
|
|
|
return (
|
|
<RecoilScope>
|
|
<ColumnIndexContext.Provider value={cellIndex}>
|
|
<td onContextMenu={(event) => handleContextMenu(event)}>
|
|
<FieldContext.Provider
|
|
value={{
|
|
recoilScopeId: currentRowId + columnDefinition.name,
|
|
entityId: currentRowId,
|
|
fieldDefinition: columnDefinition,
|
|
useUpdateEntityMutation: () => [updateEntityMutation, {}],
|
|
hotkeyScope: customHotkeyScope,
|
|
}}
|
|
>
|
|
<TableCell customHotkeyScope={{ scope: customHotkeyScope }} />
|
|
</FieldContext.Provider>
|
|
</td>
|
|
</ColumnIndexContext.Provider>
|
|
</RecoilScope>
|
|
);
|
|
};
|