-
Notifications
You must be signed in to change notification settings - Fork 382
Expand file tree
/
Copy pathToolbar.tsx
More file actions
292 lines (270 loc) · 11.7 KB
/
Toolbar.tsx
File metadata and controls
292 lines (270 loc) · 11.7 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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
import { Component, createRef } from 'react';
import styles from '@patternfly/react-styles/css/components/Toolbar/toolbar';
import { GenerateId } from '../../helpers/GenerateId/GenerateId';
import { css } from '@patternfly/react-styles';
import { ToolbarContext, globalBreakpoints, containerBreakpoints } from './ToolbarUtils';
import { ToolbarLabelGroupContent } from './ToolbarLabelGroupContent';
import { formatBreakpointMods, canUseDOM } from '../../helpers/util';
import { getDefaultOUIAId, getOUIAProps, OUIAProps } from '../../helpers';
import { PageContext } from '../Page/PageContext';
import { getResizeObserver } from '../../helpers/resizeObserver';
export enum ToolbarColorVariant {
default = 'default',
primary = 'primary',
secondary = 'secondary',
noBackground = 'no-background'
}
export interface ToolbarProps extends React.HTMLProps<HTMLDivElement>, OUIAProps {
/** Optional callback for clearing all filters in the toolbar */
clearAllFilters?: () => void;
/** Text to display in the clear all filters button */
clearFiltersButtonText?: string;
/** Custom content appended to the filter generated label group. To maintain spacing and styling, each node should be wrapped in a ToolbarItem or ToolbarGroup. This property will remove the default "Clear all filters" button. */
customLabelGroupContent?: React.ReactNode;
/** The breakpoint at which the listed filters in label groups are collapsed down to a summary */
collapseListedFiltersBreakpoint?: 'all' | 'md' | 'lg' | 'xl' | '2xl';
/** Flag indicating if a data toolbar toggle group's expandable content is expanded */
isExpanded?: boolean;
/** A callback for setting the isExpanded flag */
toggleIsExpanded?: () => void;
/** Classes applied to root element of the data toolbar */
className?: string;
/** Content to be rendered as rows in the data toolbar */
children?: React.ReactNode;
/** Id of the data toolbar */
id?: string;
/** Flag indicating the toolbar height should expand to the full height of the container */
isFullHeight?: boolean;
/** Flag indicating the toolbar is static */
isStatic?: boolean;
/** Flag indicating the toolbar should stick to the top of its container */
isSticky?: boolean;
/** Insets at various breakpoints. */
inset?: {
default?: 'insetNone' | 'insetSm' | 'insetMd' | 'insetLg' | 'insetXl' | 'inset2xl';
sm?: 'insetNone' | 'insetSm' | 'insetMd' | 'insetLg' | 'insetXl' | 'inset2xl';
md?: 'insetNone' | 'insetSm' | 'insetMd' | 'insetLg' | 'insetXl' | 'inset2xl';
lg?: 'insetNone' | 'insetSm' | 'insetMd' | 'insetLg' | 'insetXl' | 'inset2xl';
xl?: 'insetNone' | 'insetSm' | 'insetMd' | 'insetLg' | 'insetXl' | 'inset2xl';
'2xl'?: 'insetNone' | 'insetSm' | 'insetMd' | 'insetLg' | 'insetXl' | 'inset2xl';
};
/** Text to display in the total number of applied filters ToolbarFilter */
numberOfFiltersText?: (numberOfFilters: number) => string;
/** 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;
/** Background color variant of the toolbar */
colorVariant?: ToolbarColorVariant | 'default' | 'no-background' | 'primary' | 'secondary';
/** Flag indicating the toolbar padding is removed */
hasNoPadding?: boolean;
/** Use container queries instead of viewport media queries for responsive behavior */
useContainerQuery?: boolean;
/** Breakpoint for container queries. Only applies when useContainerQuery is true. */
containerQueryBreakpoint?: 'sm' | 'md' | 'lg' | 'xl' | '2xl';
}
export interface ToolbarState {
/** Flag used if the user has opted NOT to manage the 'isExpanded' state of the toggle group.
* Indicates whether or not the toggle group is expanded. */
isManagedToggleExpanded: boolean;
/** Object managing information about how many labels are in each label group */
filterInfo: FilterInfo;
/** Used to keep track of window width so we can collapse expanded content when window is resizing */
windowWidth: number;
/** Used to keep track of container width so we can collapse expanded content when container is resizing */
containerWidth: number;
ouiaStateId: string;
}
interface FilterInfo {
[key: string]: number;
}
class Toolbar extends Component<ToolbarProps, ToolbarState> {
static displayName = 'Toolbar';
labelGroupContentRef = createRef<HTMLDivElement>();
toolbarRef = createRef<HTMLDivElement>();
observer: any = () => {};
staticFilterInfo = {};
hasNoPadding = false;
state = {
isManagedToggleExpanded: false,
filterInfo: {},
windowWidth: canUseDOM ? window.innerWidth : 1200,
containerWidth: 0,
ouiaStateId: getDefaultOUIAId(Toolbar.displayName)
};
isToggleManaged = () => !(this.props.isExpanded || !!this.props.toggleIsExpanded);
toggleIsExpanded = () => {
this.setState((prevState) => ({
isManagedToggleExpanded: !prevState.isManagedToggleExpanded
}));
};
closeExpandableContent = (e: any) => {
if (e.target.innerWidth !== this.state.windowWidth) {
this.setState(() => ({
isManagedToggleExpanded: false,
windowWidth: e.target.innerWidth
}));
}
};
closeExpandableContentOnContainerResize = () => {
if (this.toolbarRef.current && this.toolbarRef.current.clientWidth) {
const newWidth = this.toolbarRef.current.clientWidth;
if (newWidth !== this.state.containerWidth) {
// If expanded and container is wide enough for inline display at the specific breakpoint, close it
const specificBreakpoint = this.props.containerQueryBreakpoint || 'lg';
// Use container breakpoints when using container queries, otherwise use global breakpoints
let isWideEnoughForInline: boolean;
if (this.props.useContainerQuery) {
// Handle 'sm' case for container breakpoints
const breakpointKey = specificBreakpoint === 'sm' ? 'sm' : specificBreakpoint;
isWideEnoughForInline = newWidth >= containerBreakpoints[breakpointKey];
} else {
// Handle 'sm' case - map to 'md' since globalBreakpoints doesn't have 'sm'
const breakpointKey = specificBreakpoint === 'sm' ? 'md' : specificBreakpoint;
isWideEnoughForInline = newWidth >= globalBreakpoints[breakpointKey];
}
if (this.state.isManagedToggleExpanded && isWideEnoughForInline) {
this.setState(() => ({
isManagedToggleExpanded: false,
containerWidth: newWidth
}));
} else {
// Just update width without closing
this.setState(() => ({
containerWidth: newWidth
}));
}
}
}
};
componentDidMount() {
if (this.isToggleManaged() && canUseDOM) {
if (this.props.useContainerQuery && this.toolbarRef.current) {
this.observer = getResizeObserver(this.toolbarRef.current, this.closeExpandableContentOnContainerResize, true);
} else {
window.addEventListener('resize', this.closeExpandableContent);
}
}
}
componentWillUnmount() {
if (this.isToggleManaged() && canUseDOM) {
if (this.props.useContainerQuery) {
this.observer();
} else {
window.removeEventListener('resize', this.closeExpandableContent);
}
}
}
updateNumberFilters = (categoryName: string, numberOfFilters: number) => {
const filterInfoToUpdate: FilterInfo = { ...this.staticFilterInfo };
if (!filterInfoToUpdate.hasOwnProperty(categoryName) || filterInfoToUpdate[categoryName] !== numberOfFilters) {
filterInfoToUpdate[categoryName] = numberOfFilters;
this.staticFilterInfo = filterInfoToUpdate;
this.setState({ filterInfo: filterInfoToUpdate });
}
};
getNumberOfFilters = () =>
Object.values(this.state.filterInfo).reduce((acc: any, cur: any) => acc + cur, 0) as number;
renderToolbar = (randomId: string) => {
const {
hasNoPadding,
clearAllFilters,
clearFiltersButtonText,
collapseListedFiltersBreakpoint,
isExpanded: isExpandedProp,
toggleIsExpanded,
className,
children,
isFullHeight,
isStatic,
inset,
isSticky,
ouiaId,
numberOfFiltersText,
customLabelGroupContent,
colorVariant = ToolbarColorVariant.default,
useContainerQuery,
containerQueryBreakpoint,
...props
} = this.props;
const { isManagedToggleExpanded } = this.state;
const isToggleManaged = this.isToggleManaged();
const isExpanded = isToggleManaged ? isManagedToggleExpanded : isExpandedProp;
const numberOfFilters = this.getNumberOfFilters();
const showClearFiltersButton = numberOfFilters > 0;
return (
<PageContext.Consumer>
{({ width, getBreakpoint }) => (
<div
className={css(
styles.toolbar,
hasNoPadding && styles.modifiers.noPadding,
isFullHeight && styles.modifiers.fullHeight,
isStatic && styles.modifiers.static,
isSticky && styles.modifiers.sticky,
useContainerQuery && !containerQueryBreakpoint && styles.modifiers.container,
useContainerQuery &&
containerQueryBreakpoint &&
((): string => {
const breakpointClassMap: Record<string, string> = {
'2xl': styles.modifiers.container_2xl,
sm: styles.modifiers.containerSm,
md: styles.modifiers.containerMd,
lg: styles.modifiers.containerLg,
xl: styles.modifiers.containerXl
};
return breakpointClassMap[containerQueryBreakpoint] || '';
})(),
formatBreakpointMods(inset, styles, '', getBreakpoint(width)),
colorVariant === 'primary' && styles.modifiers.primary,
colorVariant === 'secondary' && styles.modifiers.secondary,
colorVariant === 'no-background' && styles.modifiers.noBackground,
className
)}
id={randomId}
ref={this.toolbarRef}
{...getOUIAProps(Toolbar.displayName, ouiaId !== undefined ? ouiaId : this.state.ouiaStateId)}
{...props}
>
<ToolbarContext.Provider
value={{
isExpanded,
toggleIsExpanded: isToggleManaged ? this.toggleIsExpanded : toggleIsExpanded,
labelGroupContentRef: this.labelGroupContentRef,
updateNumberFilters: this.updateNumberFilters,
numberOfFilters,
clearAllFilters,
clearFiltersButtonText,
showClearFiltersButton,
toolbarId: randomId,
customLabelGroupContent,
useContainerQuery
}}
>
{children}
<ToolbarLabelGroupContent
isExpanded={isExpanded}
labelGroupContentRef={this.labelGroupContentRef}
clearAllFilters={clearAllFilters}
showClearFiltersButton={showClearFiltersButton}
clearFiltersButtonText={clearFiltersButtonText}
numberOfFilters={numberOfFilters}
numberOfFiltersText={numberOfFiltersText}
collapseListedFiltersBreakpoint={collapseListedFiltersBreakpoint}
customLabelGroupContent={customLabelGroupContent}
/>
</ToolbarContext.Provider>
</div>
)}
</PageContext.Consumer>
);
};
render() {
return this.props.id ? (
this.renderToolbar(this.props.id)
) : (
<GenerateId>{(randomId) => this.renderToolbar(randomId)}</GenerateId>
);
}
}
export { Toolbar };