-
Notifications
You must be signed in to change notification settings - Fork 382
Expand file tree
/
Copy pathToolbarToggleGroup.tsx
More file actions
293 lines (281 loc) · 10.1 KB
/
ToolbarToggleGroup.tsx
File metadata and controls
293 lines (281 loc) · 10.1 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
293
import { Component, createRef } from 'react';
import * as ReactDOM from 'react-dom';
import styles from '@patternfly/react-styles/css/components/Toolbar/toolbar';
import { css } from '@patternfly/react-styles';
import { ToolbarGroupProps } from './ToolbarGroup';
import { ToolbarContext, ToolbarContentContext } from './ToolbarUtils';
import { Button } from '../Button';
import globalBreakpointLg from '@patternfly/react-tokens/dist/esm/t_global_breakpoint_lg';
import { formatBreakpointMods, toCamel, canUseDOM } from '../../helpers/util';
import { PageContext } from '../Page/PageContext';
import { ToolbarExpandableContent } from './ToolbarExpandableContent';
export interface ToolbarToggleGroupProps extends ToolbarGroupProps {
/** Flag indicating when toggle group is expanded for non-managed toolbar toggle groups. */
isExpanded?: boolean;
/** Callback for toggle group click event for non-managed toolbar toggle groups. */
onToggle?: (event: React.MouseEvent) => void;
/** An icon to be rendered when the toggle group has collapsed down */
toggleIcon: React.ReactNode;
/** Controls when filters are shown and when the toggle button is hidden. */
breakpoint: 'md' | 'lg' | 'xl' | '2xl';
/** Visibility at various breakpoints. */
visibility?: {
default?: 'hidden' | 'visible';
md?: 'hidden' | 'visible';
lg?: 'hidden' | 'visible';
xl?: 'hidden' | 'visible';
'2xl'?: 'hidden' | 'visible';
};
/** Sets both the column and row gap at various breakpoints. */
gap?: {
default?: 'gapNone' | 'gapXs' | 'gapSm' | 'gapMd' | 'gapLg' | 'gapXl' | 'gap_2xl' | 'gap_3xl' | 'gap_4xl';
md?: 'gapNone' | 'gapXs' | 'gapSm' | 'gapMd' | 'gapLg' | 'gapXl' | 'gap_2xl' | 'gap_3xl' | 'gap_4xl';
lg?: 'gapNone' | 'gapXs' | 'gapSm' | 'gapMd' | 'gapLg' | 'gapXl' | 'gap_2xl' | 'gap_3xl' | 'gap_4xl';
xl?: 'gapNone' | 'gapXs' | 'gapSm' | 'gapMd' | 'gapLg' | 'gapXl' | 'gap_2xl' | 'gap_3xl' | 'gap_4xl';
'2xl'?: 'gapNone' | 'gapXs' | 'gapSm' | 'gapMd' | 'gapLg' | 'gapXl' | 'gap_2xl' | 'gap_3xl' | 'gap_4xl';
};
/** Sets only the column gap at various breakpoints. */
columnGap?: {
default?:
| 'columnGapNone'
| 'columnGapXs'
| 'columnGapSm'
| 'columnGapMd'
| 'columnGapLg'
| 'columnGapXl'
| 'columnGap_2xl'
| 'columnGap_3xl'
| 'columnGap_4xl';
md?:
| 'columnGapNone'
| 'columnGapXs'
| 'columnGapSm'
| 'columnGapMd'
| 'columnGapLg'
| 'columnGapXl'
| 'columnGap_2xl'
| 'columnGap_3xl'
| 'columnGap_4xl';
lg?:
| 'columnGapNone'
| 'columnGapXs'
| 'columnGapSm'
| 'columnGapMd'
| 'columnGapLg'
| 'columnGapXl'
| 'columnGap_2xl'
| 'columnGap_3xl'
| 'columnGap_4xl';
xl?:
| 'columnGapNone'
| 'columnGapXs'
| 'columnGapSm'
| 'columnGapMd'
| 'columnGapLg'
| 'columnGapXl'
| 'columnGap_2xl'
| 'columnGap_3xl'
| 'columnGap_4xl';
'2xl'?:
| 'columnGapNone'
| 'columnGapXs'
| 'columnGapSm'
| 'columnGapMd'
| 'columnGapLg'
| 'columnGapXl'
| 'columnGap_2xl'
| 'columnGap_3xl'
| 'columnGap_4xl';
};
/** Sets only the row gap at various breakpoints. */
rowGap?: {
default?:
| 'rowGapNone'
| 'rowGapXs'
| 'rowGapSm'
| 'rowGapMd'
| 'rowGapLg'
| 'rowGapXl'
| 'rowGap_2xl'
| 'rowGap_3xl'
| 'rowGap_4xl';
md?:
| 'rowGapNone'
| 'rowGapXs'
| 'rowGapSm'
| 'rowGapMd'
| 'rowGapLg'
| 'rowGapXl'
| 'rowGap_2xl'
| 'rowGap_3xl'
| 'rowGap_4xl';
lg?:
| 'rowGapNone'
| 'rowGapXs'
| 'rowGapSm'
| 'rowGapMd'
| 'rowGapLg'
| 'rowGapXl'
| 'rowGap_2xl'
| 'rowGap_3xl'
| 'rowGap_4xl';
xl?:
| 'rowGapNone'
| 'rowGapXs'
| 'rowGapSm'
| 'rowGapMd'
| 'rowGapLg'
| 'rowGapXl'
| 'rowGap_2xl'
| 'rowGap_3xl'
| 'rowGap_4xl';
'2xl'?:
| 'rowGapNone'
| 'rowGapXs'
| 'rowGapSm'
| 'rowGapMd'
| 'rowGapLg'
| 'rowGapXl'
| 'rowGap_2xl'
| 'rowGap_3xl'
| 'rowGap_4xl';
};
/** Reference to a label container group for filters inside the toolbar toggle group */
labelContainerRef?: React.RefObject<any>;
/** Optional callback for clearing all filters in the toolbar toggle group */
clearAllFilters?: () => void;
/** Flag indicating that the clear all filters button should be visible in the toolbar toggle group */
showClearFiltersButton?: boolean;
/** Text to display in the clear all filters button of the toolbar toggle group */
clearFiltersButtonText?: string;
}
class ToolbarToggleGroup extends Component<ToolbarToggleGroupProps> {
static displayName = 'ToolbarToggleGroup';
toggleRef = createRef<HTMLButtonElement>();
expandableContentRef = createRef<HTMLDivElement>();
isContentPopup = () => {
const viewportSize = canUseDOM ? window.innerWidth : 1200;
const lgBreakpointValue = parseInt(globalBreakpointLg.value);
return viewportSize < lgBreakpointValue;
};
render() {
const {
toggleIcon,
variant,
visibility,
breakpoint,
gap,
columnGap,
rowGap,
className,
children,
isExpanded,
onToggle,
labelContainerRef,
clearAllFilters,
showClearFiltersButton,
clearFiltersButtonText,
...props
} = this.props;
if (!breakpoint && !toggleIcon) {
// eslint-disable-next-line no-console
console.error('ToolbarToggleGroup will not be visible without a breakpoint or toggleIcon.');
}
return (
<PageContext.Consumer>
{({ width, getBreakpoint }) => (
<ToolbarContext.Consumer>
{({ toggleIsExpanded: managedOnToggle, useContainerQuery }) => {
const _onToggle = onToggle !== undefined ? onToggle : managedOnToggle;
return (
<ToolbarContentContext.Consumer>
{({
expandableContentRef,
expandableContentId,
labelContainerRef: managedLabelContainerRef,
isExpanded: managedIsExpanded,
clearAllFilters: clearAllFiltersContext,
clearFiltersButtonText: clearFiltersButtonContext,
showClearFiltersButton: showClearFiltersButtonContext
}) => {
const _isExpanded = isExpanded !== undefined ? isExpanded : managedIsExpanded;
const _labelContainerRef =
labelContainerRef !== undefined ? labelContainerRef : managedLabelContainerRef;
const breakpointMod: {
md?: 'show';
lg?: 'show';
xl?: 'show';
'2xl'?: 'show';
} = {};
breakpointMod[breakpoint] = 'show';
const expandableContent = (
<ToolbarExpandableContent
id={expandableContentId}
expandableContentRef={this.expandableContentRef}
isExpanded={_isExpanded}
clearAllFilters={clearAllFilters || clearAllFiltersContext}
showClearFiltersButton={showClearFiltersButton || showClearFiltersButtonContext}
clearFiltersButtonText={clearFiltersButtonText || clearFiltersButtonContext}
labelContainerRef={_labelContainerRef}
>
{children}
</ToolbarExpandableContent>
);
const toggleButton = (
<div className={css(styles.toolbarToggle)}>
<Button
variant="plain"
onClick={_onToggle}
aria-label="Show Filters"
{...(_isExpanded && { 'aria-expanded': true })}
aria-haspopup={_isExpanded && this.isContentPopup()}
aria-controls={_isExpanded ? expandableContentId : undefined}
ref={this.toggleRef}
icon={toggleIcon}
/>
</div>
);
return (
<div
className={css(
styles.toolbarGroup,
styles.modifiers.toggleGroup,
variant &&
styles.modifiers[
toCamel(variant) as
| 'filterGroup'
| 'actionGroup'
| 'actionGroupInline'
| 'actionGroupPlain'
| 'labelGroup'
],
formatBreakpointMods(
breakpointMod,
styles,
'',
useContainerQuery ? undefined : getBreakpoint(width)
),
formatBreakpointMods(visibility, styles, '', getBreakpoint(width)),
formatBreakpointMods(gap, styles, '', getBreakpoint(width)),
formatBreakpointMods(columnGap, styles, '', getBreakpoint(width)),
formatBreakpointMods(rowGap, styles, '', getBreakpoint(width)),
className
)}
{...props}
>
{toggleButton}
{_isExpanded && ReactDOM.createPortal(expandableContent, expandableContentRef.current)}
{!_isExpanded && children}
</div>
);
}}
</ToolbarContentContext.Consumer>
);
}}
</ToolbarContext.Consumer>
)}
</PageContext.Consumer>
);
}
}
export { ToolbarToggleGroup };