0.3.8 • Published 4 years ago

@hysight/tree v0.3.8

Weekly downloads
1
License
ISC
Repository
github
Last release
4 years ago

hysight-tree

npm 发布方法

    npm set registry http://192.168.1.207:5000
    npm adduser --registry http://192.168.1.207:5000
    npm publish 

Examples

import Tree from '@hysight/tree';

const {TreeNode, TreeMore} = Tree;
const {MenuItem} = ContextMenu;
or
class App extends Component {
    constructor(props, context) {

        super(props, context);
        this.state = {
            treeNodeData: []
        };
        this.onDragEnter = this.onDragEnter.bind(this);
        this.onDrop = this.onDrop.bind(this);
        this.onSelect = this.onSelect.bind(this);

    }

    componentDidMount() {

        this.handleFetch();

    }

    handleFetch() {

        const url = 'http://192.168.1.207:4000/hysightdata/dataset/project/16ce8f13f6994abd9b3dc8fbde8caf7a/tree';

        fetchUrl(url).then((res) => {

            if(res.code === 0) {

                return;

            }

            this.setState({
                treeNodeData: res.result
            });

        });

    }

    onDragEnter(info) {

        console.log('onDragEnter---->', info);

    }

    onDrop(info) {

        console.log('onDrop---->', info);

        const {id: dropKey, pid: targetParentId, type: targetType} = info.node.props;
        const {id: dragKey, pid: sourceParentId, type: sourceType} = info.dragNode.props;
        // this.handleMoveFolder(dragKey, dropKey);
        // const dragNodesKeys = info.dragNodesKeys;
        const loop = (data, id, callback) => {

            data.forEach((item, index, arr) => {

                if(item.id === id) {

                    return callback(item, index, arr);

                }
                if(item.children) {

                    return loop(item.children, id, callback);

                }

            });

        };
        const data = [...this.state.treeNodeData];
        let dragObj;
        loop(data, dragKey, (item, index, arr) => {

            arr.splice(index, 1);
            // dragObj = item;
            dragObj = Object.assign(
                {},
                {...item},
                {pid: info.dragOverGapTop || info.dragOverGapBottom ? targetParentId : dropKey});

        });
        if(info.dragOverGapTop) {

            let ar;
            let i;
            loop(data, dropKey, (item, index, arr) => {

                ar = arr;
                i = index;

            });
            ar.splice(i, 0, dragObj);

        } else if(info.dragOverGapBottom) {

            let ar;
            let i;
            loop(data, dropKey, (item, index, arr) => {

                ar = arr;
                i = index + 1;

            });
            ar.splice(i, 0, dragObj);

        } else {

            loop(data, dropKey, (item) => {

                item.children = item.children || [];
                // where to insert 示例添加到尾部,可以是随意位置
                item.children.push(dragObj);

            });

        }
        this.setState({
            treeNodeData: data
        });

    }

    onSelect(selectedKeys, e) {

        console.log('都是非法链接', selectedKeys, e);

    }

