WbwDetailFactor.tsx 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 type { IApiResponseDictList } from "../../../api/Dict";
  12. import { getFactorsInDict } from "./WbwFactors";
  13. import type { 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. readonly?: boolean;
  22. onChange?: Function;
  23. }
  24. const WbwDetailFactorWidget = ({
  25. data,
  26. readonly = false,
  27. onChange,
  28. }: IWidget) => {
  29. const [factorOptions, setFactorOptions] = useState<ValueType[]>([]);
  30. const inlineDict = useAppSelector(_inlineDict);
  31. const inlineWordIndex = useAppSelector(wordIndex);
  32. const lookup = (words: string[]) => {
  33. //查询这个词在内存字典里是否有
  34. const search: string[] = [];
  35. for (const it of words) {
  36. if (!inlineWordIndex.includes(it)) {
  37. search.push(it);
  38. }
  39. }
  40. if (search.length === 0) {
  41. return;
  42. }
  43. get<IApiResponseDictList>(`/v2/wbwlookup?base=${search}`).then((json) => {
  44. console.log("lookup ok", json.data.count);
  45. store.dispatch(add(json.data.rows));
  46. });
  47. };
  48. useEffect(() => {
  49. if (
  50. typeof data.factors === "undefined" ||
  51. typeof data.factors.value !== "string"
  52. ) {
  53. return;
  54. }
  55. lookup(data.factors?.value.replaceAll("-", "+").split("+"));
  56. }, [data.factors]);
  57. useEffect(() => {
  58. if (!data.real.value) {
  59. return;
  60. }
  61. const factors = getFactorsInDict(
  62. data.real.value,
  63. inlineDict.wordIndex,
  64. inlineDict.wordList
  65. );
  66. const options = factors.map((item) => {
  67. return {
  68. label: item,
  69. value: item,
  70. };
  71. });
  72. setFactorOptions([
  73. ...options,
  74. { label: data.real.value, value: data.real.value },
  75. ]);
  76. }, [data.real.value, inlineDict.wordIndex, inlineDict.wordList]);
  77. return (
  78. <AutoComplete
  79. disabled={readonly}
  80. options={factorOptions}
  81. value={data.factors?.value}
  82. onChange={(value: string) => {
  83. if (typeof onChange !== "undefined") {
  84. onChange(value);
  85. }
  86. }}
  87. >
  88. <Input disabled={readonly} placeholder="请输入" allowClear />
  89. </AutoComplete>
  90. );
  91. };
  92. export default WbwDetailFactorWidget;