WbwDetailParent2.tsx 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 type { IApiResponseDictData } from "../../../api/Dict";
  6. import type { 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. const myMap = new Map<string, number>();
  31. const parent: string[] = [];
  32. for (const iterator of result) {
  33. if (iterator.parent) {
  34. myMap.set(iterator.parent, 1);
  35. }
  36. }
  37. myMap.forEach((_value, key, _map) => {
  38. parent.push(key);
  39. });
  40. return parent;
  41. } else {
  42. return [];
  43. }
  44. };
  45. useEffect(() => {
  46. if (typeof data.parent?.value !== "string") {
  47. return;
  48. }
  49. const parent = getParentInDict(
  50. data.parent.value,
  51. inlineDict.wordIndex,
  52. inlineDict.wordList
  53. );
  54. const parentOptions = parent.map((item) => {
  55. return {
  56. label: item,
  57. value: item,
  58. };
  59. });
  60. setParentOptions(parentOptions);
  61. }, [inlineDict, data]);
  62. const grammar = ["prp", "pp", "fpp", "pass", "caus", "vdn"];
  63. const options = grammar.map((item) => {
  64. return {
  65. value: `.${item}.`,
  66. label: intl.formatMessage({
  67. id: `dict.fields.type.${item}.label`,
  68. defaultMessage: item,
  69. }),
  70. };
  71. });
  72. return (
  73. <>
  74. <div style={{ display: "flex" }}>
  75. <Form.Item
  76. name="parent2"
  77. label={intl.formatMessage({ id: "forms.fields.parent2.label" })}
  78. tooltip={intl.formatMessage({
  79. id: "forms.fields.parent2.tooltip",
  80. })}
  81. >
  82. <AutoComplete
  83. options={parentOptions}
  84. onChange={(value: any) => {
  85. if (typeof onChange !== "undefined") {
  86. onChange({ field: "parent2", value: value });
  87. }
  88. }}
  89. >
  90. <Input allowClear />
  91. </AutoComplete>
  92. </Form.Item>
  93. <Form.Item name="grammar2" noStyle>
  94. <Select
  95. style={{ width: 100 }}
  96. allowClear
  97. options={options}
  98. onChange={(value: string) => {
  99. if (typeof onChange !== "undefined") {
  100. onChange({ field: "grammar2", value: value });
  101. }
  102. }}
  103. />
  104. </Form.Item>
  105. </div>
  106. </>
  107. );
  108. };
  109. export default WbwParent2Widget;