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