import {Cascader, Input, SearchBar, Toast} from "antd-mobile"; import React, {useEffect, useMemo, useState} from "react"; import { Form, } from 'antd-mobile' import {getTaskByPid} from "../../utils"; import "./index.css" import {CloseCircleFill} from "antd-mobile-icons"; const ParentTask = (props) => { const [valueToOptions, setValueToOptions] = useState([]) const {form, disabled, pName, pidArray, addEditPName} = props; const [parentValue, setParentValue] = useState(pidArray ?? []) const [visible, setVisible] = useState(false) const [searchValue, setSearchValue] = useState(""); const [currentLab, setCurrentLab] = useState(0); const [selectValue, setSelectValue] = useState([]); // 当前标签 // let currentLab=0; // let selectValue=[]; const options = useMemo(() => { console.log("useMemo") function generate(v) { const options = valueToOptions[v] if (options === null) { return undefined } if (options === undefined) { return Cascader.optionSkeleton } // 如果有搜索有值,需要过滤, // 根据currentLab查看需要过滤第几层,当前层可能没有值就会停留在上一层 if (searchValue && searchValue.length > 0) { if (selectValue.length <= currentLab + 1) { // 可能展示最新的,或没有停留在当前页面,正常是没有选择的时候搜索 console.log({searchValue}, {currentLab}, {selectValue}) if (currentLab === 0 && v === '0') { return options.filter(option => option.label.includes(searchValue)).map(option => ({ ...option, children: generate(option.value), })) } else if (v === selectValue[currentLab - 1]) { return options.filter(option => option.label.includes(searchValue)).map(option => ({ ...option, children: generate(option.value), })) } } else { // 用户切换回之前的标签,暂不处理 } } return options.map(option => ({ ...option, children: generate(option.value), })) } return generate('0') ?? [] }, [valueToOptions, searchValue]) async function fetchOptionsForValue(v, level) { if (v in valueToOptions) return // if (level >= 3) { // setValueToOptions(prev => ({ // ...prev, // [v]: null, // })) // return // } const data = await getTaskByPid(v) console.log("await getTaskByPid(v)", data.content) const options = data.content.length === 0 ? null : data.content.map(task => ({ value: task.id, label: task.name, })) console.log("await getTaskByPid(v) options", options) setValueToOptions(prev => ({ ...prev, [v]: options, })) } useEffect(() => { initDate() }, [props]) async function initDate() { await fetchOptionsForValue('0', 0) if (pidArray && pidArray.length > 0) { setParentValue(pidArray) for (const taskId of pidArray) { await fetchOptionsForValue(taskId, 0) } } } return