00a3c8ca2b
* Change to using arrow functions Co-authored-by: v1b3m <vibenjamin6@gmail.com> Co-authored-by: Matheus <matheus_benini@hotmail.com> * Add lint rule --------- Co-authored-by: v1b3m <vibenjamin6@gmail.com> Co-authored-by: Matheus <matheus_benini@hotmail.com> Co-authored-by: Charles Bochet <charles@twenty.com>
48 lines
1.2 KiB
TypeScript
48 lines
1.2 KiB
TypeScript
import { useCurrentCellEditMode } from '../editable-cell/hooks/useCurrentCellEditMode';
|
|
import { useEditableCell } from '../editable-cell/hooks/useEditableCell';
|
|
|
|
import { useMoveSoftFocus } from './useMoveSoftFocus';
|
|
|
|
export const useCellInputEventHandlers = <T>({
|
|
onSubmit,
|
|
onCancel,
|
|
}: {
|
|
onSubmit?: (newValue: T) => void;
|
|
onCancel?: () => void;
|
|
}) => {
|
|
const { closeEditableCell } = useEditableCell();
|
|
const { isCurrentCellInEditMode } = useCurrentCellEditMode();
|
|
const { moveRight, moveLeft, moveDown } = useMoveSoftFocus();
|
|
|
|
return {
|
|
handleClickOutside: (event: MouseEvent | TouchEvent, newValue: T) => {
|
|
if (isCurrentCellInEditMode) {
|
|
event.stopImmediatePropagation();
|
|
|
|
onSubmit?.(newValue);
|
|
|
|
closeEditableCell();
|
|
}
|
|
},
|
|
handleEscape: () => {
|
|
closeEditableCell();
|
|
onCancel?.();
|
|
},
|
|
handleEnter: (newValue: T) => {
|
|
onSubmit?.(newValue);
|
|
closeEditableCell();
|
|
moveDown();
|
|
},
|
|
handleTab: (newValue: T) => {
|
|
onSubmit?.(newValue);
|
|
closeEditableCell();
|
|
moveRight();
|
|
},
|
|
handleShiftTab: (newValue: T) => {
|
|
onSubmit?.(newValue);
|
|
closeEditableCell();
|
|
moveLeft();
|
|
},
|
|
};
|
|
};
|