import { Badge, Button, Card, Checkbox, Select, Space, Typography } from "antd"; import { DownOutlined, UpOutlined } from "@ant-design/icons"; import { useEffect, useState } from "react"; import { get } from "../../request"; import type { ICaseItem, ICaseListResponse } from "../../api/Dict"; import { CheckboxValueType } from "antd/lib/checkbox/Group"; import type { CheckboxChangeEvent } from "antd/es/checkbox"; const { Text } = Typography; interface IWidget { word?: string; lines?: number; onChange?: (checkedList: string[]) => void; } const CaseListWidget = ({ word, lines, onChange }: IWidget) => { const [caseData, setCaseData] = useState(); const [showAll, setShowAll] = useState(lines ? false : true); const [words, setWords] = useState(); const [currWord, setCurrWord] = useState(); const [checkedList, setCheckedList] = useState([]); useEffect(() => { setCaseData( words ?.find((value) => value.word === currWord) ?.case.sort((a, b) => b.count - a.count) ); }, [currWord, words]); useEffect(() => { if (typeof onChange !== "undefined" && checkedList.length > 0) { onChange(checkedList); } }, [checkedList]); useEffect(() => { if (caseData) { setCheckedList(caseData?.map((item) => item.word)); } else { setCheckedList([]); } }, [caseData]); useEffect(() => { /** * 搜索变格 * 如果 keyWord 包涵空格 不搜索 */ if (typeof word === "undefined") { return; } if (word?.trim().includes(" ")) { setWords([]); setCurrWord(undefined); return; } get(`/v2/case/${word}`).then((json) => { console.log("case", json); if (json.ok && json.data.rows.length > 0) { setWords(json.data.rows); const first = json.data.rows.sort((a, b) => b.count - a.count)[0]; setCurrWord(first.word); } }); }, [word]); let checkAll = true; let indeterminate = false; if (caseData && checkedList) { checkAll = caseData?.length === checkedList?.length; indeterminate = checkedList.length > 0 && checkedList.length < caseData.length; } const onWordChange = (list: CheckboxValueType[]) => { setCheckedList(list.map((item) => item.toString())); }; const onCheckAllChange = (e: CheckboxChangeEvent) => { if (caseData) { setCheckedList( e.target.checked ? caseData?.map((item) => item.word) : [] ); } else { setCheckedList([]); } }; const showWords = showAll ? caseData : caseData?.slice(0, lines); return (
{currWord ? ( setShowAll(!showAll)}> {showAll ? ( {"折叠"} ) : ( {"展开"} )} ) : ( <> ) } title={