54 lines
1.8 KiB
TypeScript
54 lines
1.8 KiB
TypeScript
import { api } from "@nice/client"
|
|
import { TrainContent } from "@nice/common"
|
|
import { useAuth } from "@web/src/providers/auth-provider"
|
|
import { TreeSelect } from "antd"
|
|
import { useMemo } from "react"
|
|
|
|
export default function TrainContentTreeSelect() {
|
|
const { user, isAuthenticated } = useAuth()
|
|
const { data: trainContents, isLoading: trainContentsLoading }
|
|
: { data: TrainContent[], isLoading: boolean }
|
|
= isAuthenticated ? api.trainContent.findMany.useQuery({
|
|
select: {
|
|
children: true,
|
|
id: true,
|
|
title: true,
|
|
parentId: true,
|
|
type: true,
|
|
parent: {
|
|
select: {
|
|
id: true,
|
|
title: true
|
|
}
|
|
}
|
|
}
|
|
}) : { data: null, isLoading: false }
|
|
const treeData = useMemo(() => {
|
|
if (!trainContents) return [];
|
|
|
|
const buildTreeData = (contents: any[], parentId: string | null = null): any[] => {
|
|
return contents
|
|
.filter(item => item.parentId === parentId)
|
|
.map(item => ({
|
|
title: item.title,
|
|
value: item.id,
|
|
key: item.id,
|
|
type: item.type,
|
|
children: buildTreeData(contents, item.id)
|
|
}))
|
|
.filter(Boolean);
|
|
};
|
|
|
|
return buildTreeData(trainContents);
|
|
}, [trainContents]);
|
|
return (
|
|
<TreeSelect
|
|
style={{ width: '100%' }}
|
|
dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
|
|
treeData={treeData}
|
|
placeholder="请选择学科或课程"
|
|
treeDefaultExpandAll={false}
|
|
//onChange={onChange}
|
|
/>
|
|
)
|
|
} |