diff options
Diffstat (limited to 'FrontEnd/src/views/common/dialog')
-rw-r--r-- | FrontEnd/src/views/common/dialog/OperationDialog.tsx | 22 | ||||
-rw-r--r-- | FrontEnd/src/views/common/dialog/index.ts | 56 |
2 files changed, 68 insertions, 10 deletions
diff --git a/FrontEnd/src/views/common/dialog/OperationDialog.tsx b/FrontEnd/src/views/common/dialog/OperationDialog.tsx index be3f7158..74b4a5fa 100644 --- a/FrontEnd/src/views/common/dialog/OperationDialog.tsx +++ b/FrontEnd/src/views/common/dialog/OperationDialog.tsx @@ -39,7 +39,7 @@ function OperationDialogPrompt(props: OperationDialogPromptProps) { export interface OperationDialogProps<TData> { open: boolean; - close: () => void; + onClose: () => void; color?: ThemeColor; inputColor?: ThemeColor; @@ -57,7 +57,7 @@ export interface OperationDialogProps<TData> { function OperationDialog<TData>(props: OperationDialogProps<TData>) { const { open, - close, + onClose, color, inputColor, title, @@ -85,13 +85,14 @@ function OperationDialog<TData>(props: OperationDialogProps<TData>) { const [step, setStep] = useState<Step>({ type: "input" }); - const { inputGroupProps, hasError, setAllDisabled, confirm } = useInputs({ - init: inputs, - }); + const { inputGroupProps, hasErrorAndDirty, setAllDisabled, confirm } = + useInputs({ + init: inputs, + }); - function onClose() { + function close() { if (step.type !== "process") { - close(); + onClose(); if (step.type === "success" && onSuccessAndClose) { onSuccessAndClose?.(step.data); } @@ -142,12 +143,13 @@ function OperationDialog<TData>(props: OperationDialogProps<TData>) { text="operationDialog.cancel" color="secondary" outline - onClick={onClose} + onClick={close} disabled={isProcessing} /> <LoadingButton color={color} loading={isProcessing} + disabled={hasErrorAndDirty} onClick={onConfirm} > {c("operationDialog.confirm")} @@ -173,14 +175,14 @@ function OperationDialog<TData>(props: OperationDialogProps<TData>) { <OperationDialogPrompt {...promptProps} /> <hr /> <div className="cru-dialog-bottom-area"> - <Button text="operationDialog.ok" color="primary" onClick={onClose} /> + <Button text="operationDialog.ok" color="primary" onClick={close} /> </div> </div> ); } return ( - <Dialog open={open} onClose={onClose}> + <Dialog open={open} onClose={close}> <div className={classNames( "cru-operation-dialog-container", diff --git a/FrontEnd/src/views/common/dialog/index.ts b/FrontEnd/src/views/common/dialog/index.ts new file mode 100644 index 00000000..e37b9ed2 --- /dev/null +++ b/FrontEnd/src/views/common/dialog/index.ts @@ -0,0 +1,56 @@ +import { useState } from "react"; + +export { default as Dialog } from "./Dialog"; +export { default as FullPageDialog } from "./FullPageDialog"; +export { default as OperationDialog } from "./OperationDialog"; +export { default as ConfirmDialog } from "./ConfirmDialog"; + +type DialogMap<D extends string, V> = { + [K in D]: V; +}; + +type DialogKeyMap<D extends string> = DialogMap<D, number>; + +type DialogPropsMap<D extends string> = DialogMap< + D, + { key: number | string; open: boolean; onClose: () => void } +>; + +export function useDialog<D extends string>( + dialogs: D[], + initDialog?: D | null, +): { + dialog: D | null; + switchDialog: (newDialog: D | null) => void; + dialogPropsMap: DialogPropsMap<D>; + createDialogSwitch: (newDialog: D | null) => () => void; +} { + const [dialog, setDialog] = useState<D | null>(initDialog ?? null); + + const [dialogKeys, setDialogKeys] = useState<DialogKeyMap<D>>( + () => Object.fromEntries(dialogs.map((d) => [d, 0])) as DialogKeyMap<D>, + ); + + const switchDialog = (newDialog: D | null) => { + if (dialog !== null) { + setDialogKeys({ ...dialogKeys, [dialog]: dialogKeys[dialog] + 1 }); + } + setDialog(newDialog); + }; + + return { + dialog, + switchDialog, + dialogPropsMap: Object.fromEntries( + dialogs.map((d) => [ + d, + { + key: `${d}-${dialogKeys[d]}`, + open: dialog === d, + onClose: () => switchDialog(null), + }, + ]), + ) as DialogPropsMap<D>, + createDialogSwitch: (newDialog: D | null) => () => switchDialog(newDialog), + }; +} |