fix(striker-ui): allow ConfirmDialog to be a form

main
Tsu-ba-me 2 years ago
parent 1c28f4028c
commit 4006bd04b0
  1. 98
      striker-ui/components/ConfirmDialog.tsx
  2. 3
      striker-ui/types/ConfirmDialog.d.ts

@ -1,5 +1,14 @@
import { Box, Dialog } from '@mui/material'; import { Dialog as MUIDialog } from '@mui/material';
import { forwardRef, useImperativeHandle, useMemo, useState } from 'react'; import {
ButtonHTMLAttributes,
ElementType,
FormEventHandler,
forwardRef,
MouseEventHandler,
useImperativeHandle,
useMemo,
useState,
} from 'react';
import { BLUE, RED, TEXT } from '../lib/consts/DEFAULT_THEME'; import { BLUE, RED, TEXT } from '../lib/consts/DEFAULT_THEME';
@ -25,6 +34,7 @@ const ConfirmDialog = forwardRef<
{ {
actionCancelText = 'Cancel', actionCancelText = 'Cancel',
actionProceedText, actionProceedText,
contentContainerProps = {},
closeOnProceed: isCloseOnProceed = false, closeOnProceed: isCloseOnProceed = false,
content, content,
dialogProps: { dialogProps: {
@ -32,10 +42,12 @@ const ConfirmDialog = forwardRef<
PaperProps: paperProps = {}, PaperProps: paperProps = {},
...restDialogProps ...restDialogProps
} = {}, } = {},
formContent: isFormContent,
loadingAction: isLoadingAction = false, loadingAction: isLoadingAction = false,
onActionAppend, onActionAppend,
onCancelAppend, onCancelAppend,
onProceedAppend, onProceedAppend,
onSubmitAppend,
openInitially = false, openInitially = false,
proceedButtonProps = {}, proceedButtonProps = {},
proceedColour: proceedColourKey = 'blue', proceedColour: proceedColourKey = 'blue',
@ -59,6 +71,54 @@ const ConfirmDialog = forwardRef<
() => MAP_TO_COLOUR[proceedColourKey], () => MAP_TO_COLOUR[proceedColourKey],
[proceedColourKey], [proceedColourKey],
); );
const {
contentContainerComponent,
contentContainerSubmitEventHandler,
proceedButtonClickEventHandler,
proceedButtonType,
} = useMemo(() => {
let ccComponent: ElementType | undefined;
let ccSubmitEventHandler: FormEventHandler<HTMLDivElement> | undefined;
let pbClickEventHandler:
| MouseEventHandler<HTMLButtonElement>
| undefined = (...args) => {
if (isCloseOnProceed) {
setIsOpen(false);
}
onActionAppend?.call(null, ...args);
onProceedAppend?.call(null, ...args);
};
let pbType: ButtonHTMLAttributes<HTMLButtonElement>['type'] | undefined;
if (isFormContent) {
ccComponent = 'form';
ccSubmitEventHandler = (event, ...restArgs) => {
event.preventDefault();
if (isCloseOnProceed) {
setIsOpen(false);
}
onSubmitAppend?.call(null, event, ...restArgs);
};
pbClickEventHandler = undefined;
pbType = 'submit';
}
return {
contentContainerComponent: ccComponent,
contentContainerSubmitEventHandler: ccSubmitEventHandler,
proceedButtonClickEventHandler: pbClickEventHandler,
proceedButtonType: pbType,
};
}, [
isCloseOnProceed,
isFormContent,
onActionAppend,
onProceedAppend,
onSubmitAppend,
]);
const cancelButtonElement = useMemo( const cancelButtonElement = useMemo(
() => ( () => (
@ -78,14 +138,8 @@ const ConfirmDialog = forwardRef<
const proceedButtonElement = useMemo( const proceedButtonElement = useMemo(
() => ( () => (
<ContainedButton <ContainedButton
onClick={(...args) => { onClick={proceedButtonClickEventHandler}
if (isCloseOnProceed) { type={proceedButtonType}
setIsOpen(false);
}
onActionAppend?.call(null, ...args);
onProceedAppend?.call(null, ...args);
}}
{...restProceedButtonProps} {...restProceedButtonProps}
sx={{ sx={{
backgroundColor: proceedColour, backgroundColor: proceedColour,
@ -101,10 +155,9 @@ const ConfirmDialog = forwardRef<
), ),
[ [
actionProceedText, actionProceedText,
isCloseOnProceed, proceedButtonClickEventHandler,
onActionAppend,
onProceedAppend,
proceedButtonSx, proceedButtonSx,
proceedButtonType,
proceedColour, proceedColour,
restProceedButtonProps, restProceedButtonProps,
], ],
@ -125,6 +178,11 @@ const ConfirmDialog = forwardRef<
), ),
[cancelButtonElement, isLoadingAction, proceedButtonElement], [cancelButtonElement, isLoadingAction, proceedButtonElement],
); );
const contentElement = useMemo(
() =>
typeof content === 'string' ? <BodyText text={content} /> : content,
[content],
);
useImperativeHandle( useImperativeHandle(
ref, ref,
@ -135,7 +193,7 @@ const ConfirmDialog = forwardRef<
); );
return ( return (
<Dialog <MUIDialog
open={open} open={open}
PaperComponent={Panel} PaperComponent={Panel}
PaperProps={{ PaperProps={{
@ -147,11 +205,15 @@ const ConfirmDialog = forwardRef<
<PanelHeader> <PanelHeader>
<HeaderText text={titleText} /> <HeaderText text={titleText} />
</PanelHeader> </PanelHeader>
<Box sx={{ marginBottom: '1em' }}> <FlexBox
{typeof content === 'string' ? <BodyText text={content} /> : content} component={contentContainerComponent}
</Box> onSubmit={contentContainerSubmitEventHandler}
{...contentContainerProps}
>
{contentElement}
{actionAreaElement} {actionAreaElement}
</Dialog> </FlexBox>
</MUIDialog>
); );
}, },
); );

@ -1,11 +1,14 @@
type ConfirmDialogOptionalProps = { type ConfirmDialogOptionalProps = {
actionCancelText?: string; actionCancelText?: string;
closeOnProceed?: boolean; closeOnProceed?: boolean;
contentContainerProps?: import('../components/FlexBox').FlexBoxProps;
dialogProps?: Partial<import('@mui/material').DialogProps>; dialogProps?: Partial<import('@mui/material').DialogProps>;
formContent?: boolean;
loadingAction?: boolean; loadingAction?: boolean;
onActionAppend?: ContainedButtonProps['onClick']; onActionAppend?: ContainedButtonProps['onClick'];
onProceedAppend?: ContainedButtonProps['onClick']; onProceedAppend?: ContainedButtonProps['onClick'];
onCancelAppend?: ContainedButtonProps['onClick']; onCancelAppend?: ContainedButtonProps['onClick'];
onSubmitAppend?: import('react').FormEventHandler<HTMLDivElement>;
openInitially?: boolean; openInitially?: boolean;
proceedButtonProps?: ContainedButtonProps; proceedButtonProps?: ContainedButtonProps;
proceedColour?: 'blue' | 'red'; proceedColour?: 'blue' | 'red';

Loading…
Cancel
Save