| 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 { ToolbarItem } from './ToolbarItem'; |
| 6 |
import { Button } from '../Button'; |
| 7 |
import { ToolbarGroup } from './ToolbarGroup'; |
| 8 |
import { globalBreakpoints } from './ToolbarUtils'; |
| 9 |
import { PickOptional } from '../../helpers/typeUtils'; |
| 10 |
|
| 11 |
export interface ToolbarLabelGroupContentProps extends React.HTMLProps<HTMLDivElement> { |
| 12 |
/** Classes applied to root element of the data toolbar content row */ |
| 13 |
className?: string; |
| 14 |
/** Flag indicating if a data toolbar toggle group's expandable content is expanded */ |
| 15 |
isExpanded?: boolean; |
| 16 |
/** Label group content reference for passing to data toolbar children */ |
| 17 |
labelGroupContentRef?: RefObject<any>; |
| 18 |
/** optional callback for clearing all filters in the toolbar */ |
| 19 |
clearAllFilters?: () => void; |
| 20 |
/** Flag indicating that the clear all filters button should be visible */ |
| 21 |
showClearFiltersButton: boolean; |
| 22 |
/** Text to display in the clear all filters button */ |
| 23 |
clearFiltersButtonText?: string; |
| 24 |
/** Total number of filters currently being applied across all ToolbarFilter components */ |
| 25 |
numberOfFilters: number; |
| 26 |
/** Text to display in the total number of applied filters ToolbarFilter */ |
| 27 |
numberOfFiltersText?: (numberOfFilters: number) => string; |
| 28 |
/** The breakpoint at which the listed filters in label groups are collapsed down to a summary */ |
| 29 |
collapseListedFiltersBreakpoint?: 'all' | 'md' | 'lg' | 'xl' | '2xl'; |
| 30 |
/** Custom additional content appended to the generated labels. To maintain spacing and styling, each node should be a ToolbarItem or ToolbarGroup. This property will remove the built in "Clear all filters" button. */ |
| 31 |
customLabelGroupContent?: React.ReactNode; |
| 32 |
} |
| 33 |
|
| 34 |
class ToolbarLabelGroupContent extends Component<ToolbarLabelGroupContentProps, { viewportWidth: number }> { |
| 35 |
static displayName = 'ToolbarLabelGroupContent'; |
| 36 |
static defaultProps: PickOptional<ToolbarLabelGroupContentProps> = { |
| 37 |
clearFiltersButtonText: 'Clear all filters', |
| 38 |
collapseListedFiltersBreakpoint: 'lg', |
| 39 |
numberOfFiltersText: (numberOfFilters: number) => `${numberOfFilters} filters applied` |
| 40 |
}; |
| 41 |
|
| 42 |
state = { |
| 43 |
viewportWidth: 1200 |
| 44 |
}; |
| 45 |
|
| 46 |
handleResize = () => { |
| 47 |
this.setState({ viewportWidth: window.innerWidth }); |
| 48 |
}; |
| 49 |
|
| 50 |
componentDidMount() { |
| 51 |
this.handleResize(); |
| 52 |
window.addEventListener('resize', this.handleResize); |
| 53 |
} |
| 54 |
|
| 55 |
componentWillUnmount() { |
| 56 |
window.removeEventListener('resize', this.handleResize); |
| 57 |
} |
| 58 |
|
| 59 |
render() { |
| 60 |
const { |
| 61 |
className, |
| 62 |
isExpanded, |
| 63 |
labelGroupContentRef, |
| 64 |
clearAllFilters, |
| 65 |
showClearFiltersButton, |
| 66 |
clearFiltersButtonText, |
| 67 |
collapseListedFiltersBreakpoint, |
| 68 |
numberOfFilters, |
| 69 |
numberOfFiltersText, |
| 70 |
customLabelGroupContent, |
| 71 |
...props |
| 72 |
} = this.props; |
| 73 |
|
| 74 |
const clearLabelGroups = () => { |
| 75 |
clearAllFilters(); |
| 76 |
}; |
| 77 |
|
| 78 |
let collapseListedFilters = false; |
| 79 |
if (collapseListedFiltersBreakpoint === 'all') { |
| 80 |
collapseListedFilters = true; |
| 81 |
} else if (collapseListedFiltersBreakpoint) { |
| 82 |
collapseListedFilters = this.state.viewportWidth < globalBreakpoints[collapseListedFiltersBreakpoint]; |
| 83 |
} |
| 84 |
|
| 85 |
const isHidden = numberOfFilters === 0 || isExpanded; |
| 86 |
const showNumberOfFilters = collapseListedFilters && numberOfFilters > 0 && !isExpanded; |
| 87 |
const showDefaultClearFilter = showClearFiltersButton && !isExpanded && !customLabelGroupContent; |
| 88 |
|
| 89 |
return ( |
| 90 |
<div |
| 91 |
className={css(styles.toolbarContent, isHidden && styles.modifiers.hidden, className)} |
| 92 |
{...((numberOfFilters === 0 || isExpanded) && { hidden: true })} |
| 93 |
ref={labelGroupContentRef} |
| 94 |
{...props} |
| 95 |
> |
| 96 |
<ToolbarGroup |
| 97 |
className={css(collapseListedFilters && styles.modifiers.hidden)} |
| 98 |
{...(collapseListedFilters && { hidden: true })} |
| 99 |
{...(collapseListedFilters && { 'aria-hidden': true })} |
| 100 |
/> |
| 101 |
{(showNumberOfFilters || showDefaultClearFilter || customLabelGroupContent) && ( |
| 102 |
<ToolbarGroup variant="action-group-inline"> |
| 103 |
{showNumberOfFilters && <ToolbarItem>{numberOfFiltersText(numberOfFilters)}</ToolbarItem>} |
| 104 |
{showDefaultClearFilter && ( |
| 105 |
<ToolbarItem> |
| 106 |
<Button variant="link" onClick={clearLabelGroups} isInline> |
| 107 |
{clearFiltersButtonText} |
| 108 |
</Button> |
| 109 |
</ToolbarItem> |
| 110 |
)} |
| 111 |
{customLabelGroupContent && customLabelGroupContent} |
| 112 |
</ToolbarGroup> |
| 113 |
)} |
| 114 |
</div> |
| 115 |
); |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
export { ToolbarLabelGroupContent }; |