【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;

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

相关推荐
一只大黑洋8 分钟前
Clipboard.js 复制内容
前端·javascript·vue.js
然我11 分钟前
从 “只会聊天” 到 “能办实事”:OpenAI Function Call 彻底重构 AI 交互逻辑(附完整接入指南)
前端·javascript·人工智能
鹏多多12 分钟前
vue混入mixins详解和生命周期影响
前端·javascript·vue.js
汪子熙13 分钟前
HTML 中的 Bidirectional Isolate (bdi) 元素深入解析
前端·javascript
Mintopia19 分钟前
在浏览器里“养”一只会写字的仓鼠——AI SSE 流式文本生成全攻略
前端·javascript·aigc
文博知浅23 分钟前
时隔4个月,500+star,鸿蒙ArkTS vscode插件1.x已发布🎉完全重构,补全、类型提示、SDK下载管理切换一应俱全,更多新功能正在规划中...
前端·javascript·harmonyos
专业抄代码选手24 分钟前
从 Fiber 到 DOM:深入理解 React 渲染
javascript·面试
诗书画唱1 小时前
【前端面试题】JavaScript 核心知识点解析(第二十二题到第六十一题)
开发语言·前端·javascript
江城开朗的豌豆2 小时前
React输入框优化:如何精准获取用户输入完成后的最终值?
前端·javascript·全栈
CF14年老兵2 小时前
从卡顿到飞驰:我是如何用WebAssembly引爆React性能的
前端·react.js·trae