import * as DialogPrimitive from "@radix-ui/react-dialog"; import { useRouter } from "next/router"; import React, { ReactNode, useState, MouseEvent } from "react"; import { Icon } from "react-feather"; import classNames from "@calcom/lib/classNames"; import Button from "./Button"; export type DialogProps = React.ComponentProps & { name?: string; clearQueryParamsOnClose?: string[]; }; export function Dialog(props: DialogProps) { const router = useRouter(); const { children, name, ...dialogProps } = props; // only used if name is set const [open, setOpen] = useState(!!dialogProps.open); if (name) { const clearQueryParamsOnClose = ["dialog", ...(props.clearQueryParamsOnClose || [])]; dialogProps.onOpenChange = (open) => { if (props.onOpenChange) { props.onOpenChange(open); } // toggles "dialog" query param if (open) { router.query["dialog"] = name; } else { clearQueryParamsOnClose.forEach((queryParam) => { delete router.query[queryParam]; }); } router.push( { pathname: router.pathname, query: { ...router.query, }, }, undefined, { shallow: true } ); setOpen(open); }; // handles initial state if (!open && router.query["dialog"] === name) { setOpen(true); } // allow overriding if (!("open" in dialogProps)) { dialogProps.open = open; } } return ( {children} ); } type DialogContentProps = React.ComponentProps & { size?: "xl" | "lg"; type: "creation" | "confirmation"; title?: string; description?: string | undefined; closeText?: string; actionDisabled?: boolean; actionText?: string; Icon?: Icon; // If this is set it allows you to overide the action buttons. Usefull if you need to use formcontext useOwnActionButtons?: boolean; actionOnClick?: () => void; actionOnClose?: () => void; }; export const DialogContent = React.forwardRef( ({ children, Icon, ...props }, forwardedRef) => ( {/*zIndex one less than Toast */} {props.type === "creation" && (
{props.title && } {props.description &&

{props.description}

}
{children}
)} {props.type === "confirmation" && (
{Icon && (
)}
{props.title && } {props.description &&

Optional Description

}
)} {!props.useOwnActionButtons && ( {/* This will require the i18n string passed in */} {props.actionOnClick ? ( ) : ( )} )}
) ); type DialogHeaderProps = { title: React.ReactNode; subtitle?: React.ReactNode; }; export function DialogHeader(props: DialogHeaderProps) { return ( <> {props.subtitle &&
{props.subtitle}
} ); } export function DialogFooter(props: { children: ReactNode }) { return (
{props.children}
); } DialogContent.displayName = "DialogContent"; export const DialogTrigger = DialogPrimitive.Trigger; export const DialogClose = DialogPrimitive.Close;