Debian Codegraph @

node/@patternfly/react-core/dist/esm/components/Toolbar/Toolbar.js

raw
1 import { __rest } from "tslib";
2 import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
3 import { Component, createRef } from 'react';
4 import styles from '@patternfly/react-styles/css/components/Toolbar/toolbar.mjs';
5 import { css } from '@patternfly/react-styles';
6 import { ToolbarContext } from './ToolbarUtils';
7 import { ToolbarLabelGroupContent } from './ToolbarLabelGroupContent';
8 import { formatBreakpointMods, canUseDOM } from '../../helpers/util';
9 import { getOUIAProps } from '../../helpers';
10 import { SSRSafeIds } from '../../helpers/SSRSafeIds/SSRSafeIds';
11 import { PageContext } from '../Page/PageContext';
12 export var ToolbarColorVariant;
13 (function (ToolbarColorVariant) {
14 ToolbarColorVariant["default"] = "default";
15 ToolbarColorVariant["primary"] = "primary";
16 ToolbarColorVariant["secondary"] = "secondary";
17 ToolbarColorVariant["noBackground"] = "no-background";
18 })(ToolbarColorVariant || (ToolbarColorVariant = {}));
19 class Toolbar extends Component {
20 constructor() {
21 super(...arguments);
22 this.labelGroupContentRef = createRef();
23 this.staticFilterInfo = {};
24 this.hasNoPadding = false;
25 this.state = {
26 isManagedToggleExpanded: false,
27 filterInfo: {},
28 windowWidth: 1200
29 };
30 this.isToggleManaged = () => !(this.props.isExpanded || !!this.props.toggleIsExpanded);
31 this.toggleIsExpanded = () => {
32 this.setState((prevState) => ({
33 isManagedToggleExpanded: !prevState.isManagedToggleExpanded
34 }));
35 };
36 this.closeExpandableContent = (e) => {
37 if (e.target.innerWidth !== this.state.windowWidth) {
38 this.setState(() => ({
39 isManagedToggleExpanded: false,
40 windowWidth: e.target.innerWidth
41 }));
42 }
43 };
44 this.updateNumberFilters = (categoryName, numberOfFilters) => {
45 const filterInfoToUpdate = Object.assign({}, this.staticFilterInfo);
46 if (!filterInfoToUpdate.hasOwnProperty(categoryName) || filterInfoToUpdate[categoryName] !== numberOfFilters) {
47 filterInfoToUpdate[categoryName] = numberOfFilters;
48 this.staticFilterInfo = filterInfoToUpdate;
49 this.setState({ filterInfo: filterInfoToUpdate });
50 }
51 };
52 this.getNumberOfFilters = () => Object.values(this.state.filterInfo).reduce((acc, cur) => acc + cur, 0);
53 this.renderToolbar = (randomId, generatedOuiaId) => {
54 const _a = this.props, { hasNoPadding, clearAllFilters, clearFiltersButtonText, collapseListedFiltersBreakpoint, isExpanded: isExpandedProp, toggleIsExpanded, className, children, isFullHeight, isStatic, isStickyBase, isStickyStuck, inset, isSticky, isVertical, ouiaId, numberOfFiltersText, customLabelGroupContent, colorVariant = ToolbarColorVariant.default } = _a, props = __rest(_a, ["hasNoPadding", "clearAllFilters", "clearFiltersButtonText", "collapseListedFiltersBreakpoint", "isExpanded", "toggleIsExpanded", "className", "children", "isFullHeight", "isStatic", "isStickyBase", "isStickyStuck", "inset", "isSticky", "isVertical", "ouiaId", "numberOfFiltersText", "customLabelGroupContent", "colorVariant"]);
55 const { isManagedToggleExpanded } = this.state;
56 const isToggleManaged = this.isToggleManaged();
57 const isExpanded = isToggleManaged ? isManagedToggleExpanded : isExpandedProp;
58 const numberOfFilters = this.getNumberOfFilters();
59 const showClearFiltersButton = numberOfFilters > 0;
60 return (_jsx(PageContext.Consumer, { children: ({ width, getBreakpoint }) => (_jsx("div", Object.assign({ className: css(styles.toolbar, hasNoPadding && styles.modifiers.noPadding, isFullHeight && styles.modifiers.fullHeight, isStatic && styles.modifiers.static, isSticky && styles.modifiers.sticky, isStickyBase && styles.modifiers.stickyBase, isStickyStuck && styles.modifiers.stickyStuck, isVertical && styles.modifiers.vertical, formatBreakpointMods(inset, styles, '', getBreakpoint(width)), colorVariant === 'primary' && styles.modifiers.primary, colorVariant === 'secondary' && styles.modifiers.secondary, colorVariant === 'no-background' && styles.modifiers.noBackground, className), id: randomId }, getOUIAProps(Toolbar.displayName, ouiaId !== undefined ? ouiaId : generatedOuiaId), props, { children: _jsxs(ToolbarContext.Provider, { value: {
61 isExpanded,
62 toggleIsExpanded: isToggleManaged ? this.toggleIsExpanded : toggleIsExpanded,
63 labelGroupContentRef: this.labelGroupContentRef,
64 updateNumberFilters: this.updateNumberFilters,
65 numberOfFilters,
66 clearAllFilters,
67 clearFiltersButtonText,
68 showClearFiltersButton,
69 toolbarId: randomId,
70 customLabelGroupContent
71 }, children: [children, _jsx(ToolbarLabelGroupContent, { isExpanded: isExpanded, labelGroupContentRef: this.labelGroupContentRef, clearAllFilters: clearAllFilters, showClearFiltersButton: showClearFiltersButton, clearFiltersButtonText: clearFiltersButtonText, numberOfFilters: numberOfFilters, numberOfFiltersText: numberOfFiltersText, collapseListedFiltersBreakpoint: collapseListedFiltersBreakpoint, customLabelGroupContent: customLabelGroupContent })] }) }))) }));
72 };
73 }
74 componentDidMount() {
75 if (canUseDOM) {
76 this.setState({ windowWidth: window.innerWidth });
77 }
78 if (this.isToggleManaged() && canUseDOM) {
79 window.addEventListener('resize', this.closeExpandableContent);
80 }
81 }
82 componentWillUnmount() {
83 if (this.isToggleManaged() && canUseDOM) {
84 window.removeEventListener('resize', this.closeExpandableContent);
85 }
86 }
87 render() {
88 return (_jsx(SSRSafeIds, { prefix: "pf-random-id-", ouiaComponentType: "Toolbar", children: (generatedId, generatedOuiaId) => this.renderToolbar(this.props.id || generatedId, generatedOuiaId) }));
89 }
90 }
91 Toolbar.displayName = 'Toolbar';
92 export { Toolbar };
93 //# sourceMappingURL=Toolbar.js.map