    toRenderTreeNodes() {

        const loop = data => data.map((item) => {

            const {children, id, name, type, pid, dataSourceId} = item;
            if(item.children && item.children.length) {

                // <TreeMore onClick={() => console.log(111)} />
                return (
                    <TreeNode
                        key={id}
                        title={name}
                        draggable={true}
                        isLeaf={type !== 'dir'}
                        isMore={type === 'dir'}
                        moreComponent={
                            <ContextMenu
                                eventTrigger={['onClick']}
                                render={() => <TreeMore />}
                            >
                                <MenuItem onClick={() => console.log(1)} >测试1</MenuItem>
                                <MenuItem onClick={() => console.log(2)} >测试2</MenuItem>
                                <MenuItem onClick={() => console.log(3)} >测试3</MenuItem>
                            </ContextMenu>
                        }
                        isPrefixIcon={false}
                        prefixIconComponent={
                            <ContextMenu
                                eventTrigger={['onClick']}
                                render={() => {

                                    return <TreeMore />;

                                }}
                            >
                                <MenuItem onClick={(event) => console.log(4, event)} >测试1</MenuItem>
                                <MenuItem onClick={() => console.log(5)} >测试2</MenuItem>
                                <MenuItem onClick={() => console.log(6)} >测试3</MenuItem>
                            </ContextMenu>
                        }
                        // icon={type}
                        id={id}
                        pid={pid}
                    >
                        {loop(children)}
                    </TreeNode>
                );

            }
            return (
                <TreeNode
                    key={id}
                    title={name}
                    isLeaf={type !== 'dir'}
                    draggable={false}
                    isMore={type !== 'dir'}
                    moreComponent={
                        <ContextMenu
                            eventTrigger={['onClick']}
                            render={() => {

                                return <TreeMore />;

                            }}
                        >
                            <MenuItem onClick={(event) => console.log(4, event)} >测试1</MenuItem>
                            <MenuItem onClick={() => console.log(5)} >测试2</MenuItem>
                            <MenuItem onClick={() => console.log(6)} >测试3</MenuItem>
                        </ContextMenu>
                    }
                    isPrefixIcon={true}
                    prefixIconComponent={
                        <ContextMenu
                            eventTrigger={['onClick']}
                            render={() => {

                                return <TreeMore />;

                            }}
                        >
                            <MenuItem onClick={(event) => console.log(4, event)} >测试1</MenuItem>
                            <MenuItem onClick={() => console.log(5)} >测试2</MenuItem>
                            <MenuItem onClick={() => console.log(6)} >测试3</MenuItem>
                        </ContextMenu>
                    }
                    // icon={type}
                    id={id}
                    pid={pid}
                    dbId={dataSourceId}
                />
            );

        });
        return (
            <Tree
                className='draggable-tree'
                defaultExpandedKeys={this.state.expandedKeys}
                // draggable={true}
                disabledProviderEvents={true}
                onDrop={this.onDrop}
                onRightClick={() => console.log('onRightClick')}
                onExpand={() => console.log('onExpand')}
                // onSelect={this.onSelect}
                onDragStart={(info) => console.log('onDragStart', info)}
                onDragEnter={this.onDragEnter}
                onDragOver={() => console.log('onDragOver')}
                onDragLeave={() => console.log('onDragLeave')}
                onDragEnd={() => console.log('onDragEnd')}
                onMoreClick={() => console.log('onMoreClick')}
            >
                {loop(this.state.treeNodeData)}
            </Tree>
        );

    }
    render() {

        const {treeNodeData} = this.state;
        if(!treeNodeData.length) {

            return null;

        }
        return (
            <div>
                {this.toRenderTreeNodes()}
            </div>
        );

    }
}

export default App;

Usage

    npm install @hysight/tree --save --registry=http://192.168.1.207:5000
    or
    yarn add @hysight/tree --registry=http://192.168.1.207:5000
    then
    import Tree from '@hysight/tree'

Tree Props

PropDefaultTypeDescription
classNamestring根组件className
defaultExpandAllfalsebool首次记载默认是否展开所有父节点
defaultExpandedKeys[]array首次加载默认展开的节点id
expandedKeys[]array(受控)展开指定的树节点
autoExpandParentfalsebool(受控)是否自动展开父节点
selectedKeys[]array(受控)设置选中的树节点
onExpandfunc(expandedKeys, {expanded: bool, node})func展开/收起节点时触发
onSelectfunc(selectedKeys, event)func点击树节点触发
onDoubleClickfunc(selectedKeys, event, isLeaf)func双击点击树节点触发(onDoubleClick会触发onSelect事件,最好不要同时使用)
onRightClickfunc({event, event})func响应右键点击
draggablefalsebool设置节点可拖拽(IE>8)
onDragStartfunc({event, node})func开始拖拽时调用
onDragEnterfunc({event, node})funcdragenter 触发时调用
onDragOverfunc({event, node})funconDragOver 触发时调用
onDragLeavefunc({event, node})funconDragLeave 触发时调用
onDragEndfunc({event, node})funconDragEnd 触发时调用
onDropfunc({event, node, dragNode, dragNodesKeys, dropPosition, ?dragOverGapTop, ?dragOverGapBottom})funconDrop 触发时调用
disabledProviderEventsfalsebool默认不禁止Tree组件中提供onDrag/onDrop前缀事件,否则禁止
iconEyefalsebool/array:'icon-buyincang', 'icon-yincang'是否节点前显示眼睛,默认不显示眼睛,传递数组(2)可自定义眼睛className
onEyeChangefunc(selectedEyeKey, {selected, node})func切换眼睛状态回调函数

