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

144 lines
4.4 KiB
JavaScript
Raw Normal View History

2025-01-23 06:32:24 -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
2025-01-23 06:32:24 -05:00
const ParentTask = (props) => {
2024-12-29 03:54:01 -05:00
const [valueToOptions, setValueToOptions] = useState([])
2025-01-23 06:32:24 -05:00
const {form, disabled, pName, pidArray, addEditPName} = props;
const [parentValue, setParentValue] = useState(pidArray ?? [])
2024-12-27 06:06:20 -05:00
const [visible, setVisible] = useState(false)
2025-01-11 04:53:51 -05:00
2024-12-29 03:54:01 -05:00
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),
}))
}
2025-01-23 06:32:24 -05:00
2024-12-29 03:54:01 -05:00
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)
2025-01-23 06:32:24 -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,
}))
2025-01-23 06:32:24 -05:00
console.log("await getTaskByPid(v) options", options)
2024-12-29 03:54:01 -05:00
setValueToOptions(prev => ({
...prev,
[v]: options,
}))
}
useEffect(() => {
2025-01-23 06:32:24 -05:00
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)
}
}
}
2024-12-29 03:54:01 -05:00
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}
2025-01-11 04:53:51 -05:00
disabled={disabled}
2024-12-27 06:06:20 -05:00
>
<Cascader
options={options}
visible={visible}
2025-01-23 06:32:24 -05:00
defaultValue={pidArray}
2024-12-27 06:06:20 -05:00
onClose={() => {
setVisible(false)
}}
value={parentValue}
2025-01-23 06:32:24 -05:00
onConfirm={(val) => {
2024-12-29 03:54:01 -05:00
console.log(val)
2025-01-23 06:32:24 -05:00
setParentValue(val[val.length - 1])
form.setFieldValue('pidArray', val)
2024-12-29 03:54:01 -05:00
}}
// onSelect={(val, extend) => {
// console.log('onSelect', val, extend.items)
// }}
onSelect={value => {
2025-01-23 06:32:24 -05:00
console.log("value", value)
2024-12-29 03:54:01 -05:00
value.forEach((v, index) => {
fetchOptionsForValue(v, index + 1)
})
2024-12-27 06:06:20 -05:00
}}
>
{items => {
if (items.every(item => item === null)) {
2025-01-23 06:32:24 -05:00
return pName ? (<span>{pName}</span>) : disabled ?
(<span>主线任务选填</span>) :
2025-01-11 04:53:51 -05:00
(<span style={{color: "#cccccc"}}>主线任务选填</span>)
2024-12-27 06:06:20 -05:00
} else {
2025-01-23 06:32:24 -05:00
if (addEditPName) {
addEditPName(items[items.length - 1].label)
}
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;