在antd tree组件基础上封装一个文件目录树

背景

最近公司所做的项目中有个文档管理的需求,需要有个目录来展示不同的文档。可以对文档目录进行添加、重命名、删除、切换等操作。但是antd的tree组件不能完全满足需要,于是就进行了二次封装。

先看看效果图:
在这里插入图片描述
在这里插入图片描述

难点

主要的难点是这个hover按钮,之前没什么思路。后来想一想,可以放在数据中的title里面,antd中的数据都可以将一段jsx放进去。如下:

const TreeTitle = (props) => {
      const { title, id } = props;
      return (
        <React.Fragment>
          <span>{title}</span>
          {id === 'root' ? null : (
            <Popover
              content={<PopoverContent id={id} title={title} />}
              placement="rightTop"
            >
              <MoreOutlined className="icon-operate" />
            </Popover>
          )}
        </React.Fragment>
      );
    };

这里的root要注意一下,上面的效果图中的所有类别的id就是root,这是我手动添加到类别数组中的,不是后端返回的,所以不需要hover图标,才加了上面这一层处理。

然后用了递归生成目录树所需的数据结构,如下:

// 加工后端返回的treedata,给title加上hover显示图标
    const gernerateTreeData = (data) => {
      data = data.map((item) => {
        if (item.children && item.children.length) {
          return {
            key: item.key,
            title: <TreeTitle title={item.title} id={item.key} />,
            children: gernerateTreeData(item.children),
          };
        } else {
          return {
            key: item.key,
            title: <TreeTitle title={item.title} id={item.key} />,
          };
        }
      });
      return data;
    };

html部分就比较简单,如下:

<div style={{ height: '100%', paddingTop: '20px' }}>
        <Row justify="space-between">
          <Input
            prefix={<SearchOutlined style={{ color: '#ddd' }} />}
            style={{ marginLeft: '10px', marginBottom: '10px', width: '120px' }}
            size="small"
            allowClear
            onChange={this.handleSearch}
          />
          <Button
            style={{ marginRight: '5px' }}
            size="small"
            onClick={() => this.showAddConfirm('add')}
            disabled={this.state.isEditing}
          >
            <PlusOutlined />
          </Button>
        </Row>

        {this.state.treeData.length ? (
          <DirectoryTree
            style={{ marginLeft: '8px' }}
            treeData={gernerateTreeData(this.state.treeData)}
            showIcon={false}
            expandedKeys={this.state.expandedKeys}
            onExpand={this.expand}
            switcherIcon={<DownOutlined />}
            onSelect={this.select}
          ></DirectoryTree>
        ) : null}
      </div>

最终效果还是不错的,达到了业务的要求。完美! 洗洗睡。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值