Bladeren bron

fix(dict): sync search input box when lookup word changes

The SearchVocabulary input state was only initialized once from the
value prop, so clicking a second word updated the dictionary results
but left the input box showing the previous word. Sync the input state
with the value prop via useEffect.
visuddhinanda 5 dagen geleden
bovenliggende
commit
c59495f682
1 gewijzigde bestanden met toevoegingen van 7 en 1 verwijderingen
  1. 7 1
      dashboard-v6/src/components/dict/SearchVocabulary.tsx

+ 7 - 1
dashboard-v6/src/components/dict/SearchVocabulary.tsx

@@ -1,6 +1,6 @@
 import { get } from "../../request";
 import type { IVocabularyListResponse } from "../../api/dict";
-import { useRef, useState } from "react";
+import { useEffect, useRef, useState } from "react";
 import { AutoComplete, Input, Space, Typography } from "antd";
 import { DictIcon } from "../../assets/icon";
 
@@ -32,6 +32,12 @@ const SearchVocabulary = ({
   const [factors, setFactors] = useState<string[]>([]);
   const intervalRef = useRef<number | null>(null);
 
+  // 外部 value(点击单词触发的查词)变化时,同步输入框显示。
+  // 仅在 value 真正变化时同步,用户手动输入时 value 不会变,因此不会打断输入。
+  useEffect(() => {
+    setInput(value);
+  }, [value]);
+
   const renderItem = (title: string, count: number, meaning?: string) => ({
     value: title,
     label: (