import { LiHTMLAttributes, ReactNode, MouseEvent, CSSProperties, PropsWithChildren, ForwardedRef, forwardRef, isValidElement, useContext, } from 'react' import {cx, getTextFromReactNode, omit, safeInvoke, isNil, isUndefined} from '@befe/brick-utils' import {Icon} from '@befe/brick-comp-icon' import {SvgFC} from '@befe/brick-core' import {SvgLoading, SvgPlainRight} from '@befe/brick-icon' import {MenuContext} from './menu-context' import {MenuItemId, getMenuLevelIndent} from './common' type DivAttributes = Omit, 'id' | 'prefix'> export interface MenuItemContentProps extends DivAttributes { /** * 自定义 class */ className?: string /** * 唯一标识 */ id?: MenuItemId /** * 是否禁用 */ disabled?: boolean /** * 前缀内容 */ prefix?: ReactNode /** * 后缀内容 */ suffix?: ReactNode /** * 后缀箭头 */ suffixArrow?: boolean | SvgFC /** * 后缀 loading,用于懒加载时体现 loading 状态 */ suffixLoading?: boolean | SvgFC /** * title tip */ title?: string } function MenuItemContentRender( props: PropsWithChildren, ref: ForwardedRef ) { const context = useContext(MenuContext) const inCursor = context.cursorId !== null && props.id === context.cursorId const withSuffix = props.suffixArrow || props.suffixLoading || props.suffix const handleMouseEnter = (e: MouseEvent) => { const {disabled, id} = props if (disabled || isNil(id)) { return } safeInvoke(context.moveCursor, id, e) } const handleMouseLeave = (e: MouseEvent) => { safeInvoke(context.moveCursor, null, e) } const prefixNode = isValidElement(props.prefix) && ( {props.prefix} ) let suffixNode: ReactNode if (isValidElement(props.suffix)) { suffixNode = props.suffix } else if (props.suffixLoading) { const arrowSvg = props.suffixLoading === true ? SvgLoading : props.suffixLoading suffixNode = } if (!props.suffixLoading && props.suffixArrow) { const arrowSvg = props.suffixArrow === true ? SvgPlainRight : props.suffixArrow suffixNode = } const suffixWrap = suffixNode && ( {suffixNode} ) const className = cx( 'brick-menu-item-content', { 'brick-menu-item-content-in-cursor': inCursor, 'brick-menu-item-content-with-suffix': withSuffix, }, props.className ) const divProps = { ...omit(props, [ 'className', 'id', 'disabled', 'suffix', 'prefix', 'suffixArrow', 'suffixLoading', 'style', ]), onMouseEnter: handleMouseEnter, onMouseLeave: handleMouseLeave, } if (isUndefined(divProps.title)) { divProps.title = getTextFromReactNode(divProps.children) } // 运行时按实际层级现算 padding-left(替代原顶层 nav 穷举层级的纯 CSS 实现) // context.level 为 'none'(非 vertical+nav 根 / popper 子树)或 < 2 时不缩进 const indentPaddingLeft = getMenuLevelIndent({ level: context.level, size: context.size, withPrefixIcon: context.withPrefixIcon, }) const mergedStyle: CSSProperties | undefined = isUndefined(indentPaddingLeft) ? props.style : {paddingLeft: indentPaddingLeft, ...props.style} return ( {prefixNode} {props.children} {suffixWrap} ) } export const MenuItemContent = forwardRef(MenuItemContentRender) MenuItemContent.displayName = 'MenuItemContent'