import React, { BaseSyntheticEvent } from "react"; import { IntentTypes } from "../../common/Intent"; import { CLASSPREFIX as eccgui } from "../../configuration/constants"; import IconButton from "../Icon/IconButton"; import { TestableComponent } from "../interfaces"; import { Card, CardActions, CardActionsProps, CardContent, CardHeader, CardOptions, CardTitle } from "./../Card"; import Divider from "./../Separation/Divider"; import Modal, { ModalProps } from "./Modal"; export interface SimpleDialogProps extends ModalProps, TestableComponent { /** * The title of the dialog. */ title?: string; /** * Parts of the dialog are separated by a horizontal ruler. */ hasBorder?: boolean; /** * Include elements to the action footer, e.g. buttons. */ actions?: React.ReactNode | React.ReactNode[]; /** * If populated with elements, then a second content area is included before the action footer. * Mainly provided to include `Notification` elements. */ notifications?: React.ReactNode | React.ReactNode[]; /** * Can contain elements actionable/non-actionable elements display right-aligned to the dialog title. */ headerOptions?: null | React.JSX.Element | React.JSX.Element[]; /** * If enabled neither closing via `esc` key or clicking outside the component will work, except explicitly specified. */ preventSimpleClosing?: boolean; /** * Define purpose of the dialog, e.g. if it is a warning. */ intent?: IntentTypes; /** Optional props for the wrapper div element inside the modal. */ wrapperDivProps?: React.DetailedHTMLProps, HTMLDivElement>; /** If a full screen toggler is shown that will allow to switch to full screen mode. */ showFullScreenToggler?: boolean; /** Starts the modal in full screen mode. The show full screen toggler will be automatically enabled. */ startInFullScreenMode?: boolean; /** Forward properties to the actions footer component. */ actionsProps?: Omit; } /** * Simplifies the dialog display by providing a direct `Card` template for the `Modal` element. * If not given then aria attributes like `role` and `aria-labelledby` are set automatically. * `aria-describedby` is only automatically when an `intent` state is set. * Inherits all properties from `Modal`. */ export const SimpleDialog = ({ children, canOutsideClickClose = false, canEscapeKeyClose = false, title = "", actions = null, notifications = null, hasBorder = false, preventSimpleClosing = false, enforceFocus = false, intent, headerOptions, showFullScreenToggler = false, startInFullScreenMode = false, size, role, "aria-labelledby": ariaLabelledby, "aria-describedby": ariaDescribedby, "aria-label": ariaLabel, actionsProps, ...otherProps }: SimpleDialogProps) => { const dialogUniqueId = React.useId().replace(/[^a-zA-Z0-9_-]/g, ""); const [displayFullscreen, setDisplayFullscreen] = React.useState(startInFullScreenMode); const showToggler = startInFullScreenMode || showFullScreenToggler; const intentClassName = intent ? `${eccgui}-intent--${intent}` : ""; const wrapperDivProps = { ...modalPreventEvents, ...otherProps.wrapperDivProps, }; const hasSemanticIntent = intent && ["success", "warning", "danger", "info"].includes(intent); const labelFallback = hasSemanticIntent && !title && !ariaLabel && !ariaLabelledby ? intent : undefined; return ( {title || headerOptions || showToggler ? ( {title} {headerOptions || showToggler ? ( {headerOptions} {showToggler && ( setDisplayFullscreen(!displayFullscreen)} /> )} ) : ( <> )} ) : null} {hasBorder && } {children} {hasBorder && } {!!notifications && ( {notifications} )} {actions && ( {actions} )} ); }; /** Events that should be prevented to bubble up from a modal that goes beyond the most simple version of a modal, e.g. * allows to drag or supports hot keys etc. */ export const modalPreventEvents = { // Prevent certain events from leaving the modal, so that e.g. react-flow does not receive these events doing unexpected stuff onContextMenu: (event: BaseSyntheticEvent) => event.stopPropagation(), onDrag: (event: BaseSyntheticEvent) => event.stopPropagation(), onDragStart: (event: BaseSyntheticEvent) => event.stopPropagation(), onDragEnd: (event: BaseSyntheticEvent) => event.stopPropagation(), // The following prevents some drop-downs to not close anymore when clicking outside of them // onMouseDown: (event: BaseSyntheticEvent) => event.stopPropagation(), onMouseUp: (event: BaseSyntheticEvent) => event.stopPropagation(), onClick: (event: BaseSyntheticEvent) => event.stopPropagation(), }; export default SimpleDialog;