| 1 |
import { Component } from 'react'; |
| 2 |
import styles from '@patternfly/react-styles/css/components/Nav/nav'; |
| 3 |
import { css } from '@patternfly/react-styles'; |
| 4 |
import RhMicronsCaretDownIcon from '@patternfly/react-icons/dist/esm/icons/rh-microns-caret-down-icon'; |
| 5 |
import { NavContext } from './Nav'; |
| 6 |
import { PageSidebarContext } from '../Page/PageSidebar'; |
| 7 |
import { PickOptional } from '../../helpers/typeUtils'; |
| 8 |
import { getOUIAProps, OUIAProps } from '../../helpers'; |
| 9 |
import { SSRSafeIds } from '../../helpers/SSRSafeIds/SSRSafeIds'; |
| 10 |
|
| 11 |
export interface NavExpandableProps |
| 12 |
extends Omit<React.DetailedHTMLProps<React.LiHTMLAttributes<HTMLLIElement>, HTMLLIElement>, 'title'>, OUIAProps { |
| 13 |
/** Title content shown for the expandable list */ |
| 14 |
title: React.ReactNode; |
| 15 |
/** If defined, screen readers will read this text instead of the list title */ |
| 16 |
srText?: string; |
| 17 |
/** Boolean to pragmatically expand or collapse section */ |
| 18 |
isExpanded?: boolean; |
| 19 |
/** Anything that can be rendered inside of the expandable list */ |
| 20 |
children?: React.ReactNode; |
| 21 |
/** Additional classes added to the container */ |
| 22 |
className?: string; |
| 23 |
/** Group identifier, will be returned with the onToggle and onSelect callback passed to the Nav component */ |
| 24 |
groupId?: string | number; |
| 25 |
/** If true makes the expandable list title active */ |
| 26 |
isActive?: boolean; |
| 27 |
/** Identifier to use for the section aria label */ |
| 28 |
id?: string; |
| 29 |
/** allow consumer to optionally override this callback and manage expand state externally. if passed will not call Nav's onToggle. */ |
| 30 |
onExpand?: (event: React.MouseEvent<HTMLButtonElement, MouseEvent>, val: boolean) => void; |
| 31 |
/** Additional props added to the NavExpandable <button> */ |
| 32 |
buttonProps?: any; |
| 33 |
/** Icon added before the nav item children. */ |
| 34 |
icon?: React.ReactNode; |
| 35 |
/** Value to overwrite the randomly generated data-ouia-component-id.*/ |
| 36 |
ouiaId?: number | string; |
| 37 |
} |
| 38 |
|
| 39 |
interface NavExpandableState { |
| 40 |
expandedState: boolean; |
| 41 |
} |
| 42 |
|
| 43 |
class NavExpandable extends Component<NavExpandableProps, NavExpandableState> { |
| 44 |
static displayName = 'NavExpandable'; |
| 45 |
static defaultProps: PickOptional<NavExpandableProps> = { |
| 46 |
srText: '', |
| 47 |
isExpanded: false, |
| 48 |
children: '', |
| 49 |
className: '', |
| 50 |
groupId: null as string, |
| 51 |
isActive: false, |
| 52 |
id: '' |
| 53 |
}; |
| 54 |
|
| 55 |
state = { |
| 56 |
expandedState: this.props.isExpanded |
| 57 |
}; |
| 58 |
|
| 59 |
componentDidMount() { |
| 60 |
this.setState({ expandedState: this.props.isExpanded }); |
| 61 |
} |
| 62 |
|
| 63 |
componentDidUpdate(prevProps: NavExpandableProps) { |
| 64 |
if (this.props.isExpanded !== prevProps.isExpanded) { |
| 65 |
this.setState({ expandedState: this.props.isExpanded }); |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
onExpand = ( |
| 70 |
event: React.MouseEvent<HTMLButtonElement, MouseEvent>, |
| 71 |
onToggle: ( |
| 72 |
event: React.MouseEvent<HTMLButtonElement, MouseEvent>, |
| 73 |
groupId: string | number, |
| 74 |
expandedState: boolean |
| 75 |
) => void |
| 76 |
) => { |
| 77 |
const { expandedState } = this.state; |
| 78 |
if (this.props.onExpand) { |
| 79 |
this.props.onExpand(event, !expandedState); |
| 80 |
} else { |
| 81 |
this.setState((prevState) => ({ expandedState: !prevState.expandedState })); |
| 82 |
const { groupId } = this.props; |
| 83 |
onToggle(event, groupId, !expandedState); |
| 84 |
} |
| 85 |
}; |
| 86 |
|
| 87 |
render() { |
| 88 |
const { |
| 89 |
icon, |
| 90 |
title, |
| 91 |
srText, |
| 92 |
children, |
| 93 |
className, |
| 94 |
isActive, |
| 95 |
ouiaId, |
| 96 |
// eslint-disable-next-line @typescript-eslint/no-unused-vars |
| 97 |
groupId, |
| 98 |
|
| 99 |
id, |
| 100 |
// eslint-disable-next-line @typescript-eslint/no-unused-vars |
| 101 |
isExpanded, |
| 102 |
buttonProps, |
| 103 |
// eslint-disable-next-line @typescript-eslint/no-unused-vars |
| 104 |
onExpand, |
| 105 |
...props |
| 106 |
} = this.props; |
| 107 |
|
| 108 |
const { expandedState } = this.state; |
| 109 |
|
| 110 |
return ( |
| 111 |
<SSRSafeIds prefix="pf-nav-expandable-" ouiaComponentType="NavExpandable"> |
| 112 |
{(generatedId, generatedOuiaId) => { |
| 113 |
const navId = id || generatedId; |
| 114 |
|
| 115 |
return ( |
| 116 |
<NavContext.Consumer> |
| 117 |
{(context) => ( |
| 118 |
<li |
| 119 |
className={css( |
| 120 |
styles.navItem, |
| 121 |
expandedState && styles.modifiers.expanded, |
| 122 |
isActive && styles.modifiers.current, |
| 123 |
className |
| 124 |
)} |
| 125 |
{...getOUIAProps(NavExpandable.displayName, ouiaId !== undefined ? ouiaId : generatedOuiaId)} |
| 126 |
{...props} |
| 127 |
> |
| 128 |
<PageSidebarContext.Consumer> |
| 129 |
{({ isSidebarOpen }) => ( |
| 130 |
<button |
| 131 |
className={css(styles.navLink)} |
| 132 |
id={srText ? null : navId} |
| 133 |
onClick={(event) => this.onExpand(event, context.onToggle)} |
| 134 |
aria-expanded={expandedState} |
| 135 |
tabIndex={isSidebarOpen ? null : -1} |
| 136 |
{...buttonProps} |
| 137 |
> |
| 138 |
{icon && <span className={css(styles.navLinkIcon)}>{icon}</span>} |
| 139 |
{typeof title !== 'string' ? <span className={css(styles.navLinkText)}>{title}</span> : title} |
| 140 |
<span className={css(styles.navToggle)}> |
| 141 |
<span className={css(styles.navToggleIcon)}> |
| 142 |
<RhMicronsCaretDownIcon /> |
| 143 |
</span> |
| 144 |
</span> |
| 145 |
</button> |
| 146 |
)} |
| 147 |
</PageSidebarContext.Consumer> |
| 148 |
<section |
| 149 |
className={css(styles.navSubnav)} |
| 150 |
aria-labelledby={navId} |
| 151 |
hidden={expandedState ? null : true} |
| 152 |
{...(!expandedState && { inert: '' })} |
| 153 |
> |
| 154 |
{srText && ( |
| 155 |
<h2 className="pf-v6-screen-reader" id={navId}> |
| 156 |
{srText} |
| 157 |
</h2> |
| 158 |
)} |
| 159 |
<ul className={css(styles.navList)} role="list"> |
| 160 |
{children} |
| 161 |
</ul> |
| 162 |
</section> |
| 163 |
</li> |
| 164 |
)} |
| 165 |
</NavContext.Consumer> |
| 166 |
); |
| 167 |
}} |
| 168 |
</SSRSafeIds> |
| 169 |
); |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
export { NavExpandable }; |