WbwDetailParent2.tsx 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. import { AutoComplete, Form, Input, Select } from "antd";
  2. import { useEffect, useState } from "react";
  3. import { useIntl } from "react-intl";
  4. import { useAppSelector } from "../../../hooks";
  5. import { IApiResponseDictData } from "../../api/Dict";
  6. import { IWbw } from "./WbwWord";
  7. import { inlineDict as _inlineDict } from "../../../reducers/inline-dict";
  8. interface ValueType {
  9. key?: string;
  10. label: React.ReactNode;
  11. value: string | number;
  12. }
  13. interface IWidget {
  14. data: IWbw;
  15. onChange?: Function;
  16. }
  17. const WbwParent2Widget = ({ data, onChange }: IWidget) => {
  18. const intl = useIntl();
  19. const [parentOptions, setParentOptions] = useState<ValueType[]>([]);
  20. const inlineDict = useAppSelector(_inlineDict);
  21. const getParentInDict = (
  22. wordIn: string,
  23. wordIndex: string[],
  24. wordList: IApiResponseDictData[]
  25. ): string[] => {
  26. if (wordIndex.includes(wordIn)) {
  27. const result = wordList.filter((word) => word.word === wordIn);
  28. //查重
  29. //TODO 加入信心指数并排序
  30. let myMap = new Map<string, number>();
  31. let parent: string[] = [];
  32. for (const iterator of result) {
  33. myMap.set(iterator.parent, 1);
  34. }
  35. myMap.forEach((value, key, map) => {
  36. parent.push(key);
  37. });
  38. return parent;
  39. } else {
  40. return [];
  41. }
  42. };
  43. useEffect(() => {
  44. if (typeof data.parent?.value !== "string") {
  45. return;
  46. }
  47. const parent = getParentInDict(
  48. data.parent.value,
  49. inlineDict.wordIndex,
  50. inlineDict.wordList
  51. );
  52. const parentOptions = parent.map((item) => {
  53. return {
  54. label: item,
  55. value: item,
  56. };
  57. });
  58. setParentOptions(parentOptions);
  59. }, [inlineDict, data]);
  60. const grammar = ["prp", "pp", "fpp", "pass", "caus"];
  61. const options = grammar.map((item) => {
  62. return {
  63. value: `.${item}.`,
  64. label: intl.formatMessage({ id: `dict.fields.type.${item}.label` }),
  65. };
  66. });
  67. return (
  68. <>
  69. <div style={{ display: "flex" }}>
  70. <Form.Item
  71. name="parent2"
  72. label={intl.formatMessage({ id: "forms.fields.parent2.label" })}
  73. tooltip={intl.formatMessage({
  74. id: "forms.fields.parent2.tooltip",
  75. })}
  76. >
  77. <AutoComplete
  78. options={parentOptions}
  79. onChange={(value: any) => {
  80. if (typeof onChange !== "undefined") {
  81. onChange({ field: "parent2", value: value });
  82. }
  83. }}
  84. >
  85. <Input allowClear />
  86. </AutoComplete>
  87. </Form.Item>
  88. <Form.Item name="grammar2" noStyle>
  89. <Select
  90. style={{ width: 100 }}
  91. allowClear
  92. options={options}
  93. onChange={(value: string) => {
  94. if (typeof onChange !== "undefined") {
  95. onChange({ field: "grammar2", value: value });
  96. }
  97. }}
  98. />
  99. </Form.Item>
  100. </div>
  101. </>
  102. );
  103. };
  104. export default WbwParent2Widget;