import React, { ReactElement } from "react"; import Button, { ButtonProps } from "../button"; import { ControlStatusProps } from "../../common/controls.type"; import Window from "../window"; import classnames from "classnames"; import Icon from "../icon"; import ExclamationIcon from "../../icons/exclamation-icon"; import CheckIcon from "../../icons/check-icon"; import CloseIcon from "../../icons/close-icon"; import { OverlayChildrenProps } from "../overlay"; import InfoIcon from "../../icons/info-icon"; export type OnPopupClose = (result?: T) => void; export type PopupButtons = | ButtonProps[] | ((close: OnPopupClose) => ButtonProps[]); export type PopupButtonsDirection = "vertical" | "horizontal"; export interface PopupProps extends ControlStatusProps { title?: string | React.ReactElement; titleAlign?: "start" | "center" | "end"; children: | string | React.ReactElement | ((props: OverlayChildrenProps) => React.ReactElement); buttons?: PopupButtons; buttonsDirection?: PopupButtonsDirection; show?: boolean; onClose?: OnPopupClose; danger?: boolean; info?: boolean; } const Popup = ({ show = true, onClose = (result) => {}, title, titleAlign = "center", children, buttons = [], buttonsDirection = "horizontal", success, error, warning, danger, info, }: PopupProps) => { const status = error ? "error" : success ? "success" : warning ? "warning" : danger ? "danger" : info ? "info" : "default"; let buttonsArray = Array.isArray(buttons) ? buttons : buttons(onClose); if (buttonsArray.length > 2 || buttonsArray.length == 1) { buttonsDirection = "vertical"; } if (buttonsDirection == "horizontal") { buttonsArray = buttonsArray.reverse(); } let icon: ReactElement | undefined; const el = (props: OverlayChildrenProps) => { if (typeof children === "function") { return children(props); } if (typeof children == "string") { return (
{children}
); } return children as React.ReactElement; }; switch (status) { case "danger": case "warning": icon = ; break; case "error": icon = ; break; case "success": icon = ; break; case "info": icon = ; break; } return ( {(overlayProps) => { return (
{icon && (
)}
{title && (
{title}
)} {el({ ...overlayProps, close: onClose, })}
{buttonsArray.length > 0 && (
{buttonsArray.map((button, index) => (
)}
); }}
); }; export default Popup;