/* * @Author: your name * @Date: 2021-09-06 10:28:12 * @LastEditTime: 2021-10-18 10:41:23 * @LastEditors: Please set LastEditors * @Description: In User Settings Edit * @FilePath: /MedicalWisdomCheckSys/src/components/MccsFileTree/index.tsx */ import React, { ReactNode, useEffect, useState, MouseEvent } from 'react' import { Input, Spin, Image, Popconfirm } from 'antd'; import { PlusOutlined } from '@ant-design/icons'; import DirectoryTree from './components/DirectoryTree'; import './index.less' import DelActionIcon from './images/del14px.png'; import EditActionIcon from './images/edit14px.png'; import AddActionIcon from './images/add.png'; import FileIcon from './images/file.png'; // const { DirectoryTree } = Tree; const { Search } = Input; //树形文件组件 enum Types { 'add', 'del', 'edit', 'search' }; type TypeVals = keyof typeof Types; type MccsFileTreeProps = { onSelectHandle?: (data: MccsFileTree.childTree) => void, //选中回调 actionHandle?: ({ type, data }: { type: string, data: MccsFileTree.childTree }) => void, //操作回调,包括编辑/删除/新增/搜索 searchHandle?: (val: any) => void, switcherIcon?: ReactNode, treeData: MccsFileTree.childTree[] | [], defaultSelected?: string, //传id formContent?: ReactNode, editable?: boolean } const MccsFileTree: React.FC = (props) => { const { treeData, onSelectHandle, actionHandle, defaultSelected, searchHandle, editable } = props; const [isLoading, setIsLoading] = useState(true); //当前选中的 const [currentActivedIndex, setcurrentActivedIndex] = useState(defaultSelected ? defaultSelected : ''); const actionFunc = (event:MouseEvent|undefined, type: TypeVals, data: MccsFileTree.childTree) => { event?.stopPropagation(); actionHandle && actionHandle({ type, data: data }); } //操作 const Action = (props: MccsFileTree.childTree) => { const { isLeaf } = props; return (
{ !isLeaf && ( //非叶子结点才可以新增 <> actionFunc(e, 'add', props)} src={AddActionIcon} />
) } actionFunc(e, 'edit', props)} src={EditActionIcon} />
{actionFunc(e, 'del', props)}} okText="确定" cancelText="取消" > e.stopPropagation()} src={DelActionIcon} />
) } //叶子结点结构 const TreeNode = (nodeProps: MccsFileTree.childTree) => { const { title, id,code } = nodeProps; return (
{ setcurrentActivedIndex(id); onSelectHandle(nodeProps) } : () => { setcurrentActivedIndex(id); }}>
{`${code} ${title}`}
{(currentActivedIndex == id && editable) && /*点击展示操作项*/}
) } //递归树形结构 const loop = (data: MccsFileTree.childTree, i: number) => { const { title, children = [], ...restProps } = data; const label =
{`${restProps.code} ${title}`}
; if (data.isLeaf) { return } return ( onSelectHandle ? onSelectHandle(data) : () => { }} action={editable && } defaultCollapsed={true} > { children.map((item, index) => { if (item.isLeaf) { return } else { return loop(item, index); } }) } ) } useEffect(() => { if (treeData.length > 0) { setIsLoading(false); } }, [treeData]); return (
{ editable && (
actionFunc(e, 'add', { title: '', id: `0`, isLeaf: false, code: `${new Date().getTime()}`, children: [] })}>
) } searchHandle && searchHandle(val)} />
{ isLoading ?
: ( <> {treeData.map((node, i) => { return loop(node, i); })} ) }
); }; export default MccsFileTree