Skip to content

Commit

Permalink
fix(frontend): fix edit test name behavior (#3570)
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgeepc authored Jan 26, 2024
1 parent 4e3c2c6 commit edfebce
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 21 deletions.
9 changes: 6 additions & 3 deletions web/src/components/Inputs/Overlay/Overlay.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {useEffect, useRef, useState} from 'react';
import {useCallback, useEffect, useRef, useState} from 'react';
import {Input} from 'antd';
import useInputActions from 'hooks/useInputActions';
import {noop} from 'lodash';
Expand All @@ -14,14 +14,17 @@ const Overlay = ({onChange = noop, value = '', isDisabled = false}: IProps) => {
const [isOpen, setIsOpen] = useState(false);
const [inputValue, setInputValue] = useState(value);
const ref = useRef(null);
useInputActions(ref, () => {

const handler = useCallback(() => {
setIsOpen(false);
if (inputValue) {
onChange(inputValue);
} else {
setInputValue(value);
}
});
}, [inputValue, onChange, value]);

useInputActions(ref, handler);

useEffect(() => {
setInputValue(value);
Expand Down
31 changes: 13 additions & 18 deletions web/src/hooks/useInputActions.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,30 @@
import {RefObject, useCallback, useEffect} from 'react';
import {RefObject, useEffect} from 'react';
import useOnClickOutside from './useOnClickOutside';

type Handler = (event: MouseEvent | KeyboardEvent) => void;

const useInputActions = <T extends HTMLElement = HTMLElement>(ref: RefObject<T>, handler: Handler) => {
useOnClickOutside(ref, handler);
useOnClickOutside(ref, handler, 'mousedown');

const handleKeyDown = useCallback(
(event: KeyboardEvent) => {
useEffect(() => {
const element = ref.current;
if (!element) return;

const handleKeyDown = (event: KeyboardEvent) => {
if (event.key === 'Escape') {
handler(event);
}

if (event.key === 'Enter') {
handler(event);
}
},
[handler]
);

useEffect(() => {
const element = ref.current;

if (element) {
element.addEventListener('keydown', handleKeyDown);
};

return () => {
element.removeEventListener('keydown', handleKeyDown);
};
}
}, [handleKeyDown, ref]);
element.addEventListener('keydown', handleKeyDown);
return () => {
element.removeEventListener('keydown', handleKeyDown);
};
}, [handler, ref]);
};

export default useInputActions;

0 comments on commit edfebce

Please sign in to comment.