| 1 |
import { Component, forwardRef, isValidElement } from 'react'; |
| 2 |
import styles from '@patternfly/react-styles/css/components/MenuToggle/menu-toggle'; |
| 3 |
import { css } from '@patternfly/react-styles'; |
| 4 |
import RhMicronsCaretDownFillIcon from '@patternfly/react-icons/dist/esm/icons/rh-microns-caret-down-fill-icon'; |
| 5 |
import { BadgeProps } from '../Badge'; |
| 6 |
import RhUiSettingsFillIcon from '@patternfly/react-icons/dist/esm/icons/rh-ui-settings-fill-icon'; |
| 7 |
import RhUiCheckCircleFillIcon from '@patternfly/react-icons/dist/esm/icons/rh-ui-check-circle-fill-icon'; |
| 8 |
import RhUiErrorFillIcon from '@patternfly/react-icons/dist/esm/icons/rh-ui-error-fill-icon'; |
| 9 |
import RhUiWarningFillIcon from '@patternfly/react-icons/dist/esm/icons/rh-ui-warning-fill-icon'; |
| 10 |
import { OUIAProps, getOUIAProps } from '../../helpers'; |
| 11 |
import { SSRSafeIds } from '../../helpers/SSRSafeIds/SSRSafeIds'; |
| 12 |
|
| 13 |
export enum MenuToggleStatus { |
| 14 |
success = 'success', |
| 15 |
danger = 'danger', |
| 16 |
warning = 'warning' |
| 17 |
} |
| 18 |
|
| 19 |
export enum MenuToggleSize { |
| 20 |
default = 'default', |
| 21 |
sm = 'sm' |
| 22 |
} |
| 23 |
|
| 24 |
export type MenuToggleElement = HTMLDivElement | HTMLButtonElement; |
| 25 |
|
| 26 |
export interface MenuToggleProps |
| 27 |
extends |
| 28 |
Omit< |
| 29 |
React.DetailedHTMLProps< |
| 30 |
React.ButtonHTMLAttributes<HTMLButtonElement> & React.HTMLAttributes<HTMLDivElement>, |
| 31 |
MenuToggleElement |
| 32 |
>, |
| 33 |
'ref' |
| 34 |
>, |
| 35 |
OUIAProps { |
| 36 |
/** Content rendered inside the toggle */ |
| 37 |
children?: React.ReactNode; |
| 38 |
/** Additional classes added to the toggle */ |
| 39 |
className?: string; |
| 40 |
/** Flag indicating the toggle has expanded styling */ |
| 41 |
isExpanded?: boolean; |
| 42 |
/** Flag indicating the toggle is disabled */ |
| 43 |
isDisabled?: boolean; |
| 44 |
/** Flag indicating the toggle is full height */ |
| 45 |
isFullHeight?: boolean; |
| 46 |
/** Flag indicating the toggle takes up the full width of its parent */ |
| 47 |
isFullWidth?: boolean; |
| 48 |
/** @beta Flag indicating the toggle is placed inside a form */ |
| 49 |
isInForm?: boolean; |
| 50 |
/** Flag indicating the toggle contains placeholder text */ |
| 51 |
isPlaceholder?: boolean; |
| 52 |
/** @beta Flag indicating the toggle has circular styling. Can only be applied to plain toggles. */ |
| 53 |
isCircle?: boolean; |
| 54 |
/** Flag indicating whether the toggle is a settings toggle. This will override the icon property */ |
| 55 |
isSettings?: boolean; |
| 56 |
/** @beta Flag indicating the menu toggle is a docked variant. For use in docked navigation. */ |
| 57 |
isDocked?: boolean; |
| 58 |
/** @beta Flag indicating the docked toggle should display text. Only applies when isDocked is true. */ |
| 59 |
isTextExpanded?: boolean; |
| 60 |
/** Elements to display before the toggle button. When included, renders the menu toggle as a split button. */ |
| 61 |
splitButtonItems?: React.ReactNode[]; |
| 62 |
/** Variant styles of the menu toggle */ |
| 63 |
variant?: 'default' | 'plain' | 'primary' | 'plainText' | 'secondary' | 'typeahead'; |
| 64 |
/** Status styles of the menu toggle */ |
| 65 |
status?: 'success' | 'warning' | 'danger'; |
| 66 |
/** Overrides the status icon */ |
| 67 |
statusIcon?: React.ReactNode; |
| 68 |
/** Optional icon or image rendered inside the toggle, before the children content. It is |
| 69 |
* recommended to wrap most basic icons in our icon component. |
| 70 |
*/ |
| 71 |
icon?: React.ReactNode; |
| 72 |
/** Optional badge rendered inside the toggle, after the children content */ |
| 73 |
badge?: BadgeProps | React.ReactNode; |
| 74 |
/** Adds styling which affects the size of the menu toggle */ |
| 75 |
size?: 'default' | 'sm'; |
| 76 |
/** @hide Forwarded ref */ |
| 77 |
innerRef?: React.Ref<MenuToggleElement>; |
| 78 |
/** Value to overwrite the randomly generated data-ouia-component-id. It will always target the toggle button. */ |
| 79 |
ouiaId?: number | string; |
| 80 |
/** Set the value of data-ouia-safe. Only set to true when the component is in a static state, i.e. no animations are occurring. At all other times, this value must be false. */ |
| 81 |
ouiaSafe?: boolean; |
| 82 |
} |
| 83 |
|
| 84 |
class MenuToggleBase extends Component<MenuToggleProps> { |
| 85 |
displayName = 'MenuToggleBase'; |
| 86 |
static defaultProps: MenuToggleProps = { |
| 87 |
className: '', |
| 88 |
isExpanded: false, |
| 89 |
isDisabled: false, |
| 90 |
isFullWidth: false, |
| 91 |
isFullHeight: false, |
| 92 |
isInForm: false, |
| 93 |
isPlaceholder: false, |
| 94 |
isCircle: false, |
| 95 |
isDocked: false, |
| 96 |
isTextExpanded: false, |
| 97 |
size: 'default', |
| 98 |
ouiaSafe: true |
| 99 |
}; |
| 100 |
|
| 101 |
render() { |
| 102 |
const { |
| 103 |
children, |
| 104 |
className, |
| 105 |
icon, |
| 106 |
badge, |
| 107 |
isExpanded, |
| 108 |
isDisabled, |
| 109 |
isFullHeight, |
| 110 |
isFullWidth, |
| 111 |
isInForm, |
| 112 |
isPlaceholder, |
| 113 |
isCircle, |
| 114 |
isSettings, |
| 115 |
isDocked, |
| 116 |
isTextExpanded, |
| 117 |
splitButtonItems, |
| 118 |
variant, |
| 119 |
status, |
| 120 |
statusIcon, |
| 121 |
innerRef, |
| 122 |
onClick, |
| 123 |
'aria-label': ariaLabel, |
| 124 |
ouiaId, |
| 125 |
ouiaSafe, |
| 126 |
size, |
| 127 |
...otherProps |
| 128 |
} = this.props; |
| 129 |
const isPlain = variant === 'plain'; |
| 130 |
const isPlainText = variant === 'plainText'; |
| 131 |
const isTypeahead = variant === 'typeahead'; |
| 132 |
|
| 133 |
return ( |
| 134 |
<SSRSafeIds prefix="pf-" ouiaComponentType={`MenuToggle${variant ? `-${variant}` : ''}`}> |
| 135 |
{(_, generatedOuiaId) => { |
| 136 |
const ouiaProps = getOUIAProps(MenuToggle.displayName, ouiaId ?? generatedOuiaId, ouiaSafe); |
| 137 |
|
| 138 |
let _statusIcon = statusIcon; |
| 139 |
if (!statusIcon) { |
| 140 |
switch (status) { |
| 141 |
case MenuToggleStatus.success: |
| 142 |
_statusIcon = <RhUiCheckCircleFillIcon />; |
| 143 |
break; |
| 144 |
case MenuToggleStatus.warning: |
| 145 |
_statusIcon = <RhUiWarningFillIcon />; |
| 146 |
break; |
| 147 |
case MenuToggleStatus.danger: |
| 148 |
_statusIcon = <RhUiErrorFillIcon />; |
| 149 |
break; |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
const toggleControls = ( |
| 154 |
<span className={css(styles.menuToggleControls)}> |
| 155 |
{status !== undefined && <span className={css(styles.menuToggleStatusIcon)}>{_statusIcon}</span>} |
| 156 |
<span className={css(styles.menuToggleToggleIcon)}> |
| 157 |
<RhMicronsCaretDownFillIcon /> |
| 158 |
</span> |
| 159 |
</span> |
| 160 |
); |
| 161 |
|
| 162 |
const content = ( |
| 163 |
<> |
| 164 |
{(icon || isSettings) && ( |
| 165 |
<span className={css(styles.menuToggleIcon)}>{isSettings ? <RhUiSettingsFillIcon /> : icon}</span> |
| 166 |
)} |
| 167 |
{isTypeahead ? children : children && <span className={css(styles.menuToggleText)}>{children}</span>} |
| 168 |
{isValidElement(badge) && <span className={css(styles.menuToggleCount)}>{badge}</span>} |
| 169 |
{isTypeahead ? ( |
| 170 |
<button |
| 171 |
type="button" |
| 172 |
className={css(styles.menuToggleButton)} |
| 173 |
aria-expanded={isExpanded} |
| 174 |
onClick={onClick} |
| 175 |
aria-label={ariaLabel || 'Menu toggle'} |
| 176 |
tabIndex={-1} |
| 177 |
{...ouiaProps} |
| 178 |
> |
| 179 |
{toggleControls} |
| 180 |
</button> |
| 181 |
) : ( |
| 182 |
!isPlain && toggleControls |
| 183 |
)} |
| 184 |
</> |
| 185 |
); |
| 186 |
|
| 187 |
const commonStyles = css( |
| 188 |
styles.menuToggle, |
| 189 |
isExpanded && styles.modifiers.expanded, |
| 190 |
variant === 'primary' && styles.modifiers.primary, |
| 191 |
variant === 'secondary' && styles.modifiers.secondary, |
| 192 |
status && styles.modifiers[status], |
| 193 |
(isPlain || isPlainText) && styles.modifiers.plain, |
| 194 |
isPlainText && styles.modifiers.text, |
| 195 |
isFullHeight && styles.modifiers.fullHeight, |
| 196 |
isFullWidth && styles.modifiers.fullWidth, |
| 197 |
isInForm && styles.modifiers.form, |
| 198 |
isDisabled && styles.modifiers.disabled, |
| 199 |
isPlaceholder && styles.modifiers.placeholder, |
| 200 |
isSettings && styles.modifiers.settings, |
| 201 |
isDocked && styles.modifiers.docked, |
| 202 |
isDocked && isTextExpanded && styles.modifiers.textExpanded, |
| 203 |
size === MenuToggleSize.sm && styles.modifiers.small, |
| 204 |
className |
| 205 |
); |
| 206 |
|
| 207 |
const componentProps = { |
| 208 |
children: content, |
| 209 |
...(isDisabled && { disabled: true }), |
| 210 |
...otherProps |
| 211 |
}; |
| 212 |
|
| 213 |
if (isTypeahead) { |
| 214 |
return ( |
| 215 |
<div |
| 216 |
ref={innerRef as React.Ref<HTMLDivElement>} |
| 217 |
className={css(commonStyles, styles.modifiers.typeahead)} |
| 218 |
{...componentProps} |
| 219 |
/> |
| 220 |
) as React.ReactElement; |
| 221 |
} |
| 222 |
|
| 223 |
if (splitButtonItems) { |
| 224 |
return ( |
| 225 |
<div |
| 226 |
ref={innerRef as React.Ref<HTMLDivElement>} |
| 227 |
className={css(commonStyles, styles.modifiers.splitButton)} |
| 228 |
> |
| 229 |
{splitButtonItems} |
| 230 |
<button |
| 231 |
className={css(styles.menuToggleButton, children && styles.modifiers.text)} |
| 232 |
type="button" |
| 233 |
aria-expanded={isExpanded} |
| 234 |
aria-label={ariaLabel} |
| 235 |
disabled={isDisabled} |
| 236 |
onClick={onClick} |
| 237 |
{...otherProps} |
| 238 |
{...ouiaProps} |
| 239 |
> |
| 240 |
{children && <span className={css(styles.menuToggleText)}>{children}</span>} |
| 241 |
{toggleControls} |
| 242 |
</button> |
| 243 |
</div> |
| 244 |
) as React.ReactElement; |
| 245 |
} |
| 246 |
|
| 247 |
return ( |
| 248 |
<button |
| 249 |
className={css(commonStyles, isCircle && isPlain && styles.modifiers.circle)} |
| 250 |
type="button" |
| 251 |
aria-label={ariaLabel} |
| 252 |
aria-expanded={isExpanded} |
| 253 |
ref={innerRef as React.Ref<HTMLButtonElement>} |
| 254 |
disabled={isDisabled} |
| 255 |
onClick={onClick} |
| 256 |
{...componentProps} |
| 257 |
{...ouiaProps} |
| 258 |
/> |
| 259 |
) as React.ReactElement; |
| 260 |
}} |
| 261 |
</SSRSafeIds> |
| 262 |
); |
| 263 |
} |
| 264 |
} |
| 265 |
|
| 266 |
export const MenuToggle = forwardRef((props: MenuToggleProps, ref: React.Ref<MenuToggleElement>) => ( |
| 267 |
<MenuToggleBase innerRef={ref} {...props} /> |
| 268 |
)); |
| 269 |
|
| 270 |
MenuToggle.displayName = 'MenuToggle'; |