From 3d1b4dd51f8c8b401e77301842ccb8eb6a6d059d Mon Sep 17 00:00:00 2001 From: Sergey Yuferev Date: Wed, 20 Mar 2019 19:23:52 +0300 Subject: [PATCH] fix(core): renamed tsx -> ts, improved type checks --- ...nuLevelBuilder.tsx => MenuLevelBuilder.ts} | 23 +++++++++---------- 1 file changed, 11 insertions(+), 12 deletions(-) rename packages/core/src/menu/{MenuLevelBuilder.tsx => MenuLevelBuilder.ts} (71%) diff --git a/packages/core/src/menu/MenuLevelBuilder.tsx b/packages/core/src/menu/MenuLevelBuilder.ts similarity index 71% rename from packages/core/src/menu/MenuLevelBuilder.tsx rename to packages/core/src/menu/MenuLevelBuilder.ts index 81ab9899c..01da8ab0f 100644 --- a/packages/core/src/menu/MenuLevelBuilder.tsx +++ b/packages/core/src/menu/MenuLevelBuilder.ts @@ -10,7 +10,7 @@ export type MenuTree = Item & { } export class MenuLevel { - constructor(public items: Item[] = [], public activeId: string = '') {} + constructor(public items: Item[], public activeId: string) {} } export class MenuLevelBuilder { @@ -19,12 +19,11 @@ export class MenuLevelBuilder { protected buildLevels( activeId: string, items: MenuTree[], - levels: MenuLevel[], + levels: (MenuLevel | undefined)[], depth = 0, parentActive = false ): boolean { - if (levels.length <= depth) levels[depth] = new MenuLevel() - const level = levels[depth] + if (levels.length <= depth) levels.push(undefined) let activeItem: MenuTree | undefined for (let item of items) { const currentActive = item.id === activeId @@ -43,12 +42,11 @@ export class MenuLevelBuilder { if (childActive && !activeItem) activeItem = item } - if (activeItem) { - level.items = items - level.activeId = activeItem.id - } else if (parentActive) { - level.items = items - level.activeId = items[0].id + if (activeItem || parentActive) { + levels[depth] = new MenuLevel( + items, + activeItem ? activeItem.id : items[0].id + ) } return !!activeItem @@ -56,11 +54,12 @@ export class MenuLevelBuilder { /** * Split MenuTree structure to levels. + * Returns array, where index is a menu level. * * @param activeId current menu item id */ - levels(activeId: string): MenuLevel[] { - const result: MenuLevel[] = [] + levels(activeId: string): (MenuLevel | undefined)[] { + const result: (MenuLevel[] | undefined) = [] this.buildLevels(activeId, this.items, result) return result }