import React, { ReactElement, createContext, useContext, useState, } from "react"; import Icon, { IconProp } from "../icon"; import { ControlStatusProps, Placement } from "../../common/controls.type"; import classNames from "classnames"; import Dropdown from "../dropdown"; interface ActionDropdownItemProps extends ControlStatusProps { icon?: IconProp; suffixIcon?: IconProp; prefix?: string | ReactElement | ReactElement[]; suffix?: string | ReactElement | ReactElement[]; children: string | ReactElement | ReactElement[]; onClick?: React.MouseEventHandler; primary?: boolean; danger?: boolean; disabled?: boolean; closeAfterClick?: boolean; } export const ActionDropdownItem = ({ children, icon, suffixIcon, prefix, suffix, success, error, warning, danger, primary, disabled, onClick, closeAfterClick = true, }: ActionDropdownItemProps) => { const isDefault = !(success || error || warning || danger || primary); const { onClose } = useActionDropdownContext(); const handleClick: React.MouseEventHandler = (e) => { if (onClick) { onClick(e); } if (closeAfterClick) { onClose(); } }; return (
{icon && } {prefix && {prefix}} {children}
{suffix && {suffix}} {suffixIcon && }
); }; interface ActionDropdownContextProps { onClose: () => void; } const Context = createContext({ onClose: () => {}, }); const useActionDropdownContext = () => useContext(Context); interface ActionDropdownProps { button: string | ReactElement | ReactElement[]; children: string | ReactElement | ReactElement[]; show: boolean; placement?: Placement; onClose: () => void; } export const ActionDropdown = ({ children, button, show, placement, onClose, }: ActionDropdownProps) => { const contextValue: ActionDropdownContextProps = { onClose, }; return (
{children}
); };