diff --git a/packages/components/src/navigation/index.js b/packages/components/src/navigation/index.js index 4d1e8aff1f72ca..e411bb12eb8fbf 100644 --- a/packages/components/src/navigation/index.js +++ b/packages/components/src/navigation/index.js @@ -1,36 +1,44 @@ -/** - * WordPress dependencies - */ -import { Children, cloneElement } from '@wordpress/element'; +const Navigation = ( { activeId, children, items: rawItems, rootTitle } ) => { + const mapItemData = ( items ) => { + return items.map( ( item ) => { + const itemChildren = rawItems.filter( + ( i ) => i.parent === item.id + ); + return { + ...item, + children: itemChildren, + parent: item.parent || 'root', + }; + } ); + }; + const items = [ + { id: 'root', title: rootTitle }, + ...mapItemData( rawItems ), + ]; -const Navigation = ( { active, children, items } ) => { - const activeItem = items.find( ( item ) => item.id === active ); - const parentItem = items.find( ( item ) => item.id === activeItem.parent ); - const grandParentItem = - parentItem && parentItem.parent - ? items.find( ( item ) => item.id === parentItem.parent ) - : null; - const backItem = parentItem - ? items.find( ( item ) => { - if ( grandParentItem ) { - return item.parent === grandParentItem; - } - return ! item.parent; - } ) + const activeItem = items.find( ( item ) => item.id === activeId ); + const currentLevel = activeItem + ? items.find( ( item ) => item.id === activeItem.parent ) + : { id: 'root', title: rootTitle }; + const parentLevel = currentLevel + ? items.find( ( item ) => item.id === currentLevel.parent ) : null; + const currentItems = currentLevel + ? items.filter( ( item ) => item.parent === currentLevel.id ) + : items.filter( ( item ) => item.parent === 'root' ); + const parentItems = parentLevel + ? items.filter( ( item ) => item.parent === parentLevel.id ) + : items.filter( ( item ) => item.parent === 'root' ); return (
- { Children.map( children, ( child ) => - child - ? cloneElement( child, { - active, - backItem, - items, - parentItem, - } ) - : null - ) } + { children( { + activeId, + currentItems, + currentLevel, + parentItems, + parentLevel, + } ) }
); }; diff --git a/packages/components/src/navigation/pane.js b/packages/components/src/navigation/pane.js new file mode 100644 index 00000000000000..70205338a1513a --- /dev/null +++ b/packages/components/src/navigation/pane.js @@ -0,0 +1,5 @@ +const NavigationPane = ( { children } ) => { + return
{ children }
; +}; + +export default NavigationPane; diff --git a/packages/components/src/navigation/stories/index.js b/packages/components/src/navigation/stories/index.js index 604dba2a4e5fcb..ed1658495f89d5 100644 --- a/packages/components/src/navigation/stories/index.js +++ b/packages/components/src/navigation/stories/index.js @@ -8,8 +8,10 @@ import { useState } from '@wordpress/element'; */ import Navigation from '../'; import NavigationBackButton from '../back-button'; +import NavigationPane from '../pane'; import NavigationMenu from '../menu'; import NavigationTitle from '../title'; +import NavigationMenuItem from '../menu-item'; export default { title: 'Components/Navigation', @@ -80,22 +82,75 @@ const data = [ function Example() { const [ active, setActive ] = useState( 'home' ); - const items = data.map( ( item ) => { - item.onClick = () => setActive( item.id ); - return item; - } ); return ( - - ( item ? setActive( item.id ) : null ) } - /> - - -
- + + { ( { currentItems, currentLevel, parentItems, parentLevel } ) => { + return ( + + + parentLevel + ? setActive( parentItems[ 0 ].id ) + : ( window.location = + 'https://wordpress.com' ) + } + > + { ! parentLevel ? 'Dashboard' : parentLevel.title } + + + { currentLevel.id === 'root' + ? 'WooCommerce' + : currentLevel.title } + + + { currentItems + .filter( ( item ) => item.menu !== 'secondary' ) + .map( ( item ) => ( + 0 } + isActive={ item.id === active } + key={ item.id } + onClick={ () => + item.children.length + ? setActive( + item.children[ 0 ].id + ) + : setActive( item.id ) + } + > + { item.title } + + ) ) } + +
+ + { currentItems + .filter( ( item ) => item.menu === 'secondary' ) + .map( ( item ) => ( + 0 } + isActive={ item.id === active } + key={ item.id } + onClick={ () => + item.children.length + ? setActive( + item.children[ 0 ].id + ) + : setActive( item.id ) + } + > + { item.title } + + ) ) } + +
+ ); + } }
); } diff --git a/packages/components/src/navigation/style.scss b/packages/components/src/navigation/style.scss index ac22ed6f4beefa..df9ff20ed4f74e 100644 --- a/packages/components/src/navigation/style.scss +++ b/packages/components/src/navigation/style.scss @@ -11,7 +11,7 @@ margin-bottom: 40px; } -.components-navigation__menu-items { +.components-navigation__menu { display: flex; flex-direction: column; }