|
|
@ -1,5 +1,5 @@ |
|
|
|
import { MouseEventHandler, ReactNode } from 'react'; |
|
|
|
import { Box, Dialog } from '@mui/material'; |
|
|
|
import { Box, ButtonProps, Dialog, DialogProps } from '@mui/material'; |
|
|
|
import { forwardRef, useImperativeHandle, useMemo, useState } from 'react'; |
|
|
|
|
|
|
|
|
|
|
|
import { BLUE, TEXT } from '../lib/consts/DEFAULT_THEME'; |
|
|
|
import { BLUE, TEXT } from '../lib/consts/DEFAULT_THEME'; |
|
|
|
|
|
|
|
|
|
|
@ -7,82 +7,100 @@ import ContainedButton from './ContainedButton'; |
|
|
|
import { Panel, PanelHeader } from './Panels'; |
|
|
|
import { Panel, PanelHeader } from './Panels'; |
|
|
|
import { BodyText, HeaderText } from './Text'; |
|
|
|
import { BodyText, HeaderText } from './Text'; |
|
|
|
|
|
|
|
|
|
|
|
type ConfirmDialogProps = { |
|
|
|
const ConfirmDialog = forwardRef< |
|
|
|
actionCancelText?: string; |
|
|
|
ConfirmDialogForwardedRefContent, |
|
|
|
actionProceedText: string; |
|
|
|
ConfirmDialogProps |
|
|
|
content: ReactNode | string; |
|
|
|
>( |
|
|
|
dialogProps: DialogProps; |
|
|
|
( |
|
|
|
onCancel: MouseEventHandler<HTMLButtonElement>; |
|
|
|
{ |
|
|
|
onProceed: MouseEventHandler<HTMLButtonElement>; |
|
|
|
actionCancelText = 'Cancel', |
|
|
|
proceedButtonProps?: ButtonProps; |
|
|
|
actionProceedText, |
|
|
|
titleText: string; |
|
|
|
content, |
|
|
|
}; |
|
|
|
dialogProps: { open: baseOpen = false, ...restDialogProps } = {}, |
|
|
|
|
|
|
|
onCancelAppend, |
|
|
|
|
|
|
|
onProceedAppend, |
|
|
|
|
|
|
|
openInitially = false, |
|
|
|
|
|
|
|
proceedButtonProps = {}, |
|
|
|
|
|
|
|
titleText, |
|
|
|
|
|
|
|
}, |
|
|
|
|
|
|
|
ref, |
|
|
|
|
|
|
|
) => { |
|
|
|
|
|
|
|
const { sx: proceedButtonSx } = proceedButtonProps; |
|
|
|
|
|
|
|
|
|
|
|
const CONFIRM_DIALOG_DEFAULT_PROPS = { |
|
|
|
const [isOpen, setIsOpen] = useState<boolean>(openInitially); |
|
|
|
actionCancelText: 'Cancel', |
|
|
|
|
|
|
|
proceedButtonProps: { sx: undefined }, |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const ConfirmDialog = ( |
|
|
|
// TODO: using base open is depreciated; use internal state once all
|
|
|
|
{ |
|
|
|
// dependent components finish the migrate.
|
|
|
|
actionCancelText, |
|
|
|
const open = useMemo( |
|
|
|
actionProceedText, |
|
|
|
() => (ref ? isOpen : baseOpen), |
|
|
|
content, |
|
|
|
[baseOpen, isOpen, ref], |
|
|
|
dialogProps: { open }, |
|
|
|
); |
|
|
|
onCancel, |
|
|
|
|
|
|
|
onProceed, |
|
|
|
|
|
|
|
proceedButtonProps, |
|
|
|
|
|
|
|
titleText, |
|
|
|
|
|
|
|
}: ConfirmDialogProps = CONFIRM_DIALOG_DEFAULT_PROPS as ConfirmDialogProps, |
|
|
|
|
|
|
|
): JSX.Element => { |
|
|
|
|
|
|
|
const { sx: proceedButtonSx } = |
|
|
|
|
|
|
|
proceedButtonProps ?? CONFIRM_DIALOG_DEFAULT_PROPS.proceedButtonProps; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return ( |
|
|
|
useImperativeHandle( |
|
|
|
<Dialog |
|
|
|
ref, |
|
|
|
{...{ open }} |
|
|
|
() => ({ |
|
|
|
PaperComponent={Panel} |
|
|
|
setOpen: (value) => setIsOpen(value), |
|
|
|
PaperProps={{ sx: { overflow: 'visible' } }} |
|
|
|
}), |
|
|
|
> |
|
|
|
[], |
|
|
|
<PanelHeader> |
|
|
|
); |
|
|
|
<HeaderText text={titleText} /> |
|
|
|
|
|
|
|
</PanelHeader> |
|
|
|
|
|
|
|
<Box sx={{ marginBottom: '1em' }}> |
|
|
|
|
|
|
|
{typeof content === 'string' ? <BodyText text={content} /> : content} |
|
|
|
|
|
|
|
</Box> |
|
|
|
|
|
|
|
<Box |
|
|
|
|
|
|
|
sx={{ |
|
|
|
|
|
|
|
display: 'flex', |
|
|
|
|
|
|
|
flexDirection: 'row', |
|
|
|
|
|
|
|
justifyContent: 'flex-end', |
|
|
|
|
|
|
|
width: '100%', |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
'& > :not(:first-child)': { |
|
|
|
return ( |
|
|
|
marginLeft: '.5em', |
|
|
|
<Dialog |
|
|
|
}, |
|
|
|
open={open} |
|
|
|
}} |
|
|
|
PaperComponent={Panel} |
|
|
|
|
|
|
|
PaperProps={{ sx: { overflow: 'visible' } }} |
|
|
|
|
|
|
|
{...restDialogProps} |
|
|
|
> |
|
|
|
> |
|
|
|
<ContainedButton onClick={onCancel}>{actionCancelText}</ContainedButton> |
|
|
|
<PanelHeader> |
|
|
|
<ContainedButton |
|
|
|
<HeaderText text={titleText} /> |
|
|
|
|
|
|
|
</PanelHeader> |
|
|
|
|
|
|
|
<Box sx={{ marginBottom: '1em' }}> |
|
|
|
|
|
|
|
{typeof content === 'string' ? <BodyText text={content} /> : content} |
|
|
|
|
|
|
|
</Box> |
|
|
|
|
|
|
|
<Box |
|
|
|
sx={{ |
|
|
|
sx={{ |
|
|
|
backgroundColor: BLUE, |
|
|
|
display: 'flex', |
|
|
|
color: TEXT, |
|
|
|
flexDirection: 'row', |
|
|
|
|
|
|
|
justifyContent: 'flex-end', |
|
|
|
|
|
|
|
width: '100%', |
|
|
|
|
|
|
|
|
|
|
|
'&:hover': { backgroundColor: `${BLUE}F0` }, |
|
|
|
'& > :not(:first-child)': { |
|
|
|
|
|
|
|
marginLeft: '.5em', |
|
|
|
...proceedButtonSx, |
|
|
|
}, |
|
|
|
}} |
|
|
|
}} |
|
|
|
onClick={onProceed} |
|
|
|
|
|
|
|
> |
|
|
|
> |
|
|
|
{actionProceedText} |
|
|
|
<ContainedButton |
|
|
|
</ContainedButton> |
|
|
|
onClick={(...args) => { |
|
|
|
</Box> |
|
|
|
setIsOpen(false); |
|
|
|
</Dialog> |
|
|
|
|
|
|
|
); |
|
|
|
onCancelAppend?.call(null, ...args); |
|
|
|
}; |
|
|
|
}} |
|
|
|
|
|
|
|
> |
|
|
|
|
|
|
|
{actionCancelText} |
|
|
|
|
|
|
|
</ContainedButton> |
|
|
|
|
|
|
|
<ContainedButton |
|
|
|
|
|
|
|
sx={{ |
|
|
|
|
|
|
|
backgroundColor: BLUE, |
|
|
|
|
|
|
|
color: TEXT, |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
'&:hover': { backgroundColor: `${BLUE}F0` }, |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
...proceedButtonSx, |
|
|
|
|
|
|
|
}} |
|
|
|
|
|
|
|
onClick={(...args) => { |
|
|
|
|
|
|
|
setIsOpen(false); |
|
|
|
|
|
|
|
|
|
|
|
ConfirmDialog.defaultProps = CONFIRM_DIALOG_DEFAULT_PROPS; |
|
|
|
onProceedAppend?.call(null, ...args); |
|
|
|
|
|
|
|
}} |
|
|
|
|
|
|
|
> |
|
|
|
|
|
|
|
{actionProceedText} |
|
|
|
|
|
|
|
</ContainedButton> |
|
|
|
|
|
|
|
</Box> |
|
|
|
|
|
|
|
</Dialog> |
|
|
|
|
|
|
|
); |
|
|
|
|
|
|
|
}, |
|
|
|
|
|
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
export type { ConfirmDialogProps }; |
|
|
|
ConfirmDialog.displayName = 'ConfirmDialog'; |
|
|
|
|
|
|
|
|
|
|
|
export default ConfirmDialog; |
|
|
|
export default ConfirmDialog; |
|
|
|