Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(Nav): Nav Drilldown example & demo #6875

Merged
merged 1 commit into from
Feb 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions packages/react-core/src/components/Nav/examples/Nav.md
Original file line number Diff line number Diff line change
Expand Up @@ -672,3 +672,8 @@ NavWithFlyout = () => {
);
};
```

### Drilldown

```ts isBeta file="./NavDrilldown.tsx"
```
95 changes: 95 additions & 0 deletions packages/react-core/src/components/Nav/examples/NavDrilldown.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import React from 'react';

import { Nav, MenuContent, MenuItem, MenuList, DrilldownMenu, Menu } from '@patternfly/react-core';

interface MenuHeights {
[menuId: string]: number;
}

const subMenuTwo: JSX.Element = (
<DrilldownMenu id="subMenu-2">
<MenuItem itemId="subMenu-2-breadcrumb" direction="up">
SubMenu 1 - Item 1
</MenuItem>
<MenuItem itemId="subMenu-2_item-1" to="#subMenu-2_item-1">
SubMenu 2 - Item 1
</MenuItem>
<MenuItem itemId="subMenu-2_item-2" to="#subMenu-2_item-2">
SubMenu 2 - Item 2
</MenuItem>
<MenuItem itemId="subMenu-2_item-3" to="#subMenu-2_item-3">
SubMenu 2 - Item 3
</MenuItem>
</DrilldownMenu>
);

const subMenuOne: JSX.Element = (
<DrilldownMenu id="subMenu-1">
<MenuItem itemId="subMenu-1-breadcrumb" direction="up">
Item 1
</MenuItem>
<MenuItem itemId="subMenu-1_item-1" description="SubMenu 2" direction="down" drilldownMenu={subMenuTwo}>
SubMenu 1 - Item 1
</MenuItem>
<MenuItem itemId="subMenu-1_item-2" to="#subMenu-1_item-2">
SubMenu 1 - Item 2
</MenuItem>
<MenuItem itemId="subMenu-1_item-3" to="#subMenu-1_item-3">
SubMenu 1 - Item 3
</MenuItem>
</DrilldownMenu>
);

export const NavLevelThreeDrill: React.FunctionComponent = () => {
const [menuDrilledIn, setMenuDrilledIn] = React.useState<string[]>([]);
const [drilldownPath, setDrilldownPath] = React.useState<string[]>([]);
const [menuHeights, setMenuHeights] = React.useState<MenuHeights>({});
const [activeMenu, setActiveMenu] = React.useState('rootMenu');

const onDrillIn = (fromItemId: string, toItemId: string, itemId: string) => {
setMenuDrilledIn(prevMenuDrilledIn => [...prevMenuDrilledIn, fromItemId]);
setDrilldownPath(prevDrilldownPath => [...prevDrilldownPath, itemId]);
setActiveMenu(toItemId);
};

const onDrillOut = (toItemId: string, _itemId: string) => {
setMenuDrilledIn(prevMenuDrilledIn => prevMenuDrilledIn.slice(0, prevMenuDrilledIn.length - 1));
setDrilldownPath(prevDrilldownPath => prevDrilldownPath.slice(0, prevDrilldownPath.length - 1));
setActiveMenu(toItemId);
};

const onGetMenuHeight = (menuId: string, height: number) => {
if (!menuHeights[menuId] && menuId !== 'rootMenu') {
setMenuHeights(prevMenuHeights => ({ ...prevMenuHeights, [menuId]: height }));
}
};

return (
<Nav>
<Menu
id="rootMenu"
containsDrilldown
drilldownItemPath={drilldownPath}
drilledInMenus={menuDrilledIn}
activeMenu={activeMenu}
onDrillIn={onDrillIn}
onDrillOut={onDrillOut}
onGetMenuHeight={onGetMenuHeight}
>
<MenuContent menuHeight={`${menuHeights[activeMenu]}px`}>
<MenuList>
<MenuItem itemId="item-1" direction="down" description="SubMenu 1" drilldownMenu={subMenuOne}>
Item 1
</MenuItem>
<MenuItem itemId="item-2" to="#item-2">
Item 2
</MenuItem>
<MenuItem itemId="item-3" to="#item-3">
Item 3
</MenuItem>
</MenuList>
</MenuContent>
</Menu>
</Nav>
);
};
6 changes: 4 additions & 2 deletions packages/react-core/src/components/Nav/examples/nav.css
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@
#ws-react-c-navigation-mixed,
#ws-react-c-navigation-expandable-third-level,
#ws-react-c-navigation-horizontal-subnav,
#ws-react-c-navigation-flyout {
#ws-react-c-navigation-flyout,
#ws-react-c-navigation-drilldown {
background-color: var(--pf-global--BackgroundColor--dark-300);
}

#ws-react-c-navigation-flyout {
#ws-react-c-navigation-flyout,
#ws-react-c-navigation-drilldown {
width: 300px;
}
15 changes: 7 additions & 8 deletions packages/react-core/src/demos/Nav.md
Original file line number Diff line number Diff line change
Expand Up @@ -1439,7 +1439,7 @@ class VerticalPage extends React.Component {
super(props);
this.state = {
isNavOpen: true,
activeItem: 0,
activeItem: 0
};
this.onNavSelect = result => {
this.setState({
Expand Down Expand Up @@ -1489,13 +1489,7 @@ class VerticalPage extends React.Component {
<NavItem id="flyout-link1" to="#flyout-link1" itemId={0} isActive={activeItem === 0}>
System Panel
</NavItem>
<NavItem
flyout={curFlyout}
id="flyout-link2"
to="#flyout-link2"
itemId={1}
isActive={activeItem === 1}
>
<NavItem flyout={curFlyout} id="flyout-link2" to="#flyout-link2" itemId={1} isActive={activeItem === 1}>
Settings
</NavItem>
<NavItem id="flyout-link3" to="#flyout-link3" itemId={2} isActive={activeItem === 2}>
Expand All @@ -1517,3 +1511,8 @@ class VerticalPage extends React.Component {
}
}
```

### Drilldown nav

```js isBeta isFullscreen file="./examples/Nav/NavDrilldown.tsx"
```
132 changes: 132 additions & 0 deletions packages/react-core/src/demos/examples/Nav/NavDrilldown.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
import React from 'react';

import {
Page,
PageSidebar,
Nav,
MenuContent,
MenuItem,
MenuList,
DrilldownMenu,
Menu,
PageSection
} from '@patternfly/react-core';

import DashboardHeader from '../DashboardHeader';

interface MenuHeights {
[menuId: string]: number;
}

function getNavLayer(menuId: string): number {
switch (menuId) {
case 'rootMenu':
return 1;
case 'subMenu-1':
return 2;
case 'subMenu-2':
return 3;
}
}

const subMenuTwo: JSX.Element = (
<DrilldownMenu id="subMenu-2">
<MenuItem itemId="subMenu-2-breadcrumb" direction="up">
SubMenu 1 - Item 1
</MenuItem>
<MenuItem itemId="subMenu-2_item-1" to="#subMenu-2_item-1">
SubMenu 2 - Item 1
</MenuItem>
<MenuItem itemId="subMenu-2_item-2" to="#subMenu-2_item-2">
SubMenu 2 - Item 2
</MenuItem>
<MenuItem itemId="subMenu-2_item-3" to="#subMenu-2_item-3">
SubMenu 2 - Item 3
</MenuItem>
</DrilldownMenu>
);

const subMenuOne: JSX.Element = (
<DrilldownMenu id="subMenu-1">
<MenuItem itemId="subMenu-1-breadcrumb" direction="up">
Item 1
</MenuItem>
<MenuItem itemId="subMenu-1_item-1" description="SubMenu 2" direction="down" drilldownMenu={subMenuTwo}>
SubMenu 1 - Item 1
</MenuItem>
<MenuItem itemId="subMenu-1_item-2" to="#subMenu-1_item-2">
SubMenu 1 - Item 2
</MenuItem>
<MenuItem itemId="subMenu-1_item-3" to="#subMenu-1_item-3">
SubMenu 1 - Item 3
</MenuItem>
</DrilldownMenu>
);

export const NavDrilldown: React.FunctionComponent = () => {
const [menuDrilledIn, setMenuDrilledIn] = React.useState<string[]>([]);
const [drilldownPath, setDrilldownPath] = React.useState<string[]>([]);
const [menuHeights, setMenuHeights] = React.useState<MenuHeights>({});
const [activeMenuId, setActiveMenuId] = React.useState('rootMenu');
const navLayer = getNavLayer(activeMenuId);

const onDrillIn = (fromItemId: string, toItemId: string, itemId: string) => {
setMenuDrilledIn(prevMenuDrilledIn => [...prevMenuDrilledIn, fromItemId]);
setDrilldownPath(prevDrilldownPath => [...prevDrilldownPath, itemId]);
setActiveMenuId(toItemId);
};

const onDrillOut = (toItemId: string, _itemId: string) => {
setMenuDrilledIn(prevMenuDrilledIn => prevMenuDrilledIn.slice(0, prevMenuDrilledIn.length - 1));
setDrilldownPath(prevDrilldownPath => prevDrilldownPath.slice(0, prevDrilldownPath.length - 1));
setActiveMenuId(toItemId);
};

const onGetMenuHeight = (menuId: string, height: number) => {
if (!menuHeights[menuId] && menuId !== 'rootMenu') {
setMenuHeights(prevMenuHeights => ({ ...prevMenuHeights, [menuId]: height }));
}
};

const sidebar = (
<PageSidebar
nav={
<Nav>
<Menu
id="rootMenu"
containsDrilldown
drilldownItemPath={drilldownPath}
drilledInMenus={menuDrilledIn}
activeMenu={activeMenuId}
onDrillIn={onDrillIn}
onDrillOut={onDrillOut}
onGetMenuHeight={onGetMenuHeight}
>
<MenuContent menuHeight={`${menuHeights[activeMenuId]}px`}>
<MenuList>
<MenuItem itemId="item-1" direction="down" description="SubMenu 1" drilldownMenu={subMenuOne}>
Item 1
</MenuItem>
<MenuItem itemId="item-2" to="#item-2">
Item 2
</MenuItem>
<MenuItem itemId="item-3" to="#item-3">
Item 3
</MenuItem>
</MenuList>
</MenuContent>
</Menu>
</Nav>
}
/>
);

return (
<Page header={<DashboardHeader />} sidebar={sidebar}>
<PageSection>
<strong>Nav Layer: </strong>
{navLayer}
</PageSection>
</Page>
);
};