fix(striker-ui): separate type and render() in ConfirmDialog

* Make ConfirmDialog open state local and expose its setter through ref
main
Tsu-ba-me 2 years ago
parent 37c856d383
commit e41bc3a572
  1. 88
      striker-ui/components/ConfirmDialog.tsx
  2. 4
      striker-ui/components/Files/FileEditForm.tsx
  3. 4
      striker-ui/components/ProvisionServerDialog.tsx
  4. 4
      striker-ui/components/StrikerInitForm.tsx
  5. 18
      striker-ui/types/ConfirmDialog.d.ts

@ -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,42 +7,49 @@ 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>;
proceedButtonProps?: ButtonProps;
titleText: string;
};
const CONFIRM_DIALOG_DEFAULT_PROPS = {
actionCancelText: 'Cancel',
proceedButtonProps: { sx: undefined },
};
const ConfirmDialog = (
{ {
actionCancelText, actionCancelText = 'Cancel',
actionProceedText, actionProceedText,
content, content,
dialogProps: { open }, dialogProps: { open: baseOpen = false, ...restDialogProps } = {},
onCancel, onCancelAppend,
onProceed, onProceedAppend,
proceedButtonProps, openInitially = false,
proceedButtonProps = {},
titleText, titleText,
}: ConfirmDialogProps = CONFIRM_DIALOG_DEFAULT_PROPS as ConfirmDialogProps, },
): JSX.Element => { ref,
const { sx: proceedButtonSx } = ) => {
proceedButtonProps ?? CONFIRM_DIALOG_DEFAULT_PROPS.proceedButtonProps; const { sx: proceedButtonSx } = proceedButtonProps;
const [isOpen, setIsOpen] = useState<boolean>(openInitially);
// TODO: using base open is depreciated; use internal state once all
// dependent components finish the migrate.
const open = useMemo(
() => (ref ? isOpen : baseOpen),
[baseOpen, isOpen, ref],
);
useImperativeHandle(
ref,
() => ({
setOpen: (value) => setIsOpen(value),
}),
[],
);
return ( return (
<Dialog <Dialog
{...{ open }} open={open}
PaperComponent={Panel} PaperComponent={Panel}
PaperProps={{ sx: { overflow: 'visible' } }} PaperProps={{ sx: { overflow: 'visible' } }}
{...restDialogProps}
> >
<PanelHeader> <PanelHeader>
<HeaderText text={titleText} /> <HeaderText text={titleText} />
@ -62,7 +69,15 @@ const ConfirmDialog = (
}, },
}} }}
> >
<ContainedButton onClick={onCancel}>{actionCancelText}</ContainedButton> <ContainedButton
onClick={(...args) => {
setIsOpen(false);
onCancelAppend?.call(null, ...args);
}}
>
{actionCancelText}
</ContainedButton>
<ContainedButton <ContainedButton
sx={{ sx={{
backgroundColor: BLUE, backgroundColor: BLUE,
@ -72,17 +87,20 @@ const ConfirmDialog = (
...proceedButtonSx, ...proceedButtonSx,
}} }}
onClick={onProceed} onClick={(...args) => {
setIsOpen(false);
onProceedAppend?.call(null, ...args);
}}
> >
{actionProceedText} {actionProceedText}
</ContainedButton> </ContainedButton>
</Box> </Box>
</Dialog> </Dialog>
); );
}; },
);
ConfirmDialog.defaultProps = CONFIRM_DIALOG_DEFAULT_PROPS;
export type { ConfirmDialogProps }; ConfirmDialog.displayName = 'ConfirmDialog';
export default ConfirmDialog; export default ConfirmDialog;

@ -327,8 +327,8 @@ const FileEditForm = (
actionProceedText="Purge" actionProceedText="Purge"
content={`${selectedFilesCount} files will be removed from the system. You cannot undo this purge.`} content={`${selectedFilesCount} files will be removed from the system. You cannot undo this purge.`}
dialogProps={{ open: isOpenPurgeConfirmDialog }} dialogProps={{ open: isOpenPurgeConfirmDialog }}
onCancel={cancelPurge} onCancelAppend={cancelPurge}
onProceed={purgeFiles} onProceedAppend={purgeFiles}
proceedButtonProps={{ sx: purgeButtonStyleOverride }} proceedButtonProps={{ sx: purgeButtonStyleOverride }}
titleText={`Are you sure you want to purge ${selectedFilesCount} selected files? `} titleText={`Are you sure you want to purge ${selectedFilesCount} selected files? `}
/> />

@ -1787,10 +1787,10 @@ const ProvisionServerDialog = ({
actionProceedText="Provision" actionProceedText="Provision"
content={createConfirmDialogContent()} content={createConfirmDialogContent()}
dialogProps={{ open: isOpenProvisionConfirmDialog }} dialogProps={{ open: isOpenProvisionConfirmDialog }}
onCancel={() => { onCancelAppend={() => {
setIsOpenProvisionConfirmDialog(false); setIsOpenProvisionConfirmDialog(false);
}} }}
onProceed={() => { onProceedAppend={() => {
const requestBody = JSON.stringify({ const requestBody = JSON.stringify({
serverName: inputServerNameValue, serverName: inputServerNameValue,
cpuCores: inputCPUCoresValue, cpuCores: inputCPUCoresValue,

@ -210,10 +210,10 @@ const StrikerInitForm: FC = () => {
</Grid> </Grid>
} }
dialogProps={{ open: isOpenConfirm }} dialogProps={{ open: isOpenConfirm }}
onCancel={() => { onCancelAppend={() => {
setIsOpenConfirm(false); setIsOpenConfirm(false);
}} }}
onProceed={() => { onProceedAppend={() => {
setSubmitMessage(undefined); setSubmitMessage(undefined);
setIsSubmittingForm(true); setIsSubmittingForm(true);
setIsOpenConfirm(false); setIsOpenConfirm(false);

@ -0,0 +1,18 @@
type ConfirmDialogOptionalProps = {
actionCancelText?: string;
dialogProps?: import('@mui/material').DialogProps;
onCancelAppend?: ContainedButtonProps['onClick'];
openInitially?: boolean;
proceedButtonProps?: ContainedButtonProps;
};
type ConfirmDialogProps = ConfirmDialogOptionalProps & {
actionProceedText: string;
content: import('@mui/material').ReactNode;
onProceedAppend: Exclude<ContainedButtonProps['onClick'], undefined>;
titleText: string;
};
type ConfirmDialogForwardedRefContent = {
setOpen?: (value: boolean) => void;
};
Loading…
Cancel
Save