86133 | aaa3f5d | 2025-04-20 21:33:29 +0800 | [diff] [blame] | 1 | import React, { useState, useEffect } from 'react'; |
| 2 | import { Tree, message } from 'antd'; |
| 3 | import { getDeptTree } from '@/services/system/user'; |
| 4 | |
| 5 | const { DirectoryTree } = Tree; |
| 6 | |
| 7 | /* * |
| 8 | * |
| 9 | * @author whiteshader@163.com |
| 10 | * @datetime 2023/02/06 |
| 11 | * |
| 12 | * */ |
| 13 | |
| 14 | |
| 15 | export type TreeProps = { |
| 16 | onSelect: (values: any) => Promise<void>; |
| 17 | }; |
| 18 | |
| 19 | const DeptTree: React.FC<TreeProps> = (props) => { |
| 20 | const [treeData, setTreeData] = useState<any>([]); |
| 21 | const [expandedKeys, setExpandedKeys] = useState<React.Key[]>([]); |
| 22 | const [autoExpandParent, setAutoExpandParent] = useState<boolean>(true); |
| 23 | |
| 24 | const fetchDeptList = async () => { |
| 25 | const hide = message.loading('正在查询'); |
| 26 | try { |
| 27 | await getDeptTree({}).then((res: any) => { |
| 28 | const exKeys = []; |
| 29 | exKeys.push('1'); |
| 30 | setTreeData(res); |
| 31 | exKeys.push(res[0].children[0].id); |
| 32 | setExpandedKeys(exKeys); |
| 33 | props.onSelect(res[0].children[0]); |
| 34 | }); |
| 35 | hide(); |
| 36 | return true; |
| 37 | } catch (error) { |
| 38 | hide(); |
| 39 | return false; |
| 40 | } |
| 41 | }; |
| 42 | |
| 43 | useEffect(() => { |
| 44 | fetchDeptList(); |
| 45 | }, []); |
| 46 | |
| 47 | const onSelect = (keys: React.Key[], info: any) => { |
| 48 | props.onSelect(info.node); |
| 49 | }; |
| 50 | |
| 51 | const onExpand = (expandedKeysValue: React.Key[]) => { |
| 52 | setExpandedKeys(expandedKeysValue); |
| 53 | setAutoExpandParent(false); |
| 54 | }; |
| 55 | |
| 56 | return ( |
| 57 | <DirectoryTree |
| 58 | // multiple |
| 59 | defaultExpandAll |
| 60 | onExpand={onExpand} |
| 61 | expandedKeys={expandedKeys} |
| 62 | autoExpandParent={autoExpandParent} |
| 63 | onSelect={onSelect} |
| 64 | treeData={treeData} |
| 65 | /> |
| 66 | ); |
| 67 | }; |
| 68 | |
| 69 | export default DeptTree; |