Просмотр исходного кода

the enter key handler does not move when a warning appears

Shun Miyazawa 4 лет назад
Родитель
Сommit
681874d1a3
1 измененных файлов с 6 добавлено и 6 удалено
  1. 6 6
      packages/app/src/components/Common/ClosableTextInput.tsx

+ 6 - 6
packages/app/src/components/Common/ClosableTextInput.tsx

@@ -29,6 +29,7 @@ const ClosableTextInput: FC<ClosableTextInputProps> = memo((props: ClosableTextI
   const inputRef = useRef<HTMLInputElement>(null);
 
   const [inputText, setInputText] = useState(props.value);
+  const [canKeyDownHandler, setCanKeyDownHandler] = useState(false);
   const [currentAlertInfo, setAlertInfo] = useState<AlertInfo | null>(null);
 
   const onChangeHandler = async(e) => {
@@ -38,6 +39,9 @@ const ClosableTextInput: FC<ClosableTextInputProps> = memo((props: ClosableTextI
 
     const alertInfo = await props.inputValidator(inputText);
 
+    // eslint-disable-next-line no-unused-expressions
+    alertInfo == null ? setCanKeyDownHandler(true) : setCanKeyDownHandler(false);
+
     setAlertInfo(alertInfo);
     setInputText(inputText);
   };
@@ -53,12 +57,8 @@ const ClosableTextInput: FC<ClosableTextInputProps> = memo((props: ClosableTextI
   };
 
   const onKeyDownHandler = (e) => {
-    switch (e.key) {
-      case 'Enter':
-        onPressEnter();
-        break;
-      default:
-        break;
+    if (canKeyDownHandler && e.key === 'Enter') {
+      onPressEnter();
     }
   };