-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Sergey Yuferev
committed
Mar 20, 2019
1 parent
39c3bd5
commit 6b07bb5
Showing
3 changed files
with
69 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import {ReactNode} from 'react' | ||
|
||
export interface BaseMenuItem { | ||
id: string | ||
title: ReactNode | ||
} | ||
|
||
export type MenuTree<Item extends BaseMenuItem> = Item & { | ||
sub?: MenuTree<Item>[] | ||
} | ||
|
||
export class MenuLevel<Item extends BaseMenuItem> { | ||
constructor(public items: Item[] = [], public activeId: string = '') {} | ||
} | ||
|
||
export class MenuLevelBuilder<Item extends BaseMenuItem> { | ||
constructor(protected items: MenuTree<Item>[]) {} | ||
|
||
protected buildLevels( | ||
activeId: string, | ||
items: MenuTree<Item>[], | ||
levels: MenuLevel<Item>[], | ||
depth = 0, | ||
parentActive = false | ||
): boolean { | ||
if (levels.length <= depth) levels[depth] = new MenuLevel() | ||
const level = levels[depth] | ||
let activeItem: MenuTree<Item> | undefined | ||
for (let item of items) { | ||
const currentActive = item.id === activeId | ||
const sub = item.sub | ||
const childActive = | ||
sub && sub.length > 0 | ||
? this.buildLevels( | ||
activeId, | ||
sub, | ||
levels, | ||
depth + 1, | ||
currentActive || parentActive | ||
) | ||
: false | ||
if (currentActive) activeItem = item | ||
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 | ||
} | ||
|
||
return !!activeItem | ||
} | ||
|
||
/** | ||
* Split MenuTree structure to levels. | ||
* | ||
* @param activeId current menu item id | ||
*/ | ||
levels(activeId: string): MenuLevel<Item>[] { | ||
const result: MenuLevel<Item>[] = [] | ||
this.buildLevels(activeId, this.items, result) | ||
return result | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './MenuLevelBuilder' |