| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 |
- import { AutoComplete, Input } from "antd";
- import { useEffect, useState } from "react";
- import { useAppSelector } from "../../../hooks";
- import { inlineDict as _inlineDict } from "../../../reducers/inline-dict";
- import type { IWbw } from "./WbwWord";
- import type { IApiResponseDictData } from "../../../api/Dict";
- export 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 [];
- }
- };
- interface ValueType {
- key?: string;
- label: React.ReactNode;
- value: string | number;
- }
- interface IWidget {
- data: IWbw;
- readonly?: boolean;
- onChange?: Function;
- }
- const WbwDetailParentWidget = ({
- data,
- readonly = false,
- onChange,
- }: IWidget) => {
- const [parentOptions, setParentOptions] = useState<ValueType[]>([]);
- const inlineDict = useAppSelector(_inlineDict);
- useEffect(() => {
- if (!data.real.value) {
- return;
- }
- const parent = getParentInDict(
- data.word.value,
- inlineDict.wordIndex,
- inlineDict.wordList
- );
- const parentOptions = parent.map((item) => {
- return {
- label: item,
- value: item,
- };
- });
- const findParent = parentOptions.find(
- (value) => value.value === data.real.value
- );
- if (findParent) {
- setParentOptions(parentOptions);
- } else {
- setParentOptions([
- ...parentOptions,
- { label: data.real.value, value: data.real.value },
- ]);
- }
- }, [inlineDict, data]);
- return (
- <AutoComplete
- disabled={readonly}
- options={parentOptions}
- value={data.parent?.value}
- onChange={(value: any, _option: ValueType | ValueType[]) => {
- console.debug("wbw parent onChange", value);
- if (typeof onChange !== "undefined") {
- onChange(value);
- }
- }}
- >
- <Input disabled={readonly} allowClear placeholder="请输入" />
- </AutoComplete>
- );
- };
- export default WbwDetailParentWidget;
|