forked from patternfly/patternfly-react
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNav.tsx
More file actions
173 lines (161 loc) · 5.53 KB
/
Nav.tsx
File metadata and controls
173 lines (161 loc) · 5.53 KB
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
162
163
164
165
166
167
168
169
170
171
172
173
import { Component, createContext, createRef } from 'react';
import styles from '@patternfly/react-styles/css/components/Nav/nav';
import { css } from '@patternfly/react-styles';
import { getOUIAProps, OUIAProps, getDefaultOUIAId } from '../../helpers';
export type NavSelectClickHandler = (
event: React.FormEvent<HTMLInputElement>,
itemId: number | string,
groupId: number | string,
to: string
) => void;
export interface NavProps
extends Omit<React.DetailedHTMLProps<React.HTMLAttributes<HTMLElement>, HTMLElement>, 'onSelect' | 'onToggle'>,
OUIAProps {
/** Anything that can be rendered inside of the nav */
children?: React.ReactNode;
/** Additional classes added to the container */
className?: string;
/** Callback for updating when item selection changes */
onSelect?: (
event: React.FormEvent<HTMLInputElement>,
selectedItem: {
groupId: number | string;
itemId: number | string;
to: string;
}
) => void;
/** Callback for when a list is expanded or collapsed */
onToggle?: (
event: React.MouseEvent<HTMLButtonElement>,
toggledItem: {
groupId: number | string;
isExpanded: boolean;
}
) => void;
/** Accessible label for the nav when there are multiple navs on the page */
'aria-label'?: string;
/** For horizontal navs */
variant?: 'default' | 'horizontal' | 'horizontal-subnav';
/** Value to overwrite the randomly generated data-ouia-component-id.*/
ouiaId?: number | string;
/** 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. */
ouiaSafe?: boolean;
}
export interface NavContextProps {
onSelect?: (
event: React.FormEvent<HTMLInputElement>,
groupId: number | string,
itemId: number | string,
to: string,
preventDefault: boolean,
onClick: NavSelectClickHandler
) => void;
onToggle?: (event: React.MouseEvent<HTMLButtonElement>, groupId: number | string, expanded: boolean) => void;
updateIsScrollable?: (isScrollable: boolean) => void;
isHorizontal?: boolean;
flyoutRef?: React.Ref<HTMLLIElement>;
setFlyoutRef?: (ref: React.Ref<HTMLLIElement>) => void;
navRef?: React.RefObject<HTMLElement | null>;
}
export const navContextDefaults = {};
export const NavContext = createContext<NavContextProps>(navContextDefaults);
class Nav extends Component<
NavProps,
{ isScrollable: boolean; ouiaStateId: string; flyoutRef: React.Ref<HTMLLIElement> | null }
> {
static displayName = 'Nav';
static defaultProps: NavProps = {
onSelect: () => undefined,
onToggle: () => undefined,
ouiaSafe: true
};
state = {
isScrollable: false,
ouiaStateId: getDefaultOUIAId(Nav.displayName, this.props.variant),
flyoutRef: null as React.Ref<HTMLLIElement>
};
navRef = createRef<HTMLElement>();
// Callback from NavItem
onSelect(
event: React.FormEvent<HTMLInputElement>,
groupId: number | string,
itemId: number | string,
to: string,
preventDefault: boolean,
onClick: NavSelectClickHandler
) {
if (preventDefault) {
event.preventDefault();
}
this.props.onSelect(event, { groupId, itemId, to });
if (onClick) {
onClick(event, itemId, groupId, to);
}
}
// Callback from NavExpandable
onToggle(event: React.MouseEvent<HTMLButtonElement>, groupId: number | string, toggleValue: boolean) {
this.props.onToggle(event, {
groupId,
isExpanded: toggleValue
});
}
render() {
const {
'aria-label': ariaLabel,
children,
className,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
onSelect,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
onToggle,
ouiaId,
ouiaSafe,
variant,
...props
} = this.props;
const isHorizontal = ['horizontal', 'horizontal-subnav'].includes(variant);
return (
<NavContext.Provider
value={{
onSelect: (
event: React.FormEvent<HTMLInputElement>,
groupId: number | string,
itemId: number | string,
to: string,
preventDefault: boolean,
onClick: (
e: React.FormEvent<HTMLInputElement>,
itemId: number | string,
groupId: number | string,
to: string
) => void
) => this.onSelect(event, groupId, itemId, to, preventDefault, onClick),
onToggle: (event: React.MouseEvent<HTMLButtonElement>, groupId: number | string, expanded: boolean) =>
this.onToggle(event, groupId, expanded),
updateIsScrollable: (isScrollable: boolean) => this.setState({ isScrollable }),
isHorizontal: ['horizontal', 'horizontal-subnav'].includes(variant),
flyoutRef: this.state.flyoutRef,
setFlyoutRef: (flyoutRef) => this.setState({ flyoutRef }),
navRef: this.navRef
}}
>
<nav
className={css(
styles.nav,
isHorizontal && styles.modifiers.horizontal,
variant === 'horizontal-subnav' && styles.modifiers.subnav,
this.state.isScrollable && styles.modifiers.scrollable,
className
)}
aria-label={ariaLabel || (variant === 'horizontal-subnav' ? 'Local' : 'Global')}
ref={this.navRef}
{...getOUIAProps(Nav.displayName, ouiaId !== undefined ? ouiaId : this.state.ouiaStateId, ouiaSafe)}
{...props}
>
{children}
</nav>
</NavContext.Provider>
);
}
}
export { Nav };