import { ComponentChild, ComponentChildren, FunctionComponent, VNode, } from 'preact'; import { forwardRef, Ref } from 'preact/compat'; import { JSXInternal } from 'preact/src/jsx'; import { Icon, IconType } from '../Icon'; import { Switch, SwitchProps } from '../Switch'; export enum MenuItemType { IconButton, RadioButton, SwitchButton, } type MenuItemProps = { type: MenuItemType; children: ComponentChildren; onClick?: JSXInternal.MouseEventHandler; onChange?: SwitchProps['onChange']; className?: string; checked?: boolean; icon?: IconType; iconClassName?: string; tabIndex?: number; }; export const MenuItem: FunctionComponent = forwardRef( ( { children, onClick, onChange, className = '', type, checked, icon, iconClassName, tabIndex, }: MenuItemProps, ref: Ref ) => { return type === MenuItemType.SwitchButton && typeof onChange === 'function' ? ( {children} ) : ( ); } ); export const MenuItemSeparator: FunctionComponent = () => (
); type ListElementProps = { isFirstMenuItem: boolean; children: ComponentChildren; }; export const MenuItemListElement: FunctionComponent = forwardRef(({ children, isFirstMenuItem }: ListElementProps, ref: Ref) => { const child = children as VNode; return (
  • {{ ...child, props: { ...(child.props ? { ...child.props } : {}), ...(child.type === MenuItem ? { tabIndex: isFirstMenuItem ? 0 : -1, } : {}), }, }}
  • ); });