Shun Miyazawa 4 лет назад
Родитель
Сommit
cca78606a8
1 измененных файлов с 14 добавлено и 14 удалено
  1. 14 14
      packages/app/src/components/Page/TagsInput.tsx

+ 14 - 14
packages/app/src/components/Page/TagsInput.tsx

@@ -1,5 +1,5 @@
 import React, {
 import React, {
-  FC, useRef, useState,
+  FC, useRef, useState, useCallback,
 } from 'react';
 } from 'react';
 import { AsyncTypeahead } from 'react-bootstrap-typeahead';
 import { AsyncTypeahead } from 'react-bootstrap-typeahead';
 
 
@@ -8,7 +8,7 @@ import { toastError } from '~/client/util/apiNotification';
 import { ITagsSearchApiv1Result } from '~/interfaces/tag';
 import { ITagsSearchApiv1Result } from '~/interfaces/tag';
 
 
 type TypeaheadInstance = {
 type TypeaheadInstance = {
-  _handleMenuItemSelect: (activeItem: string, e: React.KeyboardEvent) => void,
+  _handleMenuItemSelect: (activeItem: string, event: React.KeyboardEvent) => void,
   state: {
   state: {
     initialItem: string,
     initialItem: string,
   },
   },
@@ -26,13 +26,13 @@ const TagsInput: FC<Props> = (props: Props) => {
   const [resultTags, setResultTags] = useState<string[]>([]);
   const [resultTags, setResultTags] = useState<string[]>([]);
   const [isLoading, setLoading] = useState(false);
   const [isLoading, setLoading] = useState(false);
 
 
-  const handleChange = (selected: string[]) => {
+  const changeHandler = useCallback((selected: string[]) => {
     if (props.onTagsUpdated != null) {
     if (props.onTagsUpdated != null) {
       props.onTagsUpdated(selected);
       props.onTagsUpdated(selected);
     }
     }
-  };
+  }, [props]);
 
 
-  const handleSearch = async(query: string) => {
+  const searchHandler = useCallback(async(query: string) => {
     setLoading(true);
     setLoading(true);
     try {
     try {
       // TODO: 91698 SWRize
       // TODO: 91698 SWRize
@@ -46,20 +46,20 @@ const TagsInput: FC<Props> = (props: Props) => {
     finally {
     finally {
       setLoading(false);
       setLoading(false);
     }
     }
-  };
+  }, []);
 
 
-  const handleSelect = (e: React.KeyboardEvent) => {
-    if (e.key === ' ') {
-      e.preventDefault();
+  const keyDownHandler = useCallback((event: React.KeyboardEvent) => {
+    if (event.key === ' ') {
+      event.preventDefault();
 
 
       const initialItem = tagsInputRef?.current?.state?.initialItem;
       const initialItem = tagsInputRef?.current?.state?.initialItem;
       const handleMenuItemSelect = tagsInputRef?.current?._handleMenuItemSelect;
       const handleMenuItemSelect = tagsInputRef?.current?._handleMenuItemSelect;
 
 
       if (initialItem != null && handleMenuItemSelect != null) {
       if (initialItem != null && handleMenuItemSelect != null) {
-        handleMenuItemSelect(initialItem, e);
+        handleMenuItemSelect(initialItem, event);
       }
       }
     }
     }
-  };
+  }, []);
 
 
   return (
   return (
     <div className="tag-typeahead">
     <div className="tag-typeahead">
@@ -72,9 +72,9 @@ const TagsInput: FC<Props> = (props: Props) => {
         minLength={1}
         minLength={1}
         multiple
         multiple
         newSelectionPrefix=""
         newSelectionPrefix=""
-        onChange={handleChange}
-        onSearch={handleSearch}
-        onKeyDown={handleSelect}
+        onChange={changeHandler}
+        onSearch={searchHandler}
+        onKeyDown={keyDownHandler}
         options={resultTags} // Search result (Some tag names)
         options={resultTags} // Search result (Some tag names)
         placeholder="tag name"
         placeholder="tag name"
         autoFocus={props.autoFocus}
         autoFocus={props.autoFocus}