refactor(ui): centralise themed dialogs
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import React, { createContext, useCallback, useContext, useMemo, useState } from "react";
|
||||
import { Button, Dialog, DialogActions, DialogContent, DialogTitle, TextField, Typography } from "@mui/material";
|
||||
import { useOptionalI18n } from "./i18n/I18nProvider";
|
||||
|
||||
type PromptOptions = {
|
||||
title?: string;
|
||||
@@ -21,6 +22,7 @@ type PromptState = PromptOptions & {
|
||||
const PromptContext = createContext<PromptContextValue | null>(null);
|
||||
|
||||
export function PromptProvider({ children }: { children: React.ReactNode }) {
|
||||
const i18n = useOptionalI18n();
|
||||
const [state, setState] = useState<PromptState>({
|
||||
open: false,
|
||||
title: "Enter value",
|
||||
@@ -43,28 +45,29 @@ export function PromptProvider({ children }: { children: React.ReactNode }) {
|
||||
setValue(options.defaultValue ?? "");
|
||||
setState({
|
||||
open: true,
|
||||
title: options.title ?? "Enter value",
|
||||
title: options.title ?? i18n?.t("enterValue") ?? "Enter value",
|
||||
message: options.message,
|
||||
defaultValue: options.defaultValue ?? "",
|
||||
confirmLabel: options.confirmLabel ?? "Save",
|
||||
cancelLabel: options.cancelLabel ?? "Cancel",
|
||||
confirmLabel: options.confirmLabel ?? i18n?.t("save") ?? "Save",
|
||||
cancelLabel: options.cancelLabel ?? i18n?.t("cancel") ?? "Cancel",
|
||||
resolver: resolve,
|
||||
});
|
||||
});
|
||||
}, []);
|
||||
}, [i18n]);
|
||||
|
||||
const contextValue = useMemo(() => ({ prompt }), [prompt]);
|
||||
|
||||
return (
|
||||
<PromptContext.Provider value={contextValue}>
|
||||
{children}
|
||||
<Dialog open={state.open} onClose={() => closeWith(null)} fullWidth maxWidth="xs">
|
||||
<DialogTitle>{state.title}</DialogTitle>
|
||||
<Dialog open={state.open} onClose={() => closeWith(null)} fullWidth maxWidth="xs"
|
||||
slotProps={{ paper: { sx: { borderRadius: 3, border: 1, borderColor: "divider", backgroundImage: "none" } } }}>
|
||||
<DialogTitle sx={{ pb: 1, fontWeight: 800 }}>{state.title}</DialogTitle>
|
||||
<DialogContent>
|
||||
<Typography sx={{ color: "text.secondary", mb: 1.5 }}>{state.message}</Typography>
|
||||
<TextField autoFocus fullWidth value={value} onChange={(e) => setValue(e.target.value)} inputProps={{ maxLength: 180 }} helperText={`${value.length}/180`} />
|
||||
</DialogContent>
|
||||
<DialogActions>
|
||||
<DialogActions sx={{ px: 3, pb: 2.5, gap: 1 }}>
|
||||
<Button onClick={() => closeWith(null)}>{state.cancelLabel}</Button>
|
||||
<Button variant="contained" onClick={() => closeWith(value)}>{state.confirmLabel}</Button>
|
||||
</DialogActions>
|
||||
|
||||
Reference in New Issue
Block a user