ice-design react组件封装之----树控件

由于项目重构需要,要用react技术,直接上代码

import { Tree, Search, Loading } from "@icedesign/base";
import React, { Component } from 'react';
const { Node: TreeNode } = Tree;
let selectedKeys2 = '';
let selectedKeysTop = [];
let extraTop = {};
export default class TreeSearchLocal extends Component {
    constructor(props) {
        super(props);
        this.state = {
            value: "",
            expandedKeys: [],
            selectedKeys: [],//选中节点的key值集合
            autoExpandParent: true,
            treeData: [],//节点数据
            visible:true//加载loading
        };
        this.matchedKeys = [];
        this.handleSearch = this.handleSearch.bind(this);
        this.handleExpand = this.handleExpand.bind(this);
    }
    componentDidMount() {
        let self = this;
        setTimeout(function(){
            self.setState({
                treeData: self.props.treeData,//将传入的树  数据进行加载出来
                visible:false
            });
        }, 2000);
      }
    onSelect(selectedKeys,extra){
        console.log(selectedKeys,extra);
        if(!extra.selected){
            //第二次点击为取消选中,禁止掉取消选中,也可以不取消,个人意愿
            console.log('selectedKeysTop',selectedKeysTop)
            this.setState({
                selectedKeys:selectedKeysTop
            })
            this.props.onSelect(selectedKeysTop,extraTop)
        }else{
            selectedKeysTop = selectedKeys
            extraTop = extra
            this.setState({
                selectedKeys:selectedKeys
            })
            this.props.onSelect(selectedKeys,extra)
        }
    }
   
    handleSearch(result){
        //搜索树节点
        const value = result.key;
        const matchedKeys = [];
        let treeData = this.state.treeData
        function loop(data){
            return treeData.forEach(function(item){
                if (item.name.indexOf(value.trim()) > -1) {
                    matchedKeys.push(item.key);
                }
                if (item.children && item.children.length) {
                    loop(item.children);
                }
            });
        }
            
        loop(treeData);
        this.setState({
            value: result.key,
            expandedKeys: matchedKeys,
            autoExpandParent: true
        });
        this.matchedKeys = matchedKeys;
    }
    expendNode(selectedKeys){
        let expandedKeys = this.state.expandedKeys;
        let index;
        if(selectedKeys[0]){
            index =expandedKeys.indexOf(selectedKeys[0]);
            selectedKeys2 = selectedKeys[0]        
        }else {
            index =expandedKeys.indexOf(selectedKeys2);
        }
        if(index == -1){
            //展开节点
            expandedKeys.push(selectedKeys2)
            this.setState({
                expandedKeys:expandedKeys
            })
        }else if(index != -1) {
            // console.log('关闭')
            expandedKeys.splice(index,1);
            this.setState({
                expandedKeys:expandedKeys
            })
        }
    }
    handleExpand(keys){
        this.setState({
            expandedKeys: keys,
            autoExpandParent: false
        });
    }
    
    render() {
        //定义loop方法,传入data参数,返回树节点
        function loop(data){
            return  data.map(function(item){
                if (item.children) {
                    return (
                        <TreeNode label={item.name} key={item.key}  type={item.type}>
                            {loop(item.children)}
                        </TreeNode>
                    );
                }
                return (
                    <TreeNode
                        label={item.name}
                        key={item.key}
                        isLeaf={item.isLeaf}
                        type={item.type}
                        //节点上的属性,根据需要增删
                    />
                );
            });
        }
           
        //返回的treeNodes就是要渲染到页面上的树节点数据
        const treeNodes = loop(this.state.treeData);

        const { value, expandedKeys, autoExpandParent, selectedKeys } = this.state;
        //待筛选的节点
        function filterTreeNode(node){
            return value && this.matchedKeys.indexOf(node.props.eventKey) > -1;
        }
        return (
            <Loading visible={this.state.visible} style={{}} shape="fusion-reactor">
                <Search
                    type="normal"
                    size="small"
                    searchText=""
                    autoWidth={true}
                    value={value}
                    onSearch={this.handleSearch}
                />
                    <Tree
                        onSelect={this.onSelect.bind(this)}
                        showLine={true}
                        expandedKeys={expandedKeys}
                        selectedKeys={selectedKeys}
                        autoExpandParent={autoExpandParent}
                        filterTreeNode={filterTreeNode.bind(this)}
                        onExpand={this.handleExpand.bind(this)}
                    >
                        {treeNodes}
                    </Tree>
            </Loading>
        );
    }
}

1. 本组件为一次性加载所有数据,树节点服务器模式(懒加载)不适用,后期会发布懒加载组件代码

       2. 部分代码可能由于注释原因报错,删掉注释即可

【右上角点个赞,谢谢】

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值