| 1 |
import { useState } from 'react'; |
| 2 |
import { |
| 3 |
Breadcrumb, |
| 4 |
BreadcrumbItem, |
| 5 |
Content, |
| 6 |
Nav, |
| 7 |
NavItem, |
| 8 |
NavList, |
| 9 |
Page, |
| 10 |
PageSection, |
| 11 |
PageSidebar, |
| 12 |
PageSidebarBody, |
| 13 |
SkipToContent |
| 14 |
} from '@patternfly/react-core'; |
| 15 |
import { DashboardHeader } from './DashboardHeader'; |
| 16 |
|
| 17 |
interface DashboardWrapperProps { |
| 18 |
/** Programmatically manage if the sidebar nav is shown */ |
| 19 |
sidebarNavOpen?: boolean; |
| 20 |
/** Flag to render sample breadcrumb if custom breadcrumb not passed */ |
| 21 |
hasDefaultBreadcrumb?: boolean; |
| 22 |
/** Flag to render sample page title if custom title not passed */ |
| 23 |
hasPageTemplateTitle?: boolean; |
| 24 |
/** Content rendered inside the main section of the page layout (e.g. <PageSection />) */ |
| 25 |
children?: React.ReactNode; |
| 26 |
/** Additional classes added to the page layout */ |
| 27 |
className?: string; |
| 28 |
/** Masthead component (e.g. <Masthead />) */ |
| 29 |
masthead?: React.ReactNode; |
| 30 |
/** Sidebar component for a side nav (e.g. <PageSidebar />) */ |
| 31 |
sidebar?: React.ReactNode; |
| 32 |
/** Notification drawer component for an optional notification drawer (e.g. <NotificationDrawer />) */ |
| 33 |
notificationDrawer?: React.ReactNode; |
| 34 |
/** Flag indicating Notification drawer in expanded */ |
| 35 |
isNotificationDrawerExpanded?: boolean; |
| 36 |
/** Flag indicating if breadcrumb width should be limited */ |
| 37 |
isBreadcrumbWidthLimited?: boolean; |
| 38 |
/** Callback when notification drawer panel is finished expanding. */ |
| 39 |
onNotificationDrawerExpand?: (event: KeyboardEvent | React.MouseEvent | React.TransitionEvent) => void; |
| 40 |
/** Skip to content component for the page */ |
| 41 |
skipToContent?: React.ReactElement<any>; |
| 42 |
/** Sets the value for role on the <main> element */ |
| 43 |
role?: string; |
| 44 |
/** an id to use for the [role="main"] element */ |
| 45 |
mainContainerId?: string; |
| 46 |
/** tabIndex to use for the [role="main"] element, null to unset it */ |
| 47 |
mainTabIndex?: number | null; |
| 48 |
/** |
| 49 |
* If true, manages the sidebar open/close state and there is no need to pass the isSidebarOpen boolean into |
| 50 |
* the sidebar component or add a callback onSidebarToggle function into the Masthead component |
| 51 |
*/ |
| 52 |
isManagedSidebar?: boolean; |
| 53 |
/** Flag indicating if tertiary nav width should be limited */ |
| 54 |
isTertiaryNavWidthLimited?: boolean; |
| 55 |
/** |
| 56 |
* If true, the managed sidebar is initially open for desktop view |
| 57 |
*/ |
| 58 |
defaultManagedSidebarIsOpen?: boolean; |
| 59 |
/** |
| 60 |
* Can add callback to be notified when resize occurs, for example to set the sidebar isSidebarOpen prop to false for a width < 768px |
| 61 |
* Returns object { mobileView: boolean, windowSize: number } |
| 62 |
*/ |
| 63 |
onPageResize?: ((event: MouseEvent | TouchEvent | React.KeyboardEvent, object: any) => void) | null; |
| 64 |
/** |
| 65 |
* The page resize observer uses the breakpoints returned from this function when adding the pf-m-breakpoint-[default|sm|md|lg|xl|2xl] class |
| 66 |
* You can override the default getBreakpoint function to return breakpoints at different sizes than the default |
| 67 |
* You can view the default getBreakpoint function here: |
| 68 |
* https://github.com/patternfly/patternfly-react/blob/main/packages/react-core/src/helpers/util.ts |
| 69 |
*/ |
| 70 |
getBreakpoint?: (width: number | null) => 'default' | 'sm' | 'md' | 'lg' | 'xl' | '2xl'; |
| 71 |
/** |
| 72 |
* The page resize observer uses the breakpoints returned from this function when adding the pf-m-breakpoint-[default|sm|md|lg|xl|2xl] class |
| 73 |
* You can override the default getVerticalBreakpoint function to return breakpoints at different sizes than the default |
| 74 |
* You can view the default getVerticalBreakpoint function here: |
| 75 |
* https://github.com/patternfly/patternfly-react/blob/main/packages/react-core/src/helpers/util.ts |
| 76 |
*/ |
| 77 |
getVerticalBreakpoint?: (height: number | null) => 'default' | 'sm' | 'md' | 'lg' | 'xl' | '2xl'; |
| 78 |
/** Breadcrumb component for the page */ |
| 79 |
breadcrumb?: React.ReactNode; |
| 80 |
/** Tertiary nav component for the page */ |
| 81 |
tertiaryNav?: React.ReactNode; |
| 82 |
/** Accessible label, can be used to name main section */ |
| 83 |
mainAriaLabel?: string; |
| 84 |
/** Flag indicating if the tertiaryNav should be in a group */ |
| 85 |
isTertiaryNavGrouped?: boolean; |
| 86 |
/** Flag indicating if the breadcrumb should be in a group */ |
| 87 |
isBreadcrumbGrouped?: boolean; |
| 88 |
/** Additional content of the group */ |
| 89 |
additionalGroupedContent?: React.ReactNode; |
| 90 |
/** HTML component used as main component of the page. Defaults to 'main', only pass in 'div' if another 'main' element already exists. */ |
| 91 |
mainComponent?: 'main' | 'div'; |
| 92 |
/** Additional props of the group */ |
| 93 |
groupProps?: any; |
| 94 |
/** Additional props of the breadcrumb */ |
| 95 |
breadcrumbProps?: any; |
| 96 |
} |
| 97 |
|
| 98 |
export const DashboardBreadcrumb = ( |
| 99 |
<Breadcrumb> |
| 100 |
<BreadcrumbItem>Section home</BreadcrumbItem> |
| 101 |
<BreadcrumbItem to="#">Section title</BreadcrumbItem> |
| 102 |
<BreadcrumbItem to="#">Section title</BreadcrumbItem> |
| 103 |
<BreadcrumbItem to="#" isActive> |
| 104 |
Section landing |
| 105 |
</BreadcrumbItem> |
| 106 |
</Breadcrumb> |
| 107 |
); |
| 108 |
|
| 109 |
const PageTemplateTitle = ( |
| 110 |
<PageSection aria-labelledby="main-title"> |
| 111 |
<Content> |
| 112 |
<h1 id="main-title">Main title</h1> |
| 113 |
<p>This is a full page demo.</p> |
| 114 |
</Content> |
| 115 |
</PageSection> |
| 116 |
); |
| 117 |
|
| 118 |
export const DashboardWrapper: React.FC<DashboardWrapperProps> = ({ |
| 119 |
children, |
| 120 |
mainContainerId, |
| 121 |
breadcrumb, |
| 122 |
masthead, |
| 123 |
sidebar, |
| 124 |
sidebarNavOpen, |
| 125 |
onPageResize, |
| 126 |
hasDefaultBreadcrumb, |
| 127 |
notificationDrawer, |
| 128 |
isNotificationDrawerExpanded, |
| 129 |
hasPageTemplateTitle, |
| 130 |
...pageProps |
| 131 |
}: DashboardWrapperProps) => { |
| 132 |
const [activeItem, setActiveItem] = useState(1); |
| 133 |
|
| 134 |
const onNavSelect = (_event: React.FormEvent<HTMLInputElement>, result: any) => { |
| 135 |
setActiveItem(result.itemId); |
| 136 |
}; |
| 137 |
|
| 138 |
let renderedBreadcrumb; |
| 139 |
if (!hasDefaultBreadcrumb) { |
| 140 |
renderedBreadcrumb = breadcrumb ?? DashboardBreadcrumb; |
| 141 |
} |
| 142 |
|
| 143 |
const PageNav = ( |
| 144 |
<Nav onSelect={onNavSelect} aria-label="Nav"> |
| 145 |
<NavList> |
| 146 |
<NavItem itemId={0} isActive={activeItem === 0} to="#system-panel"> |
| 147 |
System panel |
| 148 |
</NavItem> |
| 149 |
<NavItem itemId={1} isActive={activeItem === 1} to="#policy"> |
| 150 |
Policy |
| 151 |
</NavItem> |
| 152 |
<NavItem itemId={2} isActive={activeItem === 2} to="#auth"> |
| 153 |
Authentication |
| 154 |
</NavItem> |
| 155 |
<NavItem itemId={3} isActive={activeItem === 3} to="#network"> |
| 156 |
Network services |
| 157 |
</NavItem> |
| 158 |
<NavItem itemId={4} isActive={activeItem === 4} to="#server"> |
| 159 |
Server |
| 160 |
</NavItem> |
| 161 |
</NavList> |
| 162 |
</Nav> |
| 163 |
); |
| 164 |
|
| 165 |
const _sidebar = ( |
| 166 |
<PageSidebar isSidebarOpen={sidebarNavOpen || false}> |
| 167 |
<PageSidebarBody>{PageNav}</PageSidebarBody> |
| 168 |
</PageSidebar> |
| 169 |
); |
| 170 |
|
| 171 |
const handleClick = (event: React.MouseEvent<HTMLDivElement, MouseEvent>) => { |
| 172 |
event.preventDefault(); |
| 173 |
|
| 174 |
const mainContentElement = document.getElementById(mainContainerId ?? 'main-content-page-layout-default-nav'); |
| 175 |
if (mainContentElement) { |
| 176 |
mainContentElement.focus(); |
| 177 |
} |
| 178 |
}; |
| 179 |
|
| 180 |
const PageSkipToContent = ( |
| 181 |
<SkipToContent onClick={handleClick} href={`#${mainContainerId ?? 'main-content-page-layout-default-nav'}`}> |
| 182 |
Skip to content |
| 183 |
</SkipToContent> |
| 184 |
); |
| 185 |
|
| 186 |
return ( |
| 187 |
<Page |
| 188 |
masthead={masthead ?? <DashboardHeader />} |
| 189 |
sidebar={sidebar ?? _sidebar} |
| 190 |
isManagedSidebar |
| 191 |
skipToContent={PageSkipToContent} |
| 192 |
breadcrumb={renderedBreadcrumb} |
| 193 |
mainContainerId={mainContainerId ?? 'main-content-page-layout-default-nav'} |
| 194 |
notificationDrawer={notificationDrawer} |
| 195 |
isNotificationDrawerExpanded={isNotificationDrawerExpanded} |
| 196 |
{...(typeof onPageResize === 'function' && { |
| 197 |
onPageResize: (event: MouseEvent | TouchEvent | React.KeyboardEvent<Element>, resizeObject: any) => |
| 198 |
onPageResize(event, resizeObject) |
| 199 |
})} |
| 200 |
{...pageProps} |
| 201 |
> |
| 202 |
{hasPageTemplateTitle && PageTemplateTitle} |
| 203 |
{children} |
| 204 |
</Page> |
| 205 |
); |
| 206 |
}; |
| 207 |
DashboardWrapper.displayName = 'DashboardWrapper'; |