import type { Ref } from 'react'; import React from 'react'; import type { PressEvent } from 'react-aria'; import { useObjectRef } from 'react-aria'; import { MenuItem } from 'react-aria-components'; import classNames from 'classnames'; import type { AnchorProps } from '../../Anchor/Anchor'; import type { ButtonProps } from '../../Button/Button'; import type { IconProps } from '../../Icon/Icon'; import { Icon } from '../../Icon/Icon'; import { Text } from '../../Typography/Text/Text'; import { useDropdownContext } from '../DropdownButtonContext'; export interface DropdownButtonListItemProps { label: string; description?: string; icon?: IconProps['icon']; href?: string; target?: React.AnchorHTMLAttributes['target']; onAction?: ButtonProps['onClick'] | AnchorProps['onClick']; onPress?: (e: PressEvent) => void; variant?: 'default' | 'action'; ref?: Ref; } const noop = () => undefined; const alwaysTrue = () => true; export const DropdownButtonListItem: React.FC = ({ label, onAction, onPress, description, icon, href, target, variant = 'default', ref, ...rest }) => { const { mobileView } = useDropdownContext(); const itemRef = useObjectRef(ref); return ( { // Create a synthetic event-like object to mimic a real event (for consistency with onClick handlers) if (itemRef.current) { // TODO: Expand the synthetic event with more props if needed and remove this workaround if // react-aria-components starts to support onAction with a real event const syntheticEvent: React.MouseEvent = { currentTarget: itemRef.current, target: itemRef.current, bubbles: false, cancelable: false, defaultPrevented: true, isTrusted: false, preventDefault: noop, isDefaultPrevented: alwaysTrue, } as unknown as React.MouseEvent; onAction?.(syntheticEvent); } }} className={classNames('dropdownButton__listItem', { 'dropdownButton__listItem--mobileView': mobileView, 'dropdownButton__listItem--action': variant === 'action', })} href={href} target={target} > {meta => ( <> {icon && } {label} {description && ({description})} {meta.hasSubmenu && ( )} )} ); };