import clsx from 'clsx'; import React, { useEffect, useRef } from 'react'; import { useHistory } from 'react-router-dom'; import { Counter, Icon } from '../ui'; export interface MenuItem { action?: React.EventHandler active?: boolean count?: number destructive?: boolean href?: string icon?: string meta?: string middleClick?(event: React.MouseEvent): void target?: React.HTMLAttributeAnchorTarget text: string to?: string } interface IDropdownMenuItem { index: number item: MenuItem | null onClick?(): void } const DropdownMenuItem = ({ index, item, onClick }: IDropdownMenuItem) => { const history = useHistory(); const itemRef = useRef(null); const handleClick: React.EventHandler = (event) => { event.stopPropagation(); if (!item) return; if (onClick) onClick(); if (item.to) { event.preventDefault(); history.push(item.to); } else if (typeof item.action === 'function') { event.preventDefault(); item.action(event); } }; const handleAuxClick: React.EventHandler = (event) => { if (!item) return; if (onClick) onClick(); if (event.button === 1 && item.middleClick) { item.middleClick(event); } }; const handleItemKeyPress: React.EventHandler = (event) => { if (event.key === 'Enter' || event.key === ' ') { handleClick(event); } }; useEffect(() => { const firstItem = index === 0; if (itemRef.current && firstItem) { itemRef.current.focus({ preventScroll: true }); } }, [itemRef.current, index]); if (item === null) { return
  • ; } return (
  • {item.icon && } {item.text} {item.count ? ( ) : null}
  • ); }; export default DropdownMenuItem;