* Removed view field duplicate types * wip * wip 2 * wip 3 * Unified state for fields * Renaming * Wip * Post merge * Post post merge * wip * Delete unused file * Boolean and Probability * Finished InlineCell * Renamed EditableCell to TableCell * Finished double texts * Finished MoneyField * Fixed bug inline cell click outside * Fixed hotkey scope * Final fixes * Phone * Fix url and number input validation * Fix * Fix position * wip refactor activity editor * Fixed activity editor --------- Co-authored-by: Charles Bochet <charles@twenty.com>
70 lines
1.4 KiB
TypeScript
70 lines
1.4 KiB
TypeScript
import { useScopedHotkeys } from '@/ui/utilities/hotkey/hooks/useScopedHotkeys';
|
|
import { useListenClickOutside } from '@/ui/utilities/pointer-event/hooks/useListenClickOutside';
|
|
import { isDefined } from '~/utils/isDefined';
|
|
|
|
export const useRegisterInputEvents = <T>({
|
|
inputRef,
|
|
inputValue,
|
|
onEscape,
|
|
onEnter,
|
|
onTab,
|
|
onShiftTab,
|
|
onClickOutside,
|
|
hotkeyScope,
|
|
}: {
|
|
inputRef: React.RefObject<any>;
|
|
inputValue: T;
|
|
onEscape: (inputValue: T) => void;
|
|
onEnter: (inputValue: T) => void;
|
|
onTab?: (inputValue: T) => void;
|
|
onShiftTab?: (inputValue: T) => void;
|
|
onClickOutside?: (event: MouseEvent | TouchEvent, inputValue: T) => void;
|
|
hotkeyScope: string;
|
|
}) => {
|
|
useListenClickOutside({
|
|
refs: [inputRef],
|
|
callback: (event) => {
|
|
event.stopImmediatePropagation();
|
|
|
|
onClickOutside?.(event, inputValue);
|
|
},
|
|
enabled: isDefined(onClickOutside),
|
|
});
|
|
|
|
useScopedHotkeys(
|
|
'enter',
|
|
() => {
|
|
onEnter?.(inputValue);
|
|
},
|
|
hotkeyScope,
|
|
[onEnter, inputValue],
|
|
);
|
|
|
|
useScopedHotkeys(
|
|
'esc',
|
|
() => {
|
|
onEscape?.(inputValue);
|
|
},
|
|
hotkeyScope,
|
|
[onEscape, inputValue],
|
|
);
|
|
|
|
useScopedHotkeys(
|
|
'tab',
|
|
() => {
|
|
onTab?.(inputValue);
|
|
},
|
|
hotkeyScope,
|
|
[onTab, inputValue],
|
|
);
|
|
|
|
useScopedHotkeys(
|
|
'shift+tab',
|
|
() => {
|
|
onShiftTab?.(inputValue);
|
|
},
|
|
hotkeyScope,
|
|
[onShiftTab, inputValue],
|
|
);
|
|
};
|