Debian Codegraph @

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

raw
1 import { Component, RefObject } from 'react';
2 import styles from '@patternfly/react-styles/css/components/Toolbar/toolbar';
3 import { css } from '@patternfly/react-styles';
4
5 import { ToolbarGroup } from './ToolbarGroup';
6 import { ToolbarItem } from './ToolbarItem';
7 import { Button } from '../Button';
8 import { ToolbarContext } from './ToolbarUtils';
9 import { PickOptional } from '../../helpers/typeUtils';
10
11 export interface ToolbarExpandableContentProps extends React.HTMLProps<HTMLDivElement> {
12 /** Classes added to the root element of the data toolbar expandable content */
13 className?: string;
14 /** Flag indicating the expandable content is expanded */
15 isExpanded?: boolean;
16 /** Expandable content reference for passing to data toolbar children */
17 expandableContentRef?: RefObject<HTMLDivElement | null>;
18 /** Label container reference for passing to data toolbar children */
19 labelContainerRef?: RefObject<any>;
20 /** optional callback for clearing all filters in the toolbar */
21 clearAllFilters?: () => void;
22 /** Text to display in the clear all filters button */
23 clearFiltersButtonText?: string;
24 /** Flag indicating that the clear all filters button should be visible */
25 showClearFiltersButton: boolean;
26 }
27
28 class ToolbarExpandableContent extends Component<ToolbarExpandableContentProps> {
29 static displayName = 'ToolbarExpandableContent';
30 static contextType = ToolbarContext;
31 context!: React.ContextType<typeof ToolbarContext>;
32 static defaultProps: PickOptional<ToolbarExpandableContentProps> = {
33 isExpanded: false,
34 clearFiltersButtonText: 'Clear all filters'
35 };
36
37 render() {
38 const {
39 children,
40 className,
41 expandableContentRef,
42 labelContainerRef,
43 isExpanded,
44 clearAllFilters,
45 clearFiltersButtonText,
46 showClearFiltersButton,
47 ...props
48 } = this.props;
49 const { numberOfFilters, customLabelGroupContent } = this.context;
50
51 const clearLabelGroups = () => {
52 clearAllFilters();
53 };
54
55 return (
56 <div
57 className={css(styles.toolbarExpandableContent, isExpanded && styles.modifiers.expanded, className)}
58 ref={expandableContentRef}
59 {...props}
60 >
61 <ToolbarGroup>{children}</ToolbarGroup>
62 {numberOfFilters > 0 && (
63 <ToolbarGroup>
64 <ToolbarGroup ref={labelContainerRef} />
65 {showClearFiltersButton && !customLabelGroupContent && (
66 <ToolbarItem>
67 <Button variant="link" onClick={clearLabelGroups} isInline>
68 {clearFiltersButtonText}
69 </Button>
70 </ToolbarItem>
71 )}
72 {customLabelGroupContent && customLabelGroupContent}
73 </ToolbarGroup>
74 )}
75 </div>
76 );
77 }
78 }
79
80 export { ToolbarExpandableContent };