diff --git a/packages/itwinui-react/src/core/Menu/MenuItem.tsx b/packages/itwinui-react/src/core/Menu/MenuItem.tsx index a6c51494f04..c62d59b499e 100644 --- a/packages/itwinui-react/src/core/Menu/MenuItem.tsx +++ b/packages/itwinui-react/src/core/Menu/MenuItem.tsx @@ -8,12 +8,13 @@ import { useMergedRefs, useId, createWarningLogger, + ButtonBase, + polymorphic, } from '../../utils/index.js'; import type { PolymorphicForwardRefComponent } from '../../utils/index.js'; import { Menu, MenuContext } from './Menu.js'; import { ListItem } from '../List/ListItem.js'; import type { ListItemOwnProps } from '../List/ListItem.js'; -import cx from 'classnames'; const logWarning = createWarningLogger(); @@ -83,7 +84,6 @@ export type MenuItemProps = { */ export const MenuItem = React.forwardRef((props, forwardedRef) => { const { - className, children, isSelected, disabled, @@ -129,15 +129,7 @@ export const MenuItem = React.forwardRef((props, forwardedRef) => { } satisfies Parameters[0]['popoverProps']; }, [subMenuItems.length]); - const onClick = () => { - if (disabled) { - return; - } - - onClickProp?.(value); - }; - - const handlers: React.DOMAttributes = { onClick }; + const onClick = () => onClickProp?.(value); /** Index of this item out of all the focusable items in the parent `Menu` */ const focusableItemIndex = parentMenu?.focusableElements.findIndex( @@ -145,10 +137,7 @@ export const MenuItem = React.forwardRef((props, forwardedRef) => { ); const trigger = ( - { {...(parentMenu?.popoverGetItemProps != null ? parentMenu.popoverGetItemProps({ focusableItemIndex, - userProps: handlers, + userProps: { onClick }, }) - : handlers)} - {...(rest as React.DOMAttributes)} + : { onClick })} + {...(rest as React.DOMAttributes)} > {startIcon && ( @@ -187,7 +176,7 @@ export const MenuItem = React.forwardRef((props, forwardedRef) => { {endIcon} )} - + ); return ( @@ -206,6 +195,11 @@ if (process.env.NODE_ENV === 'development') { MenuItem.displayName = 'MenuItem'; } +const MenuItemBase = polymorphic( + ButtonBase, + ListItem, +) as PolymorphicForwardRefComponent<'button', ListItemOwnProps>; + // ---------------------------------------------------------------------------- export type TreeEvent = { diff --git a/packages/itwinui-react/src/utils/functions/polymorphic.test.tsx b/packages/itwinui-react/src/utils/functions/polymorphic.test.tsx index 3504800cfc0..d549bb2e4fa 100644 --- a/packages/itwinui-react/src/utils/functions/polymorphic.test.tsx +++ b/packages/itwinui-react/src/utils/functions/polymorphic.test.tsx @@ -4,6 +4,32 @@ *--------------------------------------------------------------------------------------------*/ import { render } from '@testing-library/react'; import { polymorphic } from './polymorphic.js'; +import type { PolymorphicForwardRefComponent } from '../props.js'; + +it('should work when called directly', () => { + const MyDiv1 = polymorphic.div('my-div1'); + const MyDiv2 = polymorphic.div('my-div2'); + const MyDiv = polymorphic( + MyDiv1, + MyDiv2, + ) as PolymorphicForwardRefComponent<'div'>; + + const { container: container1 } = render(🍏); + const el1 = container1.querySelector('div') as HTMLElement; + expect(el1).toHaveClass('my-div1', 'my-div2'); + expect(el1).toHaveTextContent('🍏'); + expect(el1).toHaveAttribute('data-testid', 'foo'); + + const { container: container2 } = render( + + 🥭 + , + ); + const el2 = container2.querySelector('span') as HTMLElement; + expect(el2).toHaveClass('my-div1', 'my-div2'); + expect(el2).toHaveTextContent('🥭'); + expect(el2).toHaveAttribute('data-testid', 'bar'); +}); it('should work when called as property', () => { const MyButton = polymorphic.button('my-button'); diff --git a/packages/itwinui-react/src/utils/functions/polymorphic.tsx b/packages/itwinui-react/src/utils/functions/polymorphic.tsx index 5b4aa0f894f..9dd0c5f1a8a 100644 --- a/packages/itwinui-react/src/utils/functions/polymorphic.tsx +++ b/packages/itwinui-react/src/utils/functions/polymorphic.tsx @@ -8,7 +8,7 @@ import type { PolymorphicForwardRefComponent } from '../props.js'; import { useGlobals } from '../hooks/useGlobals.js'; import { styles } from '../../styles.js'; -const _base = ( +const _elementBase = ( defaultElement: As, ) => { return (className: string, attrs?: JSX.IntrinsicElements[As]) => { @@ -21,6 +21,16 @@ const _base = ( ), }; + useGlobals(); + + if (Array.isArray(as as any)) { + if (as.length > 1) { + const [Component, ...restAs] = as; + return ; + } + (as as any) = as[0]; + } + const Element = (as as any) || 'div'; // Add tabIndex to interactive elements if not already set. @@ -33,8 +43,6 @@ const _base = ( props.tabIndex ??= 0; } - useGlobals(); - return ; }) as PolymorphicForwardRefComponent>; @@ -46,6 +54,19 @@ const _base = ( }; }; +const _componentBase = ( + ...components: PolymorphicForwardRefComponent[] +) => { + const Comp = _elementBase('div')(''); + return React.forwardRef((props, ref) => ( + + )) as PolymorphicForwardRefComponent<'div'>; +}; + /** * Utility to create a type-safe polymorphic component with a simple class. * @@ -57,23 +78,31 @@ const _base = ( * - adds and merges CSS classes * - adds tabIndex to interactive elements (Safari workaround) * + * Alternatively, it can be called directly with multiple components to create a chain of + * components. This is useful for creating wrapper components that still need to support + * user-specified `as` prop. **Note:** All components in the chain must be polymorphic. + * * @example * const MyPolyButton = polymorphic.button('my-poly-button', { type: 'button' }); * ...; * + * @example + * const MyMenuButton = polymorphic(MyPolyButton, MyMenu) as PolymorphicForwardRefComponent<'button'>; + * ...; + * * @private */ -export const polymorphic = new Proxy({} as never, { +export const polymorphic = new Proxy(_componentBase, { get: (target, prop) => { if (typeof prop === 'string') { // eslint-disable-next-line -- string is as far as we can narrow it down // @ts-ignore - return _base(prop); + return _elementBase(prop); } return Reflect.get(target, prop); }, -}) as { - [key in keyof JSX.IntrinsicElements]: ReturnType>; +}) as typeof _componentBase & { + [key in keyof JSX.IntrinsicElements]: ReturnType>; }; // e.g. iui-list-item-icon -> ListItemIcon