WbwDetailFactor.tsx 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. import { AutoComplete, Input } from "antd";
  2. import { useEffect, useState } from "react";
  3. import { useAppSelector } from "../../../hooks";
  4. import {
  5. add,
  6. inlineDict as _inlineDict,
  7. wordIndex,
  8. } from "../../../reducers/inline-dict";
  9. import { get } from "../../../request";
  10. import store from "../../../store";
  11. import { IApiResponseDictList } from "../../api/Dict";
  12. import { getFactorsInDict } from "./WbwFactors";
  13. import { IWbw } from "./WbwWord";
  14. interface ValueType {
  15. key?: string;
  16. label: React.ReactNode;
  17. value: string | number;
  18. }
  19. interface IWidget {
  20. data: IWbw;
  21. onChange?: Function;
  22. }
  23. const WbwDetailFactorWidget = ({ data, onChange }: IWidget) => {
  24. const [factorOptions, setFactorOptions] = useState<ValueType[]>([]);
  25. const inlineDict = useAppSelector(_inlineDict);
  26. const inlineWordIndex = useAppSelector(wordIndex);
  27. const lookup = (words: string[]) => {
  28. //查询这个词在内存字典里是否有
  29. let search: string[] = [];
  30. for (const it of words) {
  31. if (!inlineWordIndex.includes(it)) {
  32. search.push(it);
  33. }
  34. }
  35. if (search.length === 0) {
  36. return;
  37. }
  38. get<IApiResponseDictList>(`/v2/wbwlookup?base=${search}`).then((json) => {
  39. console.log("lookup ok", json.data.count);
  40. store.dispatch(add(json.data.rows));
  41. });
  42. };
  43. useEffect(() => {
  44. if (
  45. typeof data.factors === "undefined" ||
  46. typeof data.factors.value !== "string"
  47. ) {
  48. return;
  49. }
  50. lookup(data.factors?.value.split("+"));
  51. }, [data.factors]);
  52. useEffect(() => {
  53. const factors = getFactorsInDict(
  54. data.real.value,
  55. inlineDict.wordIndex,
  56. inlineDict.wordList
  57. );
  58. const options = factors.map((item) => {
  59. return {
  60. label: item,
  61. value: item,
  62. };
  63. });
  64. setFactorOptions(options);
  65. }, [data.real.value, inlineDict.wordIndex, inlineDict.wordList]);
  66. return (
  67. <AutoComplete
  68. options={factorOptions}
  69. defaultValue={data.factors?.value}
  70. onChange={(value: string) => {
  71. if (typeof onChange !== "undefined") {
  72. onChange(value);
  73. }
  74. }}
  75. >
  76. <Input allowClear />
  77. </AutoComplete>
  78. );
  79. };
  80. export default WbwDetailFactorWidget;