import { PopoverMenu, Button, ButtonProps } from '@wix/design-system'; import React, { ReactNode, useMemo, useCallback, MutableRefObject, RefObject, } from 'react'; import { ChevronDown } from '@wix/wix-ui-icons-common/lazy'; import { ActionSubItemPrivate, ActionSubitemLabelPrivate, } from './ActionSubItemProps'; import { DisabledTooltip } from '../DisabledTooltip'; function isActionSubitemLabel( item: NonNullable[number][number], ): item is ActionSubitemLabelPrivate { return typeof item === 'object' && item !== null && 'sectionTitle' in item; } export function ActionPopover(props: { actionItem: ActionSubItemPrivate; size?: ButtonProps['size']; priority?: ButtonProps['priority']; fullWidth?: ButtonProps['fullWidth']; onPopoverClick?: () => void; }) { const { actionItem, size, priority, onPopoverClick, fullWidth } = props; const { dataHook, subItems, popoverMenuProps } = actionItem; const createCombinedRef = useCallback( (wdsRef?: RefObject) => { return (wrapper: HTMLDivElement | null) => { const buttonEl = wrapper?.querySelector('button') ?? null; if (wdsRef) { (wdsRef as MutableRefObject).current = buttonEl; } const externalRef = actionItem.ref; if (externalRef) { if (typeof externalRef === 'function') { externalRef(buttonEl); } else { (externalRef as MutableRefObject).current = buttonEl; } } }; }, [actionItem.ref], ); const allActions = useMemo(() => { return subItems ?.filter((subItemGroup) => subItemGroup?.length) .reduce((result, group, index, array) => { const nodes: ReactNode[] = []; group.forEach((item, itemIndex) => { if (isActionSubitemLabel(item)) { nodes.push( , ); } else { nodes.push( , ); } }); if (index !== array.length - 1) { nodes.push(); } return [...result, ...nodes]; }, []); }, [subItems]); return ( ( // Wrapper div needed due to type mismatch: PopoverMenu expects RefObject // but Button's ref returns a class instance, not an HTMLElement.
)} > {allActions}
); }