import cx from 'classnames'; import { Component } from 'react'; export interface IMenuListItem { value?: any; content?: React.ReactNode; isGroup?: boolean; isDivider?: boolean; onClick?: React.MouseEventHandler; icon?: string; disabled?: string; active?: boolean | ((val: any) => boolean); hoverable?: boolean; className?: string; } export interface IMenuListItemProps { item: IMenuListItem; index: number; hover: boolean; focusTo: (focusIdx: number | null, autoScroll?: boolean) => void; onRequestClose: () => void; } export class MenuListItem extends Component { onClick = (e: React.MouseEvent) => { const { item, onRequestClose } = this.props; return handleItemClick({ item, onRequestClose, event: e }); }; onMouseEnter = () => { const { focusTo, index } = this.props; focusTo(index, false); }; onMouseLeave = () => { this.props.focusTo(null); }; render() { const { item, hover } = this.props; if (!item) { return null; } const { className, isDivider, isGroup, content } = item; if (isDivider) { return
; } if (isGroup) { return (
  • {content}
  • ); } const title = typeof content === 'number' || typeof content === 'string' ? `${content}` : undefined; const active = typeof item.active === 'function' ? item.active(item.value) : !!item.active; const hoverable = item.hoverable === undefined ? true : !!item.hoverable; return (
  • {item.icon ? : null} {content}
  • ); } } export function handleItemClick({ event, item, onRequestClose, }: { event: React.MouseEvent; item: IMenuListItem; onRequestClose: () => void; }) { const { disabled, onClick } = item; if (disabled) { return; } if (onClick) { // Item has a touch tap handler, Close it when it's done onClick(event); if (!event.defaultPrevented) { event.preventDefault(); event.stopPropagation(); onRequestClose && onRequestClose(); } } }