| 1 |
import { Component, forwardRef } from 'react'; |
| 2 |
import styles from '@patternfly/react-styles/css/components/Toolbar/toolbar'; |
| 3 |
import { css } from '@patternfly/react-styles'; |
| 4 |
import { formatBreakpointMods, toCamel } from '../../helpers/util'; |
| 5 |
import { PageContext } from '../Page/PageContext'; |
| 6 |
|
| 7 |
export enum ToolbarGroupVariant { |
| 8 |
'filter-group' = 'filter-group', |
| 9 |
'action-group' = 'action-group', |
| 10 |
'action-group-inline' = 'action-group-inline', |
| 11 |
'action-group-plain' = 'action-group-plain', |
| 12 |
'label-group' = 'label-group' |
| 13 |
} |
| 14 |
|
| 15 |
export interface ToolbarGroupProps extends Omit<React.HTMLProps<HTMLDivElement>, 'ref' | 'onToggle'> { |
| 16 |
/** Classes applied to root element of the data toolbar group */ |
| 17 |
className?: string; |
| 18 |
/** A type modifier which modifies spacing specifically depending on the type of group */ |
| 19 |
variant?: |
| 20 |
| ToolbarGroupVariant |
| 21 |
| 'filter-group' |
| 22 |
| 'action-group' |
| 23 |
| 'action-group-inline' |
| 24 |
| 'action-group-plain' |
| 25 |
| 'label-group'; |
| 26 |
/** Visibility at various width breakpoints. */ |
| 27 |
visibility?: { |
| 28 |
default?: 'hidden' | 'visible'; |
| 29 |
md?: 'hidden' | 'visible'; |
| 30 |
lg?: 'hidden' | 'visible'; |
| 31 |
xl?: 'hidden' | 'visible'; |
| 32 |
'2xl'?: 'hidden' | 'visible'; |
| 33 |
}; |
| 34 |
/** Visibility at various height breakpoints. */ |
| 35 |
visibilityAtHeight?: { |
| 36 |
default?: 'hidden' | 'visible'; |
| 37 |
md?: 'hidden' | 'visible'; |
| 38 |
lg?: 'hidden' | 'visible'; |
| 39 |
xl?: 'hidden' | 'visible'; |
| 40 |
'2xl'?: 'hidden' | 'visible'; |
| 41 |
}; |
| 42 |
/** Applies to a child of a flex layout, and aligns that child (and any adjacent children on the other side of it) to one side of the main axis */ |
| 43 |
align?: { |
| 44 |
default?: 'alignEnd' | 'alignStart' | 'alignCenter'; |
| 45 |
md?: 'alignEnd' | 'alignStart' | 'alignCenter'; |
| 46 |
lg?: 'alignEnd' | 'alignStart' | 'alignCenter'; |
| 47 |
xl?: 'alignEnd' | 'alignStart' | 'alignCenter'; |
| 48 |
'2xl'?: 'alignEnd' | 'alignStart' | 'alignCenter'; |
| 49 |
}; |
| 50 |
/** Vertical alignment of children */ |
| 51 |
alignItems?: 'start' | 'center' | 'baseline' | 'default' | 'end' | 'stretch'; |
| 52 |
/** Vertical alignment */ |
| 53 |
alignSelf?: 'start' | 'center' | 'baseline' | 'default' | 'end' | 'stretch'; |
| 54 |
/** Sets both the column and row gap at various breakpoints. */ |
| 55 |
gap?: { |
| 56 |
default?: 'gapNone' | 'gapXs' | 'gapSm' | 'gapMd' | 'gapLg' | 'gapXl' | 'gap_2xl' | 'gap_3xl' | 'gap_4xl'; |
| 57 |
md?: 'gapNone' | 'gapXs' | 'gapSm' | 'gapMd' | 'gapLg' | 'gapXl' | 'gap_2xl' | 'gap_3xl' | 'gap_4xl'; |
| 58 |
lg?: 'gapNone' | 'gapXs' | 'gapSm' | 'gapMd' | 'gapLg' | 'gapXl' | 'gap_2xl' | 'gap_3xl' | 'gap_4xl'; |
| 59 |
xl?: 'gapNone' | 'gapXs' | 'gapSm' | 'gapMd' | 'gapLg' | 'gapXl' | 'gap_2xl' | 'gap_3xl' | 'gap_4xl'; |
| 60 |
'2xl'?: 'gapNone' | 'gapXs' | 'gapSm' | 'gapMd' | 'gapLg' | 'gapXl' | 'gap_2xl' | 'gap_3xl' | 'gap_4xl'; |
| 61 |
}; |
| 62 |
/** Sets only the column gap at various breakpoints. */ |
| 63 |
columnGap?: { |
| 64 |
default?: |
| 65 |
| 'columnGapNone' |
| 66 |
| 'columnGapXs' |
| 67 |
| 'columnGapSm' |
| 68 |
| 'columnGapMd' |
| 69 |
| 'columnGapLg' |
| 70 |
| 'columnGapXl' |
| 71 |
| 'columnGap_2xl' |
| 72 |
| 'columnGap_3xl' |
| 73 |
| 'columnGap_4xl'; |
| 74 |
md?: |
| 75 |
| 'columnGapNone' |
| 76 |
| 'columnGapXs' |
| 77 |
| 'columnGapSm' |
| 78 |
| 'columnGapMd' |
| 79 |
| 'columnGapLg' |
| 80 |
| 'columnGapXl' |
| 81 |
| 'columnGap_2xl' |
| 82 |
| 'columnGap_3xl' |
| 83 |
| 'columnGap_4xl'; |
| 84 |
lg?: |
| 85 |
| 'columnGapNone' |
| 86 |
| 'columnGapXs' |
| 87 |
| 'columnGapSm' |
| 88 |
| 'columnGapMd' |
| 89 |
| 'columnGapLg' |
| 90 |
| 'columnGapXl' |
| 91 |
| 'columnGap_2xl' |
| 92 |
| 'columnGap_3xl' |
| 93 |
| 'columnGap_4xl'; |
| 94 |
xl?: |
| 95 |
| 'columnGapNone' |
| 96 |
| 'columnGapXs' |
| 97 |
| 'columnGapSm' |
| 98 |
| 'columnGapMd' |
| 99 |
| 'columnGapLg' |
| 100 |
| 'columnGapXl' |
| 101 |
| 'columnGap_2xl' |
| 102 |
| 'columnGap_3xl' |
| 103 |
| 'columnGap_4xl'; |
| 104 |
'2xl'?: |
| 105 |
| 'columnGapNone' |
| 106 |
| 'columnGapXs' |
| 107 |
| 'columnGapSm' |
| 108 |
| 'columnGapMd' |
| 109 |
| 'columnGapLg' |
| 110 |
| 'columnGapXl' |
| 111 |
| 'columnGap_2xl' |
| 112 |
| 'columnGap_3xl' |
| 113 |
| 'columnGap_4xl'; |
| 114 |
}; |
| 115 |
/** Sets only the row gap at various breakpoints. */ |
| 116 |
rowGap?: { |
| 117 |
default?: |
| 118 |
| 'rowGapNone' |
| 119 |
| 'rowGapXs' |
| 120 |
| 'rowGapSm' |
| 121 |
| 'rowGapMd' |
| 122 |
| 'rowGapLg' |
| 123 |
| 'rowGapXl' |
| 124 |
| 'rowGap_2xl' |
| 125 |
| 'rowGap_3xl' |
| 126 |
| 'rowGap_4xl'; |
| 127 |
md?: |
| 128 |
| 'rowGapNone' |
| 129 |
| 'rowGapXs' |
| 130 |
| 'rowGapSm' |
| 131 |
| 'rowGapMd' |
| 132 |
| 'rowGapLg' |
| 133 |
| 'rowGapXl' |
| 134 |
| 'rowGap_2xl' |
| 135 |
| 'rowGap_3xl' |
| 136 |
| 'rowGap_4xl'; |
| 137 |
lg?: |
| 138 |
| 'rowGapNone' |
| 139 |
| 'rowGapXs' |
| 140 |
| 'rowGapSm' |
| 141 |
| 'rowGapMd' |
| 142 |
| 'rowGapLg' |
| 143 |
| 'rowGapXl' |
| 144 |
| 'rowGap_2xl' |
| 145 |
| 'rowGap_3xl' |
| 146 |
| 'rowGap_4xl'; |
| 147 |
xl?: |
| 148 |
| 'rowGapNone' |
| 149 |
| 'rowGapXs' |
| 150 |
| 'rowGapSm' |
| 151 |
| 'rowGapMd' |
| 152 |
| 'rowGapLg' |
| 153 |
| 'rowGapXl' |
| 154 |
| 'rowGap_2xl' |
| 155 |
| 'rowGap_3xl' |
| 156 |
| 'rowGap_4xl'; |
| 157 |
'2xl'?: |
| 158 |
| 'rowGapNone' |
| 159 |
| 'rowGapXs' |
| 160 |
| 'rowGapSm' |
| 161 |
| 'rowGapMd' |
| 162 |
| 'rowGapLg' |
| 163 |
| 'rowGapXl' |
| 164 |
| 'rowGap_2xl' |
| 165 |
| 'rowGap_3xl' |
| 166 |
| 'rowGap_4xl'; |
| 167 |
}; |
| 168 |
/** Value to set for row wrapping at various breakpoints */ |
| 169 |
rowWrap?: { |
| 170 |
default?: 'wrap' | 'nowrap'; |
| 171 |
sm?: 'wrap' | 'nowrap'; |
| 172 |
md?: 'wrap' | 'nowrap'; |
| 173 |
lg?: 'wrap' | 'nowrap'; |
| 174 |
xl?: 'wrap' | 'nowrap'; |
| 175 |
'2xl'?: 'wrap' | 'nowrap'; |
| 176 |
}; |
| 177 |
/** Content to be rendered inside the data toolbar group */ |
| 178 |
children?: React.ReactNode; |
| 179 |
/** Flag that modifies the toolbar group to hide overflow and respond to available space. Used for horizontal navigation. */ |
| 180 |
isOverflowContainer?: boolean; |
| 181 |
/** @hide Reference to pass to this group if it has .pf-m-label-container modifier */ |
| 182 |
innerRef?: React.RefObject<any>; |
| 183 |
} |
| 184 |
|
| 185 |
class ToolbarGroupWithRef extends Component<ToolbarGroupProps> { |
| 186 |
render() { |
| 187 |
const { |
| 188 |
visibility, |
| 189 |
visibilityAtHeight, |
| 190 |
align, |
| 191 |
alignItems, |
| 192 |
alignSelf, |
| 193 |
gap, |
| 194 |
columnGap, |
| 195 |
rowGap, |
| 196 |
rowWrap, |
| 197 |
className, |
| 198 |
variant, |
| 199 |
children, |
| 200 |
isOverflowContainer, |
| 201 |
innerRef, |
| 202 |
...props |
| 203 |
} = this.props; |
| 204 |
|
| 205 |
return ( |
| 206 |
<PageContext.Consumer> |
| 207 |
{({ width, getBreakpoint, height, getVerticalBreakpoint }) => ( |
| 208 |
<div |
| 209 |
className={css( |
| 210 |
styles.toolbarGroup, |
| 211 |
variant && |
| 212 |
styles.modifiers[ |
| 213 |
toCamel(variant) as |
| 214 |
| 'filterGroup' |
| 215 |
| 'actionGroup' |
| 216 |
| 'actionGroupInline' |
| 217 |
| 'actionGroupPlain' |
| 218 |
| 'labelGroup' |
| 219 |
], |
| 220 |
formatBreakpointMods(visibility, styles, '', getBreakpoint(width)), |
| 221 |
formatBreakpointMods(visibilityAtHeight, styles, '', getVerticalBreakpoint(height), true), |
| 222 |
formatBreakpointMods(align, styles, '', getBreakpoint(width)), |
| 223 |
formatBreakpointMods(gap, styles, '', getBreakpoint(width)), |
| 224 |
formatBreakpointMods(columnGap, styles, '', getBreakpoint(width)), |
| 225 |
formatBreakpointMods(rowGap, styles, '', getBreakpoint(width)), |
| 226 |
formatBreakpointMods(rowWrap, styles, '', getBreakpoint(width)), |
| 227 |
alignItems === 'start' && styles.modifiers.alignItemsStart, |
| 228 |
alignItems === 'center' && styles.modifiers.alignItemsCenter, |
| 229 |
alignItems === 'baseline' && styles.modifiers.alignItemsBaseline, |
| 230 |
alignSelf === 'start' && styles.modifiers.alignSelfStart, |
| 231 |
alignSelf === 'center' && styles.modifiers.alignSelfCenter, |
| 232 |
alignSelf === 'baseline' && styles.modifiers.alignSelfBaseline, |
| 233 |
isOverflowContainer && styles.modifiers.overflowContainer, |
| 234 |
className |
| 235 |
)} |
| 236 |
{...props} |
| 237 |
ref={innerRef} |
| 238 |
> |
| 239 |
{children} |
| 240 |
</div> |
| 241 |
)} |
| 242 |
</PageContext.Consumer> |
| 243 |
); |
| 244 |
} |
| 245 |
} |
| 246 |
|
| 247 |
export const ToolbarGroup = forwardRef((props: ToolbarGroupProps, ref: any) => ( |
| 248 |
<ToolbarGroupWithRef {...props} innerRef={ref} /> |
| 249 |
)); |