TreeNode Props

PropDefaultTypeDescription
classNamestringTreeNode节点上className名称
title'---'string/ReactNode标题
promptstring标题提示,不设置默认为 title
key内部计算出的节点位置string被树的 (default)ExpandedKeys / (default)CheckedKeys / (default)SelectedKeys 属性所用。注意:整个树范围内的所有节点的 key 值不能重复!
isLeaffalsebool设置为叶子节点,假如强制设置true,则会把属于文件夹的TreeNode设置成不可展开的节点,并且图标默认是文件格式
isMorefalsebool设置是否限制鼠标浮动时候显示图标更多,if true 配合moreComponent props一起使用
moreComponent'---'node/element(react)if isMore true, 图标后缀组件内容
isPrefixIconfalsebool设置是否限制鼠标浮动时候显示其他图标组件(展开/折叠位置是否被替换),if true 配合prefixIconComponent props一起使用
prefixIconComponent'-'node/element(react)if isPrefixIcon true, 图标前缀组件内容
draggablefalsebool设置节点可拖拽(IE>8)(TreeNode节点会覆盖Tree draggable props)
iconLeaftruebool/string:className控制节点上文件/子节点图标,传递false即不显示图标,也可传递string可自定义className
iconDirtruebool/arr:'icon-wenjianjiaguanbi', 'icon-wenjianjiadakai'控制展开/折叠图标,传递false即不显示图标,也可传递数组(2)可自定义className
*********其他props需要携带的参数均可在props中体现

TreeMore Props

PropDefaultTypeDescription
onClickfunc点击更多图标事件

change logs

0.3.8

  • 发布到公网npm 2

0.3.7

  • 发布到公网npm

0.3.6

  • 扩展Tree组件内可传递多个null

0.3.5

  • 因扩展出问题,回退到0.3.3上,后续再扩展

0.3.4

  • 扩展defaultExpandAll/defaultExpandedKeys, 矫正autoExpandParent用法

0.3.3

  • 修复0.3.2中遗漏一处修改地方;

0.3.2

  • 增加children可传递单个object类型;

0.3.1

  • 修复0.3.0版本中tree强制为null导致报错问题;

0.3.0

  • 修复0.2.9版本异常

0.2.9

  • Tree 新增onDoubleClick事件

0.2.8

  • TreeNode 移除文字color蓝色

0.2.7

  • TreeNode 接收 null

0.2.6

  • TreeNode 增加className props

0.2.5

  • 更好的通过title去扩展修改节点信息交互

0.2.4

  • 优化

0.2.3

  • 同步icon图标

0.2.2

  • 增加props iconLeaf | iconDir | iconEye, 可实现图表部分隐藏/更换等操作

0.2.1

  • 增加节点eye与事件

0.2.0

  • 按照新版设计图重新构思

0.1.9

  • 扩展 prompt props

0.1.8

  • 修复bug

0.1.7

  • 追加展开/选中/全展开props

0.1.6

  • 修复prefixIconComponent类型不严格问题

0.1.5

  • 完善md

0.1.4

  • 增加TreeNode props(isPrefixIcon/prefixIconComponent)功能

0.1.3

  • 修复3个点位置

0.1.2

  • 根据iconfont被修正后重新设置className的版本

0.1.1

  • 修复新版UI props key未设置导致报错

0.1.0

  • 细节调整

0.0.9

  • 按照设计图新出的UI模式调整样式

0.0.8

  • 修复TreeNode上draggable的细节,完善md

0.0.7

  • 增强isLeaf api

0.0.6

  • 修复onSelect事件

0.0.5

  • 增强more/ContextMenu组件联动效果

0.0.4

  • 移除event target

0.0.3

  • 修复bug

0.0.2

  • 完善md等功能|性能优化|事件机制追加

0.0.1

  • 初始化项目