mirror of
https://github.com/hexastack/hexabot
synced 2025-06-26 18:27:28 +00:00
fix(frontend): add dialog extra props
This commit is contained in:
parent
64707c5cf5
commit
268dd6812b
@ -13,7 +13,7 @@ import {
|
|||||||
DialogContent,
|
DialogContent,
|
||||||
Grid,
|
Grid,
|
||||||
} from "@mui/material";
|
} from "@mui/material";
|
||||||
import { FC, ReactNode } from "react";
|
import { cloneElement, FC, ReactNode } from "react";
|
||||||
|
|
||||||
import { useTranslate } from "@/hooks/useTranslate";
|
import { useTranslate } from "@/hooks/useTranslate";
|
||||||
import { ConfirmOptions, DialogProps } from "@/types/common/dialogs.types";
|
import { ConfirmOptions, DialogProps } from "@/types/common/dialogs.types";
|
||||||
@ -27,12 +27,20 @@ export interface ConfirmDialogPayload extends ConfirmOptions {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export interface ConfirmDialogProps
|
export interface ConfirmDialogProps
|
||||||
extends DialogProps<ConfirmDialogPayload, boolean> {}
|
extends DialogProps<ConfirmDialogPayload, boolean> {
|
||||||
|
mode?: "selection" | "click";
|
||||||
|
count?: number;
|
||||||
|
}
|
||||||
|
|
||||||
export const ConfirmDialog: FC<ConfirmDialogProps> = ({ payload, ...rest }) => {
|
export const ConfirmDialog: FC<ConfirmDialogProps> = ({ payload, ...rest }) => {
|
||||||
const { t } = useTranslate();
|
const { t } = useTranslate();
|
||||||
const cancelButtonProps = useDialogLoadingButton(() => rest.onClose(false));
|
const cancelButtonProps = useDialogLoadingButton(() => rest.onClose(false));
|
||||||
const okButtonProps = useDialogLoadingButton(() => rest.onClose(true));
|
const okButtonProps = useDialogLoadingButton(() => rest.onClose(true));
|
||||||
|
// @ts-ignore
|
||||||
|
const messageReactNode = cloneElement(payload.msg, {
|
||||||
|
mode: rest.mode,
|
||||||
|
count: rest.count,
|
||||||
|
});
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Dialog
|
<Dialog
|
||||||
@ -44,7 +52,7 @@ export const ConfirmDialog: FC<ConfirmDialogProps> = ({ payload, ...rest }) => {
|
|||||||
<DialogTitle onClose={() => rest.onClose(false)}>
|
<DialogTitle onClose={() => rest.onClose(false)}>
|
||||||
{payload.title || t("title.warning")}
|
{payload.title || t("title.warning")}
|
||||||
</DialogTitle>
|
</DialogTitle>
|
||||||
<DialogContent>{payload.msg}</DialogContent>
|
<DialogContent>{messageReactNode}</DialogContent>
|
||||||
<DialogActions>
|
<DialogActions>
|
||||||
<Grid p="0.3rem 1rem" gap="0.5rem" display="flex">
|
<Grid p="0.3rem 1rem" gap="0.5rem" display="flex">
|
||||||
<Button
|
<Button
|
||||||
|
|||||||
@ -161,10 +161,8 @@ export const Categories = () => {
|
|||||||
variant="contained"
|
variant="contained"
|
||||||
onClick={async () => {
|
onClick={async () => {
|
||||||
const isConfirmed = await dialogs.confirm(
|
const isConfirmed = await dialogs.confirm(
|
||||||
<ConfirmDialogBody
|
<ConfirmDialogBody />,
|
||||||
mode="selection"
|
{ mode: "selection", count: selectedCategories.length },
|
||||||
count={selectedCategories.length}
|
|
||||||
/>,
|
|
||||||
);
|
);
|
||||||
|
|
||||||
if (isConfirmed) {
|
if (isConfirmed) {
|
||||||
|
|||||||
@ -77,6 +77,7 @@ function DialogsProvider(props: DialogProviderProps) {
|
|||||||
payload,
|
payload,
|
||||||
onClose,
|
onClose,
|
||||||
resolve,
|
resolve,
|
||||||
|
msgProps: { count: options.count, mode: options.mode },
|
||||||
};
|
};
|
||||||
|
|
||||||
setStack((prevStack) => [...prevStack, newEntry]);
|
setStack((prevStack) => [...prevStack, newEntry]);
|
||||||
@ -128,7 +129,7 @@ function DialogsProvider(props: DialogProviderProps) {
|
|||||||
return (
|
return (
|
||||||
<DialogsContext.Provider value={contextValue}>
|
<DialogsContext.Provider value={contextValue}>
|
||||||
{children}
|
{children}
|
||||||
{stack.map(({ key, open, Component, payload, promise }) => (
|
{stack.map(({ key, open, Component, payload, promise, msgProps }) => (
|
||||||
<Component
|
<Component
|
||||||
key={key}
|
key={key}
|
||||||
payload={payload}
|
payload={payload}
|
||||||
@ -136,6 +137,7 @@ function DialogsProvider(props: DialogProviderProps) {
|
|||||||
onClose={async (result) => {
|
onClose={async (result) => {
|
||||||
await closeDialog(promise, result);
|
await closeDialog(promise, result);
|
||||||
}}
|
}}
|
||||||
|
{...msgProps}
|
||||||
/>
|
/>
|
||||||
))}
|
))}
|
||||||
</DialogsContext.Provider>
|
</DialogsContext.Provider>
|
||||||
|
|||||||
@ -31,8 +31,22 @@ export const useDialogs = (): DialogHook => {
|
|||||||
|
|
||||||
const { open, close } = context;
|
const { open, close } = context;
|
||||||
const confirm = React.useCallback<OpenConfirmDialog>(
|
const confirm = React.useCallback<OpenConfirmDialog>(
|
||||||
async (msg, { onClose, ...options } = {}) =>
|
async (msg, { onClose, ...options } = {}) => {
|
||||||
open(ConfirmDialog, { ...options, msg }, { onClose }),
|
const { count, mode, ...rest } = options;
|
||||||
|
|
||||||
|
return open(
|
||||||
|
ConfirmDialog,
|
||||||
|
{
|
||||||
|
...rest,
|
||||||
|
msg,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
mode,
|
||||||
|
count,
|
||||||
|
onClose,
|
||||||
|
},
|
||||||
|
);
|
||||||
|
},
|
||||||
[open],
|
[open],
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|||||||
@ -9,8 +9,12 @@
|
|||||||
import { DialogProps as MuiDialogProps } from "@mui/material";
|
import { DialogProps as MuiDialogProps } from "@mui/material";
|
||||||
import { BaseSyntheticEvent } from "react";
|
import { BaseSyntheticEvent } from "react";
|
||||||
|
|
||||||
|
interface ConfirmDialogExtraOptions {
|
||||||
|
mode?: "click" | "selection";
|
||||||
|
count?: number;
|
||||||
|
}
|
||||||
// context
|
// context
|
||||||
export interface OpenDialogOptions<R> {
|
export interface OpenDialogOptions<R> extends ConfirmDialogExtraOptions {
|
||||||
/**
|
/**
|
||||||
* A function that is called before closing the dialog closes. The dialog
|
* A function that is called before closing the dialog closes. The dialog
|
||||||
* stays open as long as the returned promise is not resolved. Use this if
|
* stays open as long as the returned promise is not resolved. Use this if
|
||||||
@ -129,6 +133,7 @@ export interface DialogStackEntry<P, R> {
|
|||||||
payload: P;
|
payload: P;
|
||||||
onClose: (result: R) => Promise<void>;
|
onClose: (result: R) => Promise<void>;
|
||||||
resolve: (result: R) => void;
|
resolve: (result: R) => void;
|
||||||
|
msgProps: ConfirmDialogExtraOptions;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface DialogProviderProps {
|
export interface DialogProviderProps {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user