Debian Codegraph @

node/@patternfly/react-core/src/components/Toolbar/Toolbar.tsx

raw
1 import { Component, createRef } from 'react';
2 import styles from '@patternfly/react-styles/css/components/Toolbar/toolbar';
3 import { css } from '@patternfly/react-styles';
4 import { ToolbarContext } from './ToolbarUtils';
5 import { ToolbarLabelGroupContent } from './ToolbarLabelGroupContent';
6 import { formatBreakpointMods, canUseDOM } from '../../helpers/util';
7 import { getOUIAProps, OUIAProps } from '../../helpers';
8 import { SSRSafeIds } from '../../helpers/SSRSafeIds/SSRSafeIds';
9 import { PageContext } from '../Page/PageContext';
10
11 export enum ToolbarColorVariant {
12 default = 'default',
13 primary = 'primary',
14 secondary = 'secondary',
15 noBackground = 'no-background'
16 }
17
18 export interface ToolbarProps extends React.HTMLProps<HTMLDivElement>, OUIAProps {
19 /** Optional callback for clearing all filters in the toolbar */
20 clearAllFilters?: () => void;
21 /** Text to display in the clear all filters button */
22 clearFiltersButtonText?: string;
23 /** 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. */
24 customLabelGroupContent?: React.ReactNode;
25 /** The breakpoint at which the listed filters in label groups are collapsed down to a summary */
26 collapseListedFiltersBreakpoint?: 'all' | 'md' | 'lg' | 'xl' | '2xl';
27 /** Flag indicating if a data toolbar toggle group's expandable content is expanded */
28 isExpanded?: boolean;
29 /** A callback for setting the isExpanded flag */
30 toggleIsExpanded?: () => void;
31 /** Classes applied to root element of the data toolbar */
32 className?: string;
33 /** Content to be rendered as rows in the data toolbar */
34 children?: React.ReactNode;
35 /** Id of the data toolbar */
36 id?: string;
37 /** Flag indicating the toolbar height should expand to the full height of the container */
38 isFullHeight?: boolean;
39 /** Flag indicating the toolbar is static */
40 isStatic?: boolean;
41 /** Flag indicating the toolbar should stick to the top of its container. This property applies both the sticky position and styling. */
42 isSticky?: boolean;
43 /** @beta Flag indicating the toolbar should have sticky positioning to the top of its container */
44 isStickyBase?: boolean;
45 /** @beta Flag indicating the toolbar should have stuck styling, when the toolbar is not at the top of the scroll container */
46 isStickyStuck?: boolean;
47 /** @beta Flag indicating the toolbar has a vertical orientation */
48 isVertical?: boolean;
49 /** Insets at various breakpoints. */
50 inset?: {
51 default?: 'insetNone' | 'insetSm' | 'insetMd' | 'insetLg' | 'insetXl' | 'inset2xl';
52 sm?: 'insetNone' | 'insetSm' | 'insetMd' | 'insetLg' | 'insetXl' | 'inset2xl';
53 md?: 'insetNone' | 'insetSm' | 'insetMd' | 'insetLg' | 'insetXl' | 'inset2xl';
54 lg?: 'insetNone' | 'insetSm' | 'insetMd' | 'insetLg' | 'insetXl' | 'inset2xl';
55 xl?: 'insetNone' | 'insetSm' | 'insetMd' | 'insetLg' | 'insetXl' | 'inset2xl';
56 '2xl'?: 'insetNone' | 'insetSm' | 'insetMd' | 'insetLg' | 'insetXl' | 'inset2xl';
57 };
58 /** Text to display in the total number of applied filters ToolbarFilter */
59 numberOfFiltersText?: (numberOfFilters: number) => string;
60 /** Value to overwrite the randomly generated data-ouia-component-id.*/
61 ouiaId?: number | string;
62 /** 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. */
63 ouiaSafe?: boolean;
64 /** Background color variant of the toolbar */
65 colorVariant?: ToolbarColorVariant | 'default' | 'no-background' | 'primary' | 'secondary';
66 /** Flag indicating the toolbar padding is removed */
67 hasNoPadding?: boolean;
68 }
69
70 export interface ToolbarState {
71 /** Flag used if the user has opted NOT to manage the 'isExpanded' state of the toggle group.
72 * Indicates whether or not the toggle group is expanded. */
73 isManagedToggleExpanded: boolean;
74 /** Object managing information about how many labels are in each label group */
75 filterInfo: FilterInfo;
76 /** Used to keep track of window width so we can collapse expanded content when window is resizing */
77 windowWidth: number;
78 }
79
80 interface FilterInfo {
81 [key: string]: number;
82 }
83
84 class Toolbar extends Component<ToolbarProps, ToolbarState> {
85 static displayName = 'Toolbar';
86 labelGroupContentRef = createRef<HTMLDivElement>();
87 staticFilterInfo = {};
88 hasNoPadding = false;
89 state = {
90 isManagedToggleExpanded: false,
91 filterInfo: {},
92 windowWidth: 1200
93 };
94
95 isToggleManaged = () => !(this.props.isExpanded || !!this.props.toggleIsExpanded);
96
97 toggleIsExpanded = () => {
98 this.setState((prevState) => ({
99 isManagedToggleExpanded: !prevState.isManagedToggleExpanded
100 }));
101 };
102
103 closeExpandableContent = (e: any) => {
104 if (e.target.innerWidth !== this.state.windowWidth) {
105 this.setState(() => ({
106 isManagedToggleExpanded: false,
107 windowWidth: e.target.innerWidth
108 }));
109 }
110 };
111
112 componentDidMount() {
113 if (canUseDOM) {
114 this.setState({ windowWidth: window.innerWidth });
115 }
116 if (this.isToggleManaged() && canUseDOM) {
117 window.addEventListener('resize', this.closeExpandableContent);
118 }
119 }
120
121 componentWillUnmount() {
122 if (this.isToggleManaged() && canUseDOM) {
123 window.removeEventListener('resize', this.closeExpandableContent);
124 }
125 }
126
127 updateNumberFilters = (categoryName: string, numberOfFilters: number) => {
128 const filterInfoToUpdate: FilterInfo = { ...this.staticFilterInfo };
129 if (!filterInfoToUpdate.hasOwnProperty(categoryName) || filterInfoToUpdate[categoryName] !== numberOfFilters) {
130 filterInfoToUpdate[categoryName] = numberOfFilters;
131 this.staticFilterInfo = filterInfoToUpdate;
132 this.setState({ filterInfo: filterInfoToUpdate });
133 }
134 };
135
136 getNumberOfFilters = () =>
137 Object.values(this.state.filterInfo).reduce((acc: any, cur: any) => acc + cur, 0) as number;
138
139 renderToolbar = (randomId: string, generatedOuiaId?: string) => {
140 const {
141 hasNoPadding,
142 clearAllFilters,
143 clearFiltersButtonText,
144 collapseListedFiltersBreakpoint,
145 isExpanded: isExpandedProp,
146 toggleIsExpanded,
147 className,
148 children,
149 isFullHeight,
150 isStatic,
151 isStickyBase,
152 isStickyStuck,
153 inset,
154 isSticky,
155 isVertical,
156 ouiaId,
157 numberOfFiltersText,
158 customLabelGroupContent,
159 colorVariant = ToolbarColorVariant.default,
160 ...props
161 } = this.props;
162
163 const { isManagedToggleExpanded } = this.state;
164
165 const isToggleManaged = this.isToggleManaged();
166 const isExpanded = isToggleManaged ? isManagedToggleExpanded : isExpandedProp;
167 const numberOfFilters = this.getNumberOfFilters();
168 const showClearFiltersButton = numberOfFilters > 0;
169
170 return (
171 <PageContext.Consumer>
172 {({ width, getBreakpoint }) => (
173 <div
174 className={css(
175 styles.toolbar,
176 hasNoPadding && styles.modifiers.noPadding,
177 isFullHeight && styles.modifiers.fullHeight,
178 isStatic && styles.modifiers.static,
179 isSticky && styles.modifiers.sticky,
180 isStickyBase && styles.modifiers.stickyBase,
181 isStickyStuck && styles.modifiers.stickyStuck,
182 isVertical && styles.modifiers.vertical,
183 formatBreakpointMods(inset, styles, '', getBreakpoint(width)),
184 colorVariant === 'primary' && styles.modifiers.primary,
185 colorVariant === 'secondary' && styles.modifiers.secondary,
186 colorVariant === 'no-background' && styles.modifiers.noBackground,
187 className
188 )}
189 id={randomId}
190 {...getOUIAProps(Toolbar.displayName, ouiaId !== undefined ? ouiaId : generatedOuiaId)}
191 {...props}
192 >
193 <ToolbarContext.Provider
194 value={{
195 isExpanded,
196 toggleIsExpanded: isToggleManaged ? this.toggleIsExpanded : toggleIsExpanded,
197 labelGroupContentRef: this.labelGroupContentRef,
198 updateNumberFilters: this.updateNumberFilters,
199 numberOfFilters,
200 clearAllFilters,
201 clearFiltersButtonText,
202 showClearFiltersButton,
203 toolbarId: randomId,
204 customLabelGroupContent
205 }}
206 >
207 {children}
208 <ToolbarLabelGroupContent
209 isExpanded={isExpanded}
210 labelGroupContentRef={this.labelGroupContentRef}
211 clearAllFilters={clearAllFilters}
212 showClearFiltersButton={showClearFiltersButton}
213 clearFiltersButtonText={clearFiltersButtonText}
214 numberOfFilters={numberOfFilters}
215 numberOfFiltersText={numberOfFiltersText}
216 collapseListedFiltersBreakpoint={collapseListedFiltersBreakpoint}
217 customLabelGroupContent={customLabelGroupContent}
218 />
219 </ToolbarContext.Provider>
220 </div>
221 )}
222 </PageContext.Consumer>
223 );
224 };
225
226 render() {
227 return (
228 <SSRSafeIds prefix="pf-random-id-" ouiaComponentType="Toolbar">
229 {(generatedId, generatedOuiaId) => this.renderToolbar(this.props.id || generatedId, generatedOuiaId)}
230 </SSRSafeIds>
231 );
232 }
233 }
234
235 export { Toolbar };