import {AnchorHTMLAttributes, LiHTMLAttributes, MouseEvent, ReactNode, isValidElement, useRef, useContext} from 'react' import {cx, isNil, isUndefined, safeInvoke, omit, createRandomId, pick} from '@befe/brick-utils' import {Icon} from '@befe/brick-comp-icon' import {Checkbox, GenericCheckboxProps} from '@befe/brick-comp-checkbox' import {PopoverConfirm, PopoverConfirmProps} from '@befe/brick-comp-popover' import {SvgFC} from '@befe/brick-core' import {MenuContext} from './menu-context' import {MenuItemContent, MenuItemContentProps} from './menu-item-content' import {MenuItemId} from './common' import {MENU_ITEM_SELECT_ALL_ID} from './menu-item-select-all' type PropsFromHTMLLIElement = Omit, 'id' | 'prefix' | 'style'> type PropsFromCheckbox = Pick type PropsFromMenuItemContent = Pick export interface MenuItemProps extends PropsFromHTMLLIElement, PropsFromCheckbox, PropsFromMenuItemContent { /** * 自定义 class */ className?: string /** * 唯一标识 */ id?: MenuItemId /** * 是否禁用 */ disabled?: boolean /** * 点击回调 */ onClick?: (e: MouseEvent) => void /** * 类型(暂定) */ type?: 'normal' | 'checkbox' /** * 是否选中控制值 * 如不控制,则以是否在 context.selectedIds 进行判断是否选中 */ selected?: boolean /** * 前缀 icon * 只在第一层 Menu 下有效 */ icon?: SvgFC /** * 链接跳转的地址 * 若给如非 undefined 的值,将使用 作为 Button 的元素 */ href?: string /** * 链接跳转的 target */ target?: string /** * @private 暂不公布,目前仅为 table 操作列需要支持 */ popoverConfirmProps?: PopoverConfirmProps } export function MenuItem(props: MenuItemProps) { const {disabled = false, type: typeProp = 'normal'} = props const p: MenuItemProps = {...props, disabled, type: typeProp} const context = useContext(MenuContext) const _id = useRef(createRandomId('menu-item_')).current const id = isNil(props.id) ? _id : props.id const type = context.multipleItemType const multiple = context.multiple const isSelected = !isUndefined(props.selected) ? props.selected : (context.selectedIds || []).includes(id) const checkboxSize = context.size const hideMenuPopper = () => { if (multiple) { return } const refMenuPopper = context.refMenuPopper if (refMenuPopper && refMenuPopper.current) { refMenuPopper.current.hidePopperOnSelect() } } const handlePopoverConfirmConfirm: PopoverConfirmProps['onConfirm'] = (e) => { hideMenuPopper() safeInvoke(props.popoverConfirmProps?.onConfirm, e) } const handlePopoverConfirmCancel: PopoverConfirmProps['onCancel'] = (e) => { hideMenuPopper() safeInvoke(props.popoverConfirmProps?.onCancel, e) } const handleClick = (e: MouseEvent) => { if (disabled) { return } safeInvoke(props.onClick, e) if (!props.popoverConfirmProps) { hideMenuPopper() } id !== MENU_ITEM_SELECT_ALL_ID && safeInvoke(context.toggleSelected, id, e) } const popoverConfirmProps: PopoverConfirmProps | undefined = props.popoverConfirmProps && { ...omit(props.popoverConfirmProps, ['onConfirm', 'onCancel']), onConfirm: handlePopoverConfirmConfirm, onCancel: handlePopoverConfirmCancel, } let prefixNode: ReactNode = null if (isValidElement(props.prefix)) { prefixNode = props.prefix } else if (props.icon) { prefixNode = } else if (multiple && type === 'checkbox') { prefixNode = ( ) } let contentNode: ReactNode = props.children if (props.href) { const anchorProps: Partial> = { href: disabled ? undefined : props.href, target: props.target, } if (anchorProps.target === '_blank' && anchorProps.href?.includes('//')) { anchorProps.rel = 'noreferrer' } contentNode = {props.children} } const itemContent = ( {contentNode} ) const itemContentNode = props.popoverConfirmProps && !disabled ? ( {itemContent} ) : itemContent const className = cx( 'brick-menu-item', { 'brick-menu-item-selected': isSelected, [`brick-menu-item-type-${type!}`]: type, 'brick-menu-item-disabled': disabled, }, props.className ) const liProps = { ...(omit(p, [ // @ts-ignore 强制去掉 style 'style', 'className', 'onClick', 'id', 'indeterminate', 'icon', 'prefix', 'suffix', 'suffixArrow', 'suffixLoading', 'title', 'href', 'target', 'popoverConfirmProps', ]) as LiHTMLAttributes), className, onClick: handleClick, } return (
  • {itemContentNode}
  • ) } MenuItem.displayName = 'MenuItem'