| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115 |
- import { AutoComplete, Form, Input, Select } from "antd";
- import { useEffect, useState } from "react";
- import { useIntl } from "react-intl";
- import { useAppSelector } from "../../../hooks";
- import type { IApiResponseDictData } from "../../../api/Dict";
- import type { IWbw } from "./WbwWord";
- import { inlineDict as _inlineDict } from "../../../reducers/inline-dict";
- interface ValueType {
- key?: string;
- label: React.ReactNode;
- value: string | number;
- }
- interface IWidget {
- data: IWbw;
- onChange?: Function;
- }
- const WbwParent2Widget = ({ data, onChange }: IWidget) => {
- const intl = useIntl();
- const [parentOptions, setParentOptions] = useState<ValueType[]>([]);
- const inlineDict = useAppSelector(_inlineDict);
- const getParentInDict = (
- wordIn: string,
- wordIndex: string[],
- wordList: IApiResponseDictData[]
- ): string[] => {
- if (wordIndex.includes(wordIn)) {
- const result = wordList.filter((word) => word.word === wordIn);
- //查重
- //TODO 加入信心指数并排序
- const myMap = new Map<string, number>();
- const parent: string[] = [];
- for (const iterator of result) {
- if (iterator.parent) {
- myMap.set(iterator.parent, 1);
- }
- }
- myMap.forEach((_value, key, _map) => {
- parent.push(key);
- });
- return parent;
- } else {
- return [];
- }
- };
- useEffect(() => {
- if (typeof data.parent?.value !== "string") {
- return;
- }
- const parent = getParentInDict(
- data.parent.value,
- inlineDict.wordIndex,
- inlineDict.wordList
- );
- const parentOptions = parent.map((item) => {
- return {
- label: item,
- value: item,
- };
- });
- setParentOptions(parentOptions);
- }, [inlineDict, data]);
- const grammar = ["prp", "pp", "fpp", "pass", "caus", "vdn"];
- const options = grammar.map((item) => {
- return {
- value: `.${item}.`,
- label: intl.formatMessage({
- id: `dict.fields.type.${item}.label`,
- defaultMessage: item,
- }),
- };
- });
- return (
- <>
- <div style={{ display: "flex" }}>
- <Form.Item
- name="parent2"
- label={intl.formatMessage({ id: "forms.fields.parent2.label" })}
- tooltip={intl.formatMessage({
- id: "forms.fields.parent2.tooltip",
- })}
- >
- <AutoComplete
- options={parentOptions}
- onChange={(value: any) => {
- if (typeof onChange !== "undefined") {
- onChange({ field: "parent2", value: value });
- }
- }}
- >
- <Input allowClear />
- </AutoComplete>
- </Form.Item>
- <Form.Item name="grammar2" noStyle>
- <Select
- style={{ width: 100 }}
- allowClear
- options={options}
- onChange={(value: string) => {
- if (typeof onChange !== "undefined") {
- onChange({ field: "grammar2", value: value });
- }
- }}
- />
- </Form.Item>
- </div>
- </>
- );
- };
- export default WbwParent2Widget;
|