【React+Taro】手动开发的树组件Tree

Tree.js

javascript 复制代码
import Taro, { useState } from '@tarojs/taro';
import { View, Text, Button } from '@tarojs/components';
import './Tree.scss';

const TreeNode = ({ node, onSelect, onExpand }) => {
  const handleNodeClick = () => {
    onSelect && onSelect(node.id);
  };

  const handleExpandClick = () => {
    onExpand && onExpand(node.id);
  };

  return (
    <View className="tree-node">
      <Text className="title" onClick={handleNodeClick}>
        {node.title}
      </Text>
      {node.children && node.children.length > 0 && (
        <Button className="expand-btn" onClick={handleExpandClick}>
          {node.expanded ? '-' : '+'}
        </Button>
      )}
      {node.expanded && node.children && node.children.length > 0 && (
        <View className="children">
          {node.children.map((child) => (
            <TreeNode
              key={child.id}
              node={child}
              onSelect={onSelect}
              onExpand={onExpand}
            />
          ))}
        </View>
      )}
    </View>
  );
};

const Tree = () => {
  const [treeData, setTreeData] = useState([
    {
      id: '1',
      title: 'Node 1',
      children: [
        {
          id: '1-1',
          title: 'Node 1-1',
          children: [
            {
              id: '1-1-1',
              title: 'Node 1-1-1',
              children: [
                { id: '1-1-1-1', title: 'Node 1-1-1-1', children: [] },
                { id: '1-1-1-2', title: 'Node 1-1-1-2', children: [] },
              ],
            },
            {
              id: '1-1-2',
              title: 'Node 1-1-2',
              children: [],
            },
          ],
        },
        {
          id: '1-2',
          title: 'Node 1-2',
          children: [
            {
              id: '1-2-1',
              title: 'Node 1-2-1',
              children: [],
            },
            {
              id: '1-2-2',
              title: 'Node 1-2-2',
              children: [],
            },
          ],
        },
      ],
    },
  ]);

  const handleNodeSelect = (id) => {
    console.log('Selected Node:', id);
  };

  const handleNodeExpand = (id) => {
    setTreeData((prevTreeData) => {
      return toggleNodeExpanded(prevTreeData, id);
    });
  };

  const toggleNodeExpanded = (data, id) => {
    return data.map((node) => {
      if (node.id === id) {
        return {
          ...node,
          expanded: !node.expanded,
        };
      } else if (node.children && node.children.length > 0) {
        return {
          ...node,
          children: toggleNodeExpanded(node.children, id),
        };
      }
      return node;
    });
  };

  return (
    <View className="tree">
      {treeData.map((node) => (
        <TreeNode
          key={node.id}
          node={node}
          onSelect={handleNodeSelect}
          onExpand={handleNodeExpand}
        />
      ))}
    </View>
  );
};

export default Tree;

Tree.scss

javascript 复制代码
.tree {
  .tree-node {
    display: flex;
    align-items: center;
    margin-bottom: 10px;

    .title {
      flex-grow: 1;
      cursor: pointer;
    }

    .expand-btn {
      width: 20px;
      height: 20px;
      line-height: 20px;
      margin-left: 5px;
      font-size: 16px;
    }

    &.children {
      margin-left: 10px;
    }
  }
}

引用Tree.js的主页面index.js

javascript 复制代码
import Taro, { Component } from '@tarojs/taro';
import { View } from '@tarojs/components';
import Tree from '../../components/Tree';

class Index extends Component {
  config = {
    navigationBarTitleText: '首页',
  };

  render() {
    return (
      <View>
        <Tree />
      </View>
    );
  }
}

export default Index;

具体的样式大家可以根据需求手动调整。

相关推荐
雾恋6 小时前
最近一年的感悟
前端·javascript·程序员
华仔啊6 小时前
Vue3 的 ref 和 reactive 到底用哪个?90% 的开发者都选错了
javascript·vue.js
A黄俊辉A7 小时前
axios+ts封装
开发语言·前端·javascript
小李小李不讲道理7 小时前
「Ant Design 组件库探索」四:Input组件
前端·javascript·react.js
郑板桥308 小时前
tua-body-scroll-lock踩坑记录
前端·javascript
解道Jdon9 小时前
SpringBoot4与Spring7发布:云原生深度进化
javascript·reactjs
gnip10 小时前
pnpm 的 monorepo架构多包管理
前端·javascript
zolty10 小时前
基于hiprint的票据定位打印系统开发实践
javascript
百思可瑞教育11 小时前
使用UniApp实现一个AI对话页面
javascript·vue.js·人工智能·uni-app·xcode·北京百思可瑞教育·百思可瑞教育
Enddme12 小时前
《前端笔试必备:JavaScript ACM输入输出模板》
前端·javascript·面试