| 1 |
import { Component, createContext, createRef } from 'react'; |
| 2 |
import styles from '@patternfly/react-styles/css/components/Nav/nav'; |
| 3 |
import { css } from '@patternfly/react-styles'; |
| 4 |
import { getOUIAProps, OUIAProps } from '../../helpers'; |
| 5 |
import { SSRSafeIds } from '../../helpers/SSRSafeIds/SSRSafeIds'; |
| 6 |
|
| 7 |
export type NavSelectClickHandler = ( |
| 8 |
event: React.FormEvent<HTMLInputElement>, |
| 9 |
itemId: number | string, |
| 10 |
groupId: number | string, |
| 11 |
to: string |
| 12 |
) => void; |
| 13 |
export interface NavProps |
| 14 |
extends |
| 15 |
Omit<React.DetailedHTMLProps<React.HTMLAttributes<HTMLElement>, HTMLElement>, 'onSelect' | 'onToggle'>, |
| 16 |
OUIAProps { |
| 17 |
/** Anything that can be rendered inside of the nav */ |
| 18 |
children?: React.ReactNode; |
| 19 |
/** Additional classes added to the container */ |
| 20 |
className?: string; |
| 21 |
/** Callback for updating when item selection changes */ |
| 22 |
onSelect?: ( |
| 23 |
event: React.FormEvent<HTMLInputElement>, |
| 24 |
selectedItem: { |
| 25 |
groupId: number | string; |
| 26 |
itemId: number | string; |
| 27 |
to: string; |
| 28 |
} |
| 29 |
) => void; |
| 30 |
/** Callback for when a list is expanded or collapsed */ |
| 31 |
onToggle?: ( |
| 32 |
event: React.MouseEvent<HTMLButtonElement>, |
| 33 |
toggledItem: { |
| 34 |
groupId: number | string; |
| 35 |
isExpanded: boolean; |
| 36 |
} |
| 37 |
) => void; |
| 38 |
/** Accessible label for the nav when there are multiple navs on the page */ |
| 39 |
'aria-label'?: string; |
| 40 |
/** The nav variant to use. Docked is in beta. */ |
| 41 |
variant?: 'default' | 'horizontal' | 'horizontal-subnav' | 'docked'; |
| 42 |
/** @beta Flag indicating the docked nav should display text. Only applies when variant is docked. */ |
| 43 |
isTextExpanded?: boolean; |
| 44 |
/** Value to overwrite the randomly generated data-ouia-component-id.*/ |
| 45 |
ouiaId?: number | string; |
| 46 |
/** Set the value of data-ouia-safe. Only set to true when the component is in a static state, i.e. no animations are occurring. At all other times, this value must be false. */ |
| 47 |
ouiaSafe?: boolean; |
| 48 |
} |
| 49 |
|
| 50 |
export interface NavContextProps { |
| 51 |
onSelect?: ( |
| 52 |
event: React.FormEvent<HTMLInputElement>, |
| 53 |
groupId: number | string, |
| 54 |
itemId: number | string, |
| 55 |
to: string, |
| 56 |
preventDefault: boolean, |
| 57 |
onClick: NavSelectClickHandler |
| 58 |
) => void; |
| 59 |
onToggle?: (event: React.MouseEvent<HTMLButtonElement>, groupId: number | string, expanded: boolean) => void; |
| 60 |
updateIsScrollable?: (isScrollable: boolean) => void; |
| 61 |
isHorizontal?: boolean; |
| 62 |
flyoutRef?: React.Ref<HTMLLIElement>; |
| 63 |
setFlyoutRef?: (ref: React.Ref<HTMLLIElement>) => void; |
| 64 |
navRef?: React.RefObject<HTMLElement | null>; |
| 65 |
} |
| 66 |
export const navContextDefaults = {}; |
| 67 |
export const NavContext = createContext<NavContextProps>(navContextDefaults); |
| 68 |
|
| 69 |
class Nav extends Component<NavProps, { isScrollable: boolean; flyoutRef: React.Ref<HTMLLIElement> | null }> { |
| 70 |
static displayName = 'Nav'; |
| 71 |
static defaultProps: NavProps = { |
| 72 |
onSelect: () => undefined, |
| 73 |
onToggle: () => undefined, |
| 74 |
ouiaSafe: true |
| 75 |
}; |
| 76 |
|
| 77 |
state = { |
| 78 |
isScrollable: false, |
| 79 |
flyoutRef: null as React.Ref<HTMLLIElement> |
| 80 |
}; |
| 81 |
|
| 82 |
navRef = createRef<HTMLElement>(); |
| 83 |
|
| 84 |
// Callback from NavItem |
| 85 |
onSelect( |
| 86 |
event: React.FormEvent<HTMLInputElement>, |
| 87 |
groupId: number | string, |
| 88 |
itemId: number | string, |
| 89 |
to: string, |
| 90 |
preventDefault: boolean, |
| 91 |
onClick: NavSelectClickHandler |
| 92 |
) { |
| 93 |
if (preventDefault) { |
| 94 |
event.preventDefault(); |
| 95 |
} |
| 96 |
|
| 97 |
this.props.onSelect(event, { groupId, itemId, to }); |
| 98 |
|
| 99 |
if (onClick) { |
| 100 |
onClick(event, itemId, groupId, to); |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
// Callback from NavExpandable |
| 105 |
onToggle(event: React.MouseEvent<HTMLButtonElement>, groupId: number | string, toggleValue: boolean) { |
| 106 |
this.props.onToggle(event, { |
| 107 |
groupId, |
| 108 |
isExpanded: toggleValue |
| 109 |
}); |
| 110 |
} |
| 111 |
|
| 112 |
render() { |
| 113 |
const { |
| 114 |
'aria-label': ariaLabel, |
| 115 |
children, |
| 116 |
className, |
| 117 |
// eslint-disable-next-line @typescript-eslint/no-unused-vars |
| 118 |
onSelect, |
| 119 |
// eslint-disable-next-line @typescript-eslint/no-unused-vars |
| 120 |
onToggle, |
| 121 |
ouiaId, |
| 122 |
ouiaSafe, |
| 123 |
variant, |
| 124 |
isTextExpanded = false, |
| 125 |
...props |
| 126 |
} = this.props; |
| 127 |
const isHorizontal = ['horizontal', 'horizontal-subnav'].includes(variant); |
| 128 |
const isDocked = variant === 'docked'; |
| 129 |
return ( |
| 130 |
<SSRSafeIds prefix="pf-" ouiaComponentType={`Nav${variant ? `-${variant}` : ''}`}> |
| 131 |
{(_, generatedOuiaId) => ( |
| 132 |
<NavContext.Provider |
| 133 |
value={{ |
| 134 |
onSelect: ( |
| 135 |
event: React.FormEvent<HTMLInputElement>, |
| 136 |
groupId: number | string, |
| 137 |
itemId: number | string, |
| 138 |
to: string, |
| 139 |
preventDefault: boolean, |
| 140 |
onClick: ( |
| 141 |
e: React.FormEvent<HTMLInputElement>, |
| 142 |
itemId: number | string, |
| 143 |
groupId: number | string, |
| 144 |
to: string |
| 145 |
) => void |
| 146 |
) => this.onSelect(event, groupId, itemId, to, preventDefault, onClick), |
| 147 |
onToggle: (event: React.MouseEvent<HTMLButtonElement>, groupId: number | string, expanded: boolean) => |
| 148 |
this.onToggle(event, groupId, expanded), |
| 149 |
updateIsScrollable: (isScrollable: boolean) => this.setState({ isScrollable }), |
| 150 |
isHorizontal: ['horizontal', 'horizontal-subnav'].includes(variant), |
| 151 |
flyoutRef: this.state.flyoutRef, |
| 152 |
setFlyoutRef: (flyoutRef) => this.setState({ flyoutRef }), |
| 153 |
navRef: this.navRef |
| 154 |
}} |
| 155 |
> |
| 156 |
<nav |
| 157 |
className={css( |
| 158 |
styles.nav, |
| 159 |
isHorizontal && styles.modifiers.horizontal, |
| 160 |
isDocked && styles.modifiers.docked, |
| 161 |
variant === 'horizontal-subnav' && styles.modifiers.subnav, |
| 162 |
isDocked && isTextExpanded && styles.modifiers.textExpanded, |
| 163 |
this.state.isScrollable && styles.modifiers.scrollable, |
| 164 |
className |
| 165 |
)} |
| 166 |
aria-label={ariaLabel || (variant === 'horizontal-subnav' ? 'Local' : 'Global')} |
| 167 |
ref={this.navRef} |
| 168 |
{...getOUIAProps(Nav.displayName, ouiaId !== undefined ? ouiaId : generatedOuiaId, ouiaSafe)} |
| 169 |
{...props} |
| 170 |
> |
| 171 |
{children} |
| 172 |
</nav> |
| 173 |
</NavContext.Provider> |
| 174 |
)} |
| 175 |
</SSRSafeIds> |
| 176 |
); |
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
export { Nav }; |