| 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 { ToolbarContentContext, ToolbarContext } from './ToolbarUtils'; |
| 5 |
import { formatBreakpointMods } from '../../helpers/util'; |
| 6 |
import { PageContext } from '../Page/PageContext'; |
| 7 |
|
| 8 |
export interface ToolbarContentProps extends React.HTMLProps<HTMLDivElement> { |
| 9 |
/** Classes applied to root element of the data toolbar content row */ |
| 10 |
className?: string; |
| 11 |
/** Visibility at various width breakpoints. */ |
| 12 |
visibility?: { |
| 13 |
default?: 'hidden' | 'visible'; |
| 14 |
md?: 'hidden' | 'visible'; |
| 15 |
lg?: 'hidden' | 'visible'; |
| 16 |
xl?: 'hidden' | 'visible'; |
| 17 |
'2xl'?: 'hidden' | 'visible'; |
| 18 |
}; |
| 19 |
/** Visibility at various height breakpoints. */ |
| 20 |
visibilityAtHeight?: { |
| 21 |
default?: 'hidden' | 'visible'; |
| 22 |
md?: 'hidden' | 'visible'; |
| 23 |
lg?: 'hidden' | 'visible'; |
| 24 |
xl?: 'hidden' | 'visible'; |
| 25 |
'2xl'?: 'hidden' | 'visible'; |
| 26 |
}; |
| 27 |
/** Value to set for content wrapping at various breakpoints */ |
| 28 |
rowWrap?: { |
| 29 |
default?: 'wrap' | 'nowrap'; |
| 30 |
sm?: 'wrap' | 'nowrap'; |
| 31 |
md?: 'wrap' | 'nowrap'; |
| 32 |
lg?: 'wrap' | 'nowrap'; |
| 33 |
xl?: 'wrap' | 'nowrap'; |
| 34 |
'2xl'?: 'wrap' | 'nowrap'; |
| 35 |
}; |
| 36 |
/** Vertical alignment of children */ |
| 37 |
alignItems?: 'start' | 'center' | 'baseline' | 'default'; |
| 38 |
/** Content to be rendered as children of the content row */ |
| 39 |
children?: React.ReactNode; |
| 40 |
/** Flag indicating if a data toolbar toggle group's expandable content is expanded */ |
| 41 |
isExpanded?: boolean; |
| 42 |
/** Optional callback for clearing all filters in the toolbar */ |
| 43 |
clearAllFilters?: () => void; |
| 44 |
/** Flag indicating that the clear all filters button should be visible */ |
| 45 |
showClearFiltersButton?: boolean; |
| 46 |
/** Text to display in the clear all filters button */ |
| 47 |
clearFiltersButtonText?: string; |
| 48 |
/** Id of the parent Toolbar component */ |
| 49 |
toolbarId?: string; |
| 50 |
} |
| 51 |
|
| 52 |
class ToolbarContent extends Component<ToolbarContentProps> { |
| 53 |
static displayName = 'ToolbarContent'; |
| 54 |
private expandableContentRef = createRef<HTMLDivElement>(); |
| 55 |
private labelContainerRef = createRef<HTMLDivElement>(); |
| 56 |
private static currentId = 0; |
| 57 |
|
| 58 |
static defaultProps: ToolbarContentProps = { |
| 59 |
isExpanded: false, |
| 60 |
showClearFiltersButton: false |
| 61 |
}; |
| 62 |
|
| 63 |
render() { |
| 64 |
const { |
| 65 |
className, |
| 66 |
children, |
| 67 |
isExpanded, |
| 68 |
toolbarId, |
| 69 |
visibility, |
| 70 |
visibilityAtHeight, |
| 71 |
rowWrap, |
| 72 |
alignItems, |
| 73 |
clearAllFilters, |
| 74 |
showClearFiltersButton, |
| 75 |
clearFiltersButtonText, |
| 76 |
...props |
| 77 |
} = this.props; |
| 78 |
|
| 79 |
return ( |
| 80 |
<PageContext.Consumer> |
| 81 |
{({ width, getBreakpoint, height, getVerticalBreakpoint }) => ( |
| 82 |
<div |
| 83 |
className={css( |
| 84 |
styles.toolbarContent, |
| 85 |
formatBreakpointMods(visibility, styles, '', getBreakpoint(width)), |
| 86 |
formatBreakpointMods(visibilityAtHeight, styles, '', getVerticalBreakpoint(height), true), |
| 87 |
className |
| 88 |
)} |
| 89 |
ref={this.expandableContentRef} |
| 90 |
{...props} |
| 91 |
> |
| 92 |
<ToolbarContext.Consumer> |
| 93 |
{({ |
| 94 |
clearAllFilters: clearAllFiltersContext, |
| 95 |
clearFiltersButtonText: clearFiltersButtonContext, |
| 96 |
showClearFiltersButton: showClearFiltersButtonContext, |
| 97 |
isExpanded: isExpandedContext, |
| 98 |
toolbarId: toolbarIdContext |
| 99 |
}) => { |
| 100 |
const expandableContentId = `${ |
| 101 |
toolbarId || toolbarIdContext |
| 102 |
}-expandable-content-${ToolbarContent.currentId++}`; |
| 103 |
return ( |
| 104 |
<ToolbarContentContext.Provider |
| 105 |
value={{ |
| 106 |
expandableContentRef: this.expandableContentRef, |
| 107 |
expandableContentId, |
| 108 |
labelContainerRef: this.labelContainerRef, |
| 109 |
isExpanded: isExpanded || isExpandedContext, |
| 110 |
clearAllFilters: clearAllFilters || clearAllFiltersContext, |
| 111 |
clearFiltersButtonText: clearFiltersButtonText || clearFiltersButtonContext, |
| 112 |
showClearFiltersButton: showClearFiltersButton || showClearFiltersButtonContext |
| 113 |
}} |
| 114 |
> |
| 115 |
<div |
| 116 |
className={css( |
| 117 |
styles.toolbarContentSection, |
| 118 |
formatBreakpointMods(rowWrap, styles, '', getBreakpoint(width)), |
| 119 |
alignItems === 'center' && styles.modifiers.alignItemsCenter, |
| 120 |
alignItems === 'start' && styles.modifiers.alignItemsStart, |
| 121 |
alignItems === 'baseline' && styles.modifiers.alignItemsBaseline |
| 122 |
)} |
| 123 |
> |
| 124 |
{children} |
| 125 |
</div> |
| 126 |
</ToolbarContentContext.Provider> |
| 127 |
); |
| 128 |
}} |
| 129 |
</ToolbarContext.Consumer> |
| 130 |
</div> |
| 131 |
)} |
| 132 |
</PageContext.Consumer> |
| 133 |
); |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
export { ToolbarContent }; |