DictComponent.tsx 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. import { useState, useEffect } from "react";
  2. import { useAppSelector } from "../../hooks";
  3. import { lookup, lookupWord, myDictIsDirty } from "../../reducers/command";
  4. import store from "../../store";
  5. import Dictionary from "./Dictionary";
  6. import { notification } from "antd";
  7. export interface IWidgetDict {
  8. word?: string;
  9. }
  10. const DictComponentWidget = ({ word }: IWidgetDict) => {
  11. const [wordSearch, setWordSearch] = useState(word);
  12. //接收查字典消息
  13. const searchWord = useAppSelector(lookupWord);
  14. const myDictDirty = useAppSelector(myDictIsDirty);
  15. useEffect(() => {
  16. console.log("get command", searchWord);
  17. if (typeof searchWord === "string" && searchWord !== wordSearch) {
  18. if (myDictDirty) {
  19. notification.warning({
  20. message: "用户词典有未保存内容,请保存后再查词",
  21. });
  22. return;
  23. }
  24. setWordSearch(searchWord);
  25. }
  26. }, [searchWord, myDictDirty, wordSearch]);
  27. return (
  28. <Dictionary
  29. word={wordSearch}
  30. compact={true}
  31. onSearch={(value: string) => {
  32. console.debug("onSearch", value);
  33. store.dispatch(lookup(value));
  34. }}
  35. />
  36. );
  37. };
  38. export default DictComponentWidget;