assistant-todo-mobile/src/components/ParentTask/index.js

125 lines
3.7 KiB
JavaScript
Raw Normal View History

2024-12-27 06:06:20 -05:00
import {Cascader,Input, Toast} from "antd-mobile";
2024-12-29 03:54:01 -05:00
import React, {useEffect, useMemo, useState} from "react";
2024-12-27 06:06:20 -05:00
import {
Form,
} from 'antd-mobile'
2024-12-29 03:54:01 -05:00
import {getTaskByPid} from "../../utils";
2024-12-27 06:06:20 -05:00
2024-12-29 03:54:01 -05:00
const ParentTask = (props)=>{
const [valueToOptions, setValueToOptions] = useState([])
2024-12-27 06:06:20 -05:00
const [parentValue, setParentValue] = useState("")
const [visible, setVisible] = useState(false)
2024-12-29 03:54:01 -05:00
const {form} = props;
const options = useMemo(() => {
function generate(v) {
const options = valueToOptions[v]
if (options === null) {
return undefined
}
if (options === undefined) {
return Cascader.optionSkeleton
}
return options.map(option => ({
...option,
children: generate(option.value),
}))
}
return generate('0') ?? []
}, [valueToOptions])
async function fetchOptionsForValue(v, level) {
if (v in valueToOptions) return
// if (level >= 3) {
// setValueToOptions(prev => ({
// ...prev,
// [v]: null,
// }))
// return
// }
const data = await getTaskByPid(v)
2024-12-31 08:53:20 -05:00
console.log("await getTaskByPid(v)",data.content)
2024-12-29 03:54:01 -05:00
const options =
2024-12-31 08:53:20 -05:00
data.content.length === 0
2024-12-29 03:54:01 -05:00
? null
2024-12-31 08:53:20 -05:00
: data.content.map(task => ({
2024-12-29 03:54:01 -05:00
value: task.id,
label: task.name,
}))
console.log("await getTaskByPid(v) options",options)
setValueToOptions(prev => ({
...prev,
[v]: options,
}))
}
useEffect(() => {
fetchOptionsForValue('0', 0)
}, [])
2024-12-27 06:06:20 -05:00
return <Form.Item
2024-12-29 03:54:01 -05:00
name='pidArray'
2024-12-27 06:06:20 -05:00
label='主线任务'
onClick={() => {
setVisible(true)
}}
2024-12-29 03:54:01 -05:00
value={parentValue}
2024-12-27 06:06:20 -05:00
>
<Cascader
options={options}
visible={visible}
onClose={() => {
setVisible(false)
}}
value={parentValue}
2024-12-29 03:54:01 -05:00
onConfirm={(val)=>{
console.log(val)
setParentValue(val[val.length-1])
form.setFieldValue('pidArray',val)
}}
// onSelect={(val, extend) => {
// console.log('onSelect', val, extend.items)
// }}
onSelect={value => {
console.log("value",value)
value.forEach((v, index) => {
fetchOptionsForValue(v, index + 1)
})
2024-12-27 06:06:20 -05:00
}}
>
{items => {
if (items.every(item => item === null)) {
2024-12-29 03:54:01 -05:00
return <span style={{color:"#cccccc"}}>主线任务选填</span>
2024-12-27 06:06:20 -05:00
} else {
2024-12-29 03:54:01 -05:00
return items[items.length-1].label
2024-12-27 06:06:20 -05:00
}
}}
</Cascader>
</Form.Item>
// const [parentValue, setParentValue] = useState("")
// return <Form.Item
// name='name'
// label='主线任务'
// onClick={async () => {
// const value = await Cascader.prompt({
// options: options,
// placeholder: '请选择',
// })
// Toast.show(
// value ? `你选择了 ${value.join(' - ')}` : '你没有进行选择'
// )
// if (value){
// setParentValue(value.join(' - '))
// }
// }}
// >
// <Input
// defaultValue={parentValue}
// value={parentValue}
// readOnly={true}
// placeholder='主线任务名称选填'>
// {parentValue}
// </Input>
// </Form.Item>
}
export default ParentTask;