react ant ice3 实现点击一级菜单自动打开它下面最深的第一个子菜单

1.问题

默认的如果没有你的菜单结构是这样的:

js 复制代码
[
	{
	children: [
		{
			name: "通用配置"
			parentId: "1744857774620672"
			path: "basic"
		}
	],
	name: "系统管理"
	parentId: "-1"
	path: "system"
	}
]

可以看到每层菜单的path都只有当前的路径,没有进行全拼接。

那么此时点击一级菜单就会有问题:

2.解决

让一级菜单进行全拼接,二三级等子菜单不做处理,仍然保持当前的path,也就是点击一级菜单自动打开它下面最深的第一个子菜单。

js 复制代码
/**
 * @description: 递归 设置一个父亲菜单完整的菜单路径,
 * 例如:[{path:home,children;[{path:console}]}]
 * 得到:[{path:home/console,children;[{path:console}]}]
 * @param menuData 递归的对象
 * @param parentPath 父路径
 * @returns 替换后的对象
 */
export const setParentPaths = (data) => {
  if (data.length === 0) return;
  data.forEach((i) => {
    const concatPaths = (item, parentPath = "") => {
      const currentPath = parentPath + "/" + (item.path || item.fullPath);

      if (item.children && item.children.length > 0) {
        item.fullPath = concatPaths(item.children[0], currentPath);
      } else {
        item.fullPath = currentPath;
      }

      return item.fullPath;
    };

    concatPaths(i);
  });

  return data;
};
相关推荐
之歆6 小时前
DAY_12JavaScript DOM 完全指南(二):实战与性能篇
开发语言·前端·javascript·ecmascript
Maimai108086 小时前
React如何用 @microsoft/fetch-event-source 落地 SSE:比原生 EventSource 更灵活的实时推送方案
前端·javascript·react.js·microsoft·前端框架·reactjs·webassembly
candyTong6 小时前
Claude Code 的 Edit 工具是怎么工作的
javascript·后端·架构
卡卡军9 小时前
agmd 1.0 重磅升级——Rust 重写,性能起飞
javascript·rust
Larcher9 小时前
🔥 告别抓瞎:用 Claude Code (cc) 优雅接手与维护已有项目
javascript·机器学习·前端框架
JYeontu9 小时前
轮播图不够惊艳?试下这个立体卡片轮播图
前端·javascript·css
亲亲小宝宝鸭9 小时前
如何监听DOM尺寸的变化?element-resize-detector 和 resizeObserver
前端·javascript
卷帘依旧12 小时前
Generator 全面解析 + async/await 深度对比
前端·javascript
weixin_4713830312 小时前
统一缩放单位基础(px、em、rem)
开发语言·javascript·ecmascript
yqcoder12 小时前
数据劫持的双雄:深入解析 Object.defineProperty 与 Proxy
开发语言·前端·javascript