import { CSSProperties, ReactNode, Children, PropsWithChildren, isValidElement, MouseEvent, LiHTMLAttributes, useRef, useContext, } from 'react' import { cx, codeWarningOnce, safeInvoke, createRandomId, ifNodeContains, isNil, isUndefined, } from '@befe/brick-utils' import {Icon, IconProps} from '@befe/brick-comp-icon' import {SvgPlainDown, SvgPlainRight, SvgPlainUp} from '@befe/brick-icon' import {MenuItem, MenuItemProps} from './menu-item' import {MenuItemContent} from './menu-item-content' import {MenuPopper, MenuPopperProps, MenuPopperHandle} from './menu-popper' import {Menu} from './menu' import {MenuContext} from './menu-context' import { MenuItemId, SUBMENU_POPPER_TRIGGER_TYPE_DEFAULT, SUBMENU_SELECTABLE_DEFAULT, SUBMENU_TYPE_DEFAULT, SubmenuType, } from './common' export interface MenuSubmenuProps extends Pick { /** * 自定义 class */ className?: string /** * 自定义 style */ style?: CSSProperties /** * 子菜单类型 */ type?: SubmenuType /** * 唯一标识 */ id?: MenuItemId /** * 是否选中控制值 * * 如不控制,则以是否在 context.selectedIds 进行判断是否选中 */ selected?: boolean /** * 是否展开控制值 * * 如不控制,则以是否在 context.expandedIds 进行判断是否展开 */ expanded?: boolean /** * 菜单项元素 */ itemContent?: ReactNode /** * popper menu 触发类型 */ popperTriggerType?: 'hover' | 'click' /** * popper menu 位置 */ popperPlacement?: MenuPopperProps['placement'] /** * 点击回调 */ onClick?: (e: MouseEvent) => void /** * 是否可以选择,即点击是否有进行 MenuItem 的 select 行为 */ selectable?: boolean /** * 禁用 menu popper 的 popup portal * * 详见 [Popup](/components/popup) */ disablePortal?: MenuPopperProps['disablePortal'] /** * 指定 menu popper 的 popup portal * * 详见 [Popup](/components/popup) */ portalContainer?: MenuPopperProps['portalContainer'] } function hasDescendantSelectedIn(children: ReactNode, selectedIds: MenuItemId[]): boolean { return Children.toArray(children).some(child => { if (!isValidElement(child)) { return false } const childProps = child.props as PropsWithChildren> if (child.type === MenuItem) { const {selected} = childProps return isUndefined(selected) ? selectedIds.includes(childProps.id as MenuItemId) : selected } // eslint-disable-next-line @typescript-eslint/no-use-before-define if (child.type === Submenu) { return hasDescendantSelectedIn(childProps.children, selectedIds) } return false }) } export function Submenu(props: PropsWithChildren) { const context = useContext(MenuContext) const refMenuPopper = useRef(null) const refMenuArrow = useRef(null) const _id = useRef(createRandomId('submenu_')).current const id = isNil(props.id) ? _id : props.id const layout = context.layout const isLayoutHorizontal = layout === 'horizontal' const mustBePopper = isLayoutHorizontal const type = (isUndefined(props.type) ? context.submenuType : props.type) || SUBMENU_TYPE_DEFAULT const isTypePopper = type === 'popper' || mustBePopper const isExpanded = !isUndefined(props.expanded) ? props.expanded : (context.expandedIds || []).includes(id) const isSelected = !isUndefined(props.selected) ? props.selected : (context.selectedIds || []).includes(id) const hasDescendantSelected = context.selectedIds ? hasDescendantSelectedIn(props.children, context.selectedIds) : false const menuPopperSize = context.type === 'nav' ? 'md' : context.size const isSelectable = !isUndefined(props.selectable) ? props.selectable : (context.submenuSelectable || SUBMENU_SELECTABLE_DEFAULT) const popperTriggerType = (isUndefined(props.popperTriggerType) ? context.submenuPopperTriggerType : props.popperTriggerType) || SUBMENU_POPPER_TRIGGER_TYPE_DEFAULT const submenuSize = type === 'popper' ? menuPopperSize : context.size const submenuRefMenuPopper = isTypePopper ? refMenuPopper : context.refMenuPopper const popperMenuPlacement: MenuPopperProps['placement'] = !isUndefined(props.popperPlacement) ? props.popperPlacement : isLayoutHorizontal ? 'bottom-start' : layout === 'vertical-right' ? 'left-start' : 'right-start' const handleClickItemContent = (e: MouseEvent) => { const {onClick} = props const {folderToggleSpot} = context const isClickToggleArrow = ifNodeContains(refMenuArrow.current, e.target as HTMLElement) if (isClickToggleArrow && type === 'folder') { safeInvoke(context.toggleExpanded, id, e) return } let hasClickHandler = false if (!isUndefined(onClick)) { safeInvoke(props.onClick, e) hasClickHandler = true } if (isSelectable) { safeInvoke(context.toggleSelected, id, e) hasClickHandler = true } if (hasClickHandler) { return } // 没有点击处理, // 且不是连接, // 且 folderToggleSpot 为自动模式(即没有指定为 'toggle') // 点击自动退化为"toggle展开收起" const target = e.target const isClickAnchor = target instanceof Element && target.tagName === 'A' if (!isClickAnchor && type === 'folder' && folderToggleSpot !== 'toggle') { safeInvoke(context.toggleExpanded, id, e) } } const prefixNode = isValidElement(props.prefix) ? props.prefix : (props.icon ? : null) let arrowNode: ReactNode = null if (type !== 'group') { const iconProps: IconProps = { className: 'brick-menu-item-suffix-arrow', } if (type === 'folder') { iconProps.svg = isExpanded ? SvgPlainUp : SvgPlainDown } // type === 'popper' else { iconProps.svg = layout === 'horizontal' ? SvgPlainDown : SvgPlainRight } arrowNode = } const validType = type === 'popper' || !mustBePopper codeWarningOnce(validType, [ 'while layout is \'horizontal\',', 'level 1 Submenu type must be \'popper\'.', 'Submenu will change it automatically', ].join(' ')) const liProps: LiHTMLAttributes = { className: cx( 'brick-menu-submenu', `brick-menu-submenu-type-${type}`, { 'brick-menu-submenu-selected': isSelected, [`brick-menu-submenu-${isExpanded ? 'expanded' : 'collapsed'}`]: type === 'folder', 'brick-menu-submenu-has-descendant-selected': hasDescendantSelected, }, props.className ), style: props.style, } const submenuMenuNode = ( {props.children} ) const hideInlineSubmenu = type === 'popper' || mustBePopper || (type === 'folder' && !isExpanded) const inlineSubmenu = hideInlineSubmenu ? null : submenuMenuNode const itemContentNode = props.itemContent && ( {props.itemContent} ) const menuItem = (
  • {itemContentNode} {inlineSubmenu}
  • ) return isTypePopper ? ( {/* 浮层子树不参与 inline 层级缩进:level 置 0(粘滞下传),缩进由 _menu-popper.scss 维持现状 */} {submenuMenuNode} ) : menuItem } Submenu.displayName = 'Submenu'