import { Box as MuiBox } from '@mui/material'; import { ForwardRefExoticComponent, PropsWithChildren, RefAttributes, createElement, forwardRef, useImperativeHandle, useMemo, useRef, } from 'react'; import { DialogActionArea, DialogScrollBox, DialogWithHeader } from './Dialog'; import FlexBox from './FlexBox'; import sxstring from '../lib/sxstring'; import { BodyText } from './Text'; const ConfirmDialog: ForwardRefExoticComponent< PropsWithChildren & RefAttributes > = forwardRef( ( { actionCancelText = 'Cancel', actionProceedText, children, closeOnProceed = false, contentContainerProps, dialogProps, disableProceed, loading, loadingAction = false, onActionAppend, onCancelAppend, onProceedAppend, openInitially, preActionArea, proceedButtonProps, proceedColour = 'blue', scrollContent = false, scrollBoxProps, showActionArea = true, showCancel, showClose, titleText, wide, // Dependents content = children, }, ref, ) => { const dialogRef = useRef(null); const contentElement = useMemo( () => sxstring(content, BodyText), [content], ); const bodyElement = useMemo( () => createElement( scrollContent ? DialogScrollBox : MuiBox, scrollBoxProps, contentElement, ), [contentElement, scrollBoxProps, scrollContent], ); const actionArea = useMemo( () => showActionArea && ( { onActionAppend?.call(null, ...args); onCancelAppend?.call(null, ...args); }, }} closeOnProceed={closeOnProceed} loading={loadingAction} proceedProps={{ background: proceedColour, children: actionProceedText, disabled: disableProceed, onClick: (...args) => { onActionAppend?.call(null, ...args); onProceedAppend?.call(null, ...args); }, ...proceedButtonProps, }} showCancel={showCancel} /> ), [ actionCancelText, actionProceedText, closeOnProceed, disableProceed, loadingAction, onActionAppend, onCancelAppend, onProceedAppend, proceedButtonProps, proceedColour, showActionArea, showCancel, ], ); useImperativeHandle( ref, () => ({ setOpen: (open) => dialogRef.current?.setOpen(open), }), [], ); return ( {bodyElement} {preActionArea} {actionArea} ); }, ); ConfirmDialog.displayName = 'ConfirmDialog'; export default ConfirmDialog;