From f5ad645c0951c2a1f6865ba07854f304ebe57fbc Mon Sep 17 00:00:00 2001 From: Titani Date: Fri, 14 Jun 2024 15:21:53 -0400 Subject: [PATCH] feat(MenuToggle): add OUIA support --- .../src/components/MenuToggle/MenuToggle.tsx | 36 +++++++++++++++---- 1 file changed, 29 insertions(+), 7 deletions(-) diff --git a/packages/react-core/src/components/MenuToggle/MenuToggle.tsx b/packages/react-core/src/components/MenuToggle/MenuToggle.tsx index 90b1d2fdc18..4853fa7400c 100644 --- a/packages/react-core/src/components/MenuToggle/MenuToggle.tsx +++ b/packages/react-core/src/components/MenuToggle/MenuToggle.tsx @@ -6,6 +6,7 @@ import { BadgeProps } from '../Badge'; import CheckCircleIcon from '@patternfly/react-icons/dist/esm/icons/check-circle-icon'; import ExclamationCircleIcon from '@patternfly/react-icons/dist/esm/icons/exclamation-circle-icon'; import ExclamationTriangleIcon from '@patternfly/react-icons/dist/esm/icons/exclamation-triangle-icon'; +import { OUIAProps, getDefaultOUIAId, getOUIAProps } from '../../helpers'; export enum MenuToggleStatus { success = 'success', @@ -24,12 +25,13 @@ export interface SplitButtonOptions { export interface MenuToggleProps extends Omit< - React.DetailedHTMLProps< - React.ButtonHTMLAttributes & React.HTMLAttributes, - MenuToggleElement + React.DetailedHTMLProps< + React.ButtonHTMLAttributes & React.HTMLAttributes, + MenuToggleElement + >, + 'ref' >, - 'ref' - > { + OUIAProps { /** Content rendered inside the toggle */ children?: React.ReactNode; /** Additional classes added to the toggle */ @@ -58,16 +60,29 @@ export interface MenuToggleProps badge?: BadgeProps | React.ReactNode; /** @hide Forwarded ref */ innerRef?: React.Ref; + /** Value to overwrite the randomly generated data-ouia-component-id. It will always target the toggle button. */ + ouiaId?: number | string; + /** 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. */ + ouiaSafe?: boolean; } -class MenuToggleBase extends React.Component { +interface MenuToggleState { + ouiaStateId: string; +} + +class MenuToggleBase extends React.Component { displayName = 'MenuToggleBase'; static defaultProps: MenuToggleProps = { className: '', isExpanded: false, isDisabled: false, isFullWidth: false, - isFullHeight: false + isFullHeight: false, + ouiaSafe: true + }; + + state: MenuToggleState = { + ouiaStateId: getDefaultOUIAId(MenuToggle.displayName, this.props.variant) }; render() { @@ -87,12 +102,16 @@ class MenuToggleBase extends React.Component { innerRef, onClick, 'aria-label': ariaLabel, + ouiaId, + ouiaSafe, ...otherProps } = this.props; const isPlain = variant === 'plain'; const isPlainText = variant === 'plainText'; const isTypeahead = variant === 'typeahead'; + const ouiaProps = getOUIAProps(MenuToggle.displayName, ouiaId ?? this.state.ouiaStateId, ouiaSafe); + let _statusIcon = statusIcon; if (!statusIcon) { switch (status) { @@ -130,6 +149,7 @@ class MenuToggleBase extends React.Component { onClick={onClick} aria-label={ariaLabel || 'Menu toggle'} tabIndex={-1} + {...ouiaProps} > {toggleControls} @@ -189,6 +209,7 @@ class MenuToggleBase extends React.Component { onClick={onClick} {...(children && { style: { display: 'flex', paddingLeft: 'var(--pf-v5-global--spacer--sm)' } })} {...otherProps} + {...ouiaProps} > {children && {children}} {toggleControls} @@ -207,6 +228,7 @@ class MenuToggleBase extends React.Component { disabled={isDisabled} onClick={onClick} {...componentProps} + {...ouiaProps} /> ); }