| 1 |
import { Component, createRef } from 'react'; |
| 2 |
import * as ReactDOM from 'react-dom'; |
| 3 |
import styles from '@patternfly/react-styles/css/components/Toolbar/toolbar'; |
| 4 |
import { css } from '@patternfly/react-styles'; |
| 5 |
import { ToolbarGroupProps } from './ToolbarGroup'; |
| 6 |
import { ToolbarContext, ToolbarContentContext } from './ToolbarUtils'; |
| 7 |
import { Button } from '../Button'; |
| 8 |
import globalBreakpointLg from '@patternfly/react-tokens/dist/esm/t_global_breakpoint_lg'; |
| 9 |
import { formatBreakpointMods, toCamel } from '../../helpers/util'; |
| 10 |
import { PageContext } from '../Page/PageContext'; |
| 11 |
import { ToolbarExpandableContent } from './ToolbarExpandableContent'; |
| 12 |
|
| 13 |
export interface ToolbarToggleGroupProps extends ToolbarGroupProps { |
| 14 |
/** Flag indicating when toggle group is expanded for non-managed toolbar toggle groups. */ |
| 15 |
isExpanded?: boolean; |
| 16 |
/** Callback for toggle group click event for non-managed toolbar toggle groups. */ |
| 17 |
onToggle?: (event: React.MouseEvent) => void; |
| 18 |
/** An icon to be rendered when the toggle group has collapsed down */ |
| 19 |
toggleIcon: React.ReactNode; |
| 20 |
/** Controls when filters are shown and when the toggle button is hidden. */ |
| 21 |
breakpoint: 'md' | 'lg' | 'xl' | '2xl'; |
| 22 |
/** Visibility at various breakpoints. */ |
| 23 |
visibility?: { |
| 24 |
default?: 'hidden' | 'visible'; |
| 25 |
md?: 'hidden' | 'visible'; |
| 26 |
lg?: 'hidden' | 'visible'; |
| 27 |
xl?: 'hidden' | 'visible'; |
| 28 |
'2xl'?: 'hidden' | 'visible'; |
| 29 |
}; |
| 30 |
/** Sets both the column and row gap at various breakpoints. */ |
| 31 |
gap?: { |
| 32 |
default?: 'gapNone' | 'gapXs' | 'gapSm' | 'gapMd' | 'gapLg' | 'gapXl' | 'gap_2xl' | 'gap_3xl' | 'gap_4xl'; |
| 33 |
md?: 'gapNone' | 'gapXs' | 'gapSm' | 'gapMd' | 'gapLg' | 'gapXl' | 'gap_2xl' | 'gap_3xl' | 'gap_4xl'; |
| 34 |
lg?: 'gapNone' | 'gapXs' | 'gapSm' | 'gapMd' | 'gapLg' | 'gapXl' | 'gap_2xl' | 'gap_3xl' | 'gap_4xl'; |
| 35 |
xl?: 'gapNone' | 'gapXs' | 'gapSm' | 'gapMd' | 'gapLg' | 'gapXl' | 'gap_2xl' | 'gap_3xl' | 'gap_4xl'; |
| 36 |
'2xl'?: 'gapNone' | 'gapXs' | 'gapSm' | 'gapMd' | 'gapLg' | 'gapXl' | 'gap_2xl' | 'gap_3xl' | 'gap_4xl'; |
| 37 |
}; |
| 38 |
/** Sets only the column gap at various breakpoints. */ |
| 39 |
columnGap?: { |
| 40 |
default?: |
| 41 |
| 'columnGapNone' |
| 42 |
| 'columnGapXs' |
| 43 |
| 'columnGapSm' |
| 44 |
| 'columnGapMd' |
| 45 |
| 'columnGapLg' |
| 46 |
| 'columnGapXl' |
| 47 |
| 'columnGap_2xl' |
| 48 |
| 'columnGap_3xl' |
| 49 |
| 'columnGap_4xl'; |
| 50 |
md?: |
| 51 |
| 'columnGapNone' |
| 52 |
| 'columnGapXs' |
| 53 |
| 'columnGapSm' |
| 54 |
| 'columnGapMd' |
| 55 |
| 'columnGapLg' |
| 56 |
| 'columnGapXl' |
| 57 |
| 'columnGap_2xl' |
| 58 |
| 'columnGap_3xl' |
| 59 |
| 'columnGap_4xl'; |
| 60 |
lg?: |
| 61 |
| 'columnGapNone' |
| 62 |
| 'columnGapXs' |
| 63 |
| 'columnGapSm' |
| 64 |
| 'columnGapMd' |
| 65 |
| 'columnGapLg' |
| 66 |
| 'columnGapXl' |
| 67 |
| 'columnGap_2xl' |
| 68 |
| 'columnGap_3xl' |
| 69 |
| 'columnGap_4xl'; |
| 70 |
xl?: |
| 71 |
| 'columnGapNone' |
| 72 |
| 'columnGapXs' |
| 73 |
| 'columnGapSm' |
| 74 |
| 'columnGapMd' |
| 75 |
| 'columnGapLg' |
| 76 |
| 'columnGapXl' |
| 77 |
| 'columnGap_2xl' |
| 78 |
| 'columnGap_3xl' |
| 79 |
| 'columnGap_4xl'; |
| 80 |
'2xl'?: |
| 81 |
| 'columnGapNone' |
| 82 |
| 'columnGapXs' |
| 83 |
| 'columnGapSm' |
| 84 |
| 'columnGapMd' |
| 85 |
| 'columnGapLg' |
| 86 |
| 'columnGapXl' |
| 87 |
| 'columnGap_2xl' |
| 88 |
| 'columnGap_3xl' |
| 89 |
| 'columnGap_4xl'; |
| 90 |
}; |
| 91 |
/** Sets only the row gap at various breakpoints. */ |
| 92 |
rowGap?: { |
| 93 |
default?: |
| 94 |
| 'rowGapNone' |
| 95 |
| 'rowGapXs' |
| 96 |
| 'rowGapSm' |
| 97 |
| 'rowGapMd' |
| 98 |
| 'rowGapLg' |
| 99 |
| 'rowGapXl' |
| 100 |
| 'rowGap_2xl' |
| 101 |
| 'rowGap_3xl' |
| 102 |
| 'rowGap_4xl'; |
| 103 |
md?: |
| 104 |
| 'rowGapNone' |
| 105 |
| 'rowGapXs' |
| 106 |
| 'rowGapSm' |
| 107 |
| 'rowGapMd' |
| 108 |
| 'rowGapLg' |
| 109 |
| 'rowGapXl' |
| 110 |
| 'rowGap_2xl' |
| 111 |
| 'rowGap_3xl' |
| 112 |
| 'rowGap_4xl'; |
| 113 |
lg?: |
| 114 |
| 'rowGapNone' |
| 115 |
| 'rowGapXs' |
| 116 |
| 'rowGapSm' |
| 117 |
| 'rowGapMd' |
| 118 |
| 'rowGapLg' |
| 119 |
| 'rowGapXl' |
| 120 |
| 'rowGap_2xl' |
| 121 |
| 'rowGap_3xl' |
| 122 |
| 'rowGap_4xl'; |
| 123 |
xl?: |
| 124 |
| 'rowGapNone' |
| 125 |
| 'rowGapXs' |
| 126 |
| 'rowGapSm' |
| 127 |
| 'rowGapMd' |
| 128 |
| 'rowGapLg' |
| 129 |
| 'rowGapXl' |
| 130 |
| 'rowGap_2xl' |
| 131 |
| 'rowGap_3xl' |
| 132 |
| 'rowGap_4xl'; |
| 133 |
'2xl'?: |
| 134 |
| 'rowGapNone' |
| 135 |
| 'rowGapXs' |
| 136 |
| 'rowGapSm' |
| 137 |
| 'rowGapMd' |
| 138 |
| 'rowGapLg' |
| 139 |
| 'rowGapXl' |
| 140 |
| 'rowGap_2xl' |
| 141 |
| 'rowGap_3xl' |
| 142 |
| 'rowGap_4xl'; |
| 143 |
}; |
| 144 |
/** Reference to a label container group for filters inside the toolbar toggle group */ |
| 145 |
labelContainerRef?: React.RefObject<any>; |
| 146 |
/** Optional callback for clearing all filters in the toolbar toggle group */ |
| 147 |
clearAllFilters?: () => void; |
| 148 |
/** Flag indicating that the clear all filters button should be visible in the toolbar toggle group */ |
| 149 |
showClearFiltersButton?: boolean; |
| 150 |
/** Text to display in the clear all filters button of the toolbar toggle group */ |
| 151 |
clearFiltersButtonText?: string; |
| 152 |
} |
| 153 |
|
| 154 |
class ToolbarToggleGroup extends Component<ToolbarToggleGroupProps> { |
| 155 |
static displayName = 'ToolbarToggleGroup'; |
| 156 |
toggleRef = createRef<HTMLButtonElement>(); |
| 157 |
expandableContentRef = createRef<HTMLDivElement>(); |
| 158 |
|
| 159 |
isContentPopup = () => { |
| 160 |
const viewportSize = typeof window !== 'undefined' ? window.innerWidth : 1200; |
| 161 |
const lgBreakpointValue = parseInt(globalBreakpointLg.value); |
| 162 |
return viewportSize < lgBreakpointValue; |
| 163 |
}; |
| 164 |
|
| 165 |
render() { |
| 166 |
const { |
| 167 |
toggleIcon, |
| 168 |
variant, |
| 169 |
visibility, |
| 170 |
breakpoint, |
| 171 |
gap, |
| 172 |
columnGap, |
| 173 |
rowGap, |
| 174 |
className, |
| 175 |
children, |
| 176 |
isExpanded, |
| 177 |
onToggle, |
| 178 |
labelContainerRef, |
| 179 |
clearAllFilters, |
| 180 |
showClearFiltersButton, |
| 181 |
clearFiltersButtonText, |
| 182 |
...props |
| 183 |
} = this.props; |
| 184 |
|
| 185 |
if (!breakpoint && !toggleIcon) { |
| 186 |
// eslint-disable-next-line no-console |
| 187 |
console.error('ToolbarToggleGroup will not be visible without a breakpoint or toggleIcon.'); |
| 188 |
} |
| 189 |
|
| 190 |
return ( |
| 191 |
<PageContext.Consumer> |
| 192 |
{({ width, getBreakpoint }) => ( |
| 193 |
<ToolbarContext.Consumer> |
| 194 |
{({ toggleIsExpanded: managedOnToggle }) => { |
| 195 |
const _onToggle = onToggle !== undefined ? onToggle : managedOnToggle; |
| 196 |
|
| 197 |
return ( |
| 198 |
<ToolbarContentContext.Consumer> |
| 199 |
{({ |
| 200 |
expandableContentRef, |
| 201 |
expandableContentId, |
| 202 |
labelContainerRef: managedLabelContainerRef, |
| 203 |
isExpanded: managedIsExpanded, |
| 204 |
clearAllFilters: clearAllFiltersContext, |
| 205 |
clearFiltersButtonText: clearFiltersButtonContext, |
| 206 |
showClearFiltersButton: showClearFiltersButtonContext |
| 207 |
}) => { |
| 208 |
const _isExpanded = isExpanded !== undefined ? isExpanded : managedIsExpanded; |
| 209 |
const _labelContainerRef = |
| 210 |
labelContainerRef !== undefined ? labelContainerRef : managedLabelContainerRef; |
| 211 |
|
| 212 |
const breakpointMod: { |
| 213 |
md?: 'show'; |
| 214 |
lg?: 'show'; |
| 215 |
xl?: 'show'; |
| 216 |
'2xl'?: 'show'; |
| 217 |
} = {}; |
| 218 |
breakpointMod[breakpoint] = 'show'; |
| 219 |
|
| 220 |
const expandableContent = ( |
| 221 |
<ToolbarExpandableContent |
| 222 |
id={expandableContentId} |
| 223 |
expandableContentRef={this.expandableContentRef} |
| 224 |
isExpanded={_isExpanded} |
| 225 |
clearAllFilters={clearAllFilters || clearAllFiltersContext} |
| 226 |
showClearFiltersButton={showClearFiltersButton || showClearFiltersButtonContext} |
| 227 |
clearFiltersButtonText={clearFiltersButtonText || clearFiltersButtonContext} |
| 228 |
labelContainerRef={_labelContainerRef} |
| 229 |
> |
| 230 |
{children} |
| 231 |
</ToolbarExpandableContent> |
| 232 |
); |
| 233 |
|
| 234 |
const toggleButton = ( |
| 235 |
<div className={css(styles.toolbarToggle)}> |
| 236 |
<Button |
| 237 |
variant="plain" |
| 238 |
onClick={_onToggle} |
| 239 |
aria-label="Show Filters" |
| 240 |
{...(_isExpanded && { 'aria-expanded': true })} |
| 241 |
aria-haspopup={_isExpanded && this.isContentPopup()} |
| 242 |
aria-controls={_isExpanded ? expandableContentId : undefined} |
| 243 |
ref={this.toggleRef} |
| 244 |
icon={toggleIcon} |
| 245 |
/> |
| 246 |
</div> |
| 247 |
); |
| 248 |
|
| 249 |
return ( |
| 250 |
<div |
| 251 |
className={css( |
| 252 |
styles.toolbarGroup, |
| 253 |
styles.modifiers.toggleGroup, |
| 254 |
variant && |
| 255 |
styles.modifiers[ |
| 256 |
toCamel(variant) as |
| 257 |
| 'filterGroup' |
| 258 |
| 'actionGroup' |
| 259 |
| 'actionGroupInline' |
| 260 |
| 'actionGroupPlain' |
| 261 |
| 'labelGroup' |
| 262 |
], |
| 263 |
formatBreakpointMods(breakpointMod, styles, '', getBreakpoint(width)), |
| 264 |
formatBreakpointMods(visibility, styles, '', getBreakpoint(width)), |
| 265 |
formatBreakpointMods(gap, styles, '', getBreakpoint(width)), |
| 266 |
formatBreakpointMods(columnGap, styles, '', getBreakpoint(width)), |
| 267 |
formatBreakpointMods(rowGap, styles, '', getBreakpoint(width)), |
| 268 |
className |
| 269 |
)} |
| 270 |
{...props} |
| 271 |
> |
| 272 |
{toggleButton} |
| 273 |
{_isExpanded && ReactDOM.createPortal(expandableContent, expandableContentRef.current)} |
| 274 |
{!_isExpanded && children} |
| 275 |
</div> |
| 276 |
); |
| 277 |
}} |
| 278 |
</ToolbarContentContext.Consumer> |
| 279 |
); |
| 280 |
}} |
| 281 |
</ToolbarContext.Consumer> |
| 282 |
)} |
| 283 |
</PageContext.Consumer> |
| 284 |
); |
| 285 |
} |
| 286 |
} |
| 287 |
|
| 288 |
export { ToolbarToggleGroup }; |