* Improve test coverage and refactor storybook arch * Fix coverage * Fix tests * Fix lint * Fix lint
45 lines
1.1 KiB
TypeScript
45 lines
1.1 KiB
TypeScript
import { useListenClickOutsideArrayOfRef } from '@/ui/hooks/useListenClickOutsideArrayOfRef';
|
|
import { useScopedHotkeys } from '@/ui/hotkey/hooks/useScopedHotkeys';
|
|
|
|
import { EditableFieldHotkeyScope } from '../types/EditableFieldHotkeyScope';
|
|
|
|
import { useEditableField } from './useEditableField';
|
|
|
|
export function useRegisterCloseFieldHandlers(
|
|
wrapperRef: React.RefObject<HTMLDivElement>,
|
|
onSubmit?: () => void,
|
|
onCancel?: () => void,
|
|
) {
|
|
const { closeEditableField, isFieldInEditMode } = useEditableField();
|
|
|
|
useListenClickOutsideArrayOfRef({
|
|
refs: [wrapperRef],
|
|
callback: () => {
|
|
if (isFieldInEditMode) {
|
|
onSubmit?.();
|
|
closeEditableField();
|
|
}
|
|
},
|
|
});
|
|
|
|
useScopedHotkeys(
|
|
'enter',
|
|
() => {
|
|
onSubmit?.();
|
|
closeEditableField();
|
|
},
|
|
EditableFieldHotkeyScope.EditableField,
|
|
[closeEditableField, onSubmit],
|
|
);
|
|
|
|
useScopedHotkeys(
|
|
'esc',
|
|
() => {
|
|
closeEditableField();
|
|
onCancel?.();
|
|
},
|
|
EditableFieldHotkeyScope.EditableField,
|
|
[closeEditableField, onCancel],
|
|
);
|
|
}
|