Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

[web] Standardize single-input forms and unlock screens

AswinAsokf9dcd2d50fd4
diff --git a/web/apps/albums/src/public-album/access/components/PasswordUnlockScreen.tsx b/web/apps/albums/src/public-album/access/components/PasswordUnlockScreen.tsx
index cf1321b2bb..66b8b957b7 100644
--- a/web/apps/albums/src/public-album/access/components/PasswordUnlockScreen.tsx
+++ b/web/apps/albums/src/public-album/access/components/PasswordUnlockScreen.tsx
@@ -2,7 +2,7 @@ import {
PublicAccessLayout,
PublicAccessLayoutTitle,
} from "@/public-album/access/components/PublicAccessLayout";
-import { Stack, Typography } from "@mui/material";
+import { Typography } from "@mui/material";
import {
SingleInputForm,
type SingleInputFormProps,
@@ -18,17 +18,14 @@ export const PasswordUnlockScreen: React.FC<PasswordUnlockScreenProps> = ({
}) => (
<PublicAccessLayout>
<PublicAccessLayoutTitle>{t("password")}</PublicAccessLayoutTitle>
- <Stack>
- <Typography variant="small" sx={{ color: "text.muted", mb: 2 }}>
- {t("link_password_description")}
- </Typography>
- <SingleInputForm
- inputType="password"
- label={t("password")}
- submitButtonColor="primary"
- submitButtonTitle={t("unlock")}
- onSubmit={onSubmit}
- />
- </Stack>
+ <Typography sx={{ color: "text.muted" }}>
+ {t("link_password_description")}
+ </Typography>
+ <SingleInputForm
+ inputType="password"
+ label={t("password")}
+ submitButtonTitle={t("unlock")}
+ onSubmit={onSubmit}
+ />
</PublicAccessLayout>
);
diff --git a/web/apps/albums/src/public-album/access/components/PublicAccessLayout.tsx b/web/apps/albums/src/public-album/access/components/PublicAccessLayout.tsx
index ba304f70f5..0e26211a5c 100644
--- a/web/apps/albums/src/public-album/access/components/PublicAccessLayout.tsx
+++ b/web/apps/albums/src/public-album/access/components/PublicAccessLayout.tsx
@@ -1,7 +1,11 @@
-import { Paper, Stack, styled, Typography } from "@mui/material";
+import { Paper, Stack, Typography } from "@mui/material";
import { CenteredFill } from "ente-base/components/containers";
import { EnteLogo } from "ente-base/components/EnteLogo";
import { NavbarBase } from "ente-base/components/Navbar";
+import {
+ singleInputPagePaperSx,
+ v2TitleSx,
+} from "ente-base/components/SingleInput.styles";
export const PublicAccessLayout: React.FC<React.PropsWithChildren> = ({
children,
@@ -31,28 +35,11 @@ export const PublicAccessLayout: React.FC<React.PropsWithChildren> = ({
}),
]}
>
- <PublicAccessPagePaper>{children}</PublicAccessPagePaper>
+ <Paper sx={singleInputPagePaperSx}>{children}</Paper>
</CenteredFill>
</Stack>
);
-const PublicAccessPagePaper = styled(Paper)(({ theme }) => ({
- marginBlock: theme.spacing(2),
- padding: theme.spacing(5, 3),
- [theme.breakpoints.up("sm")]: { padding: theme.spacing(5) },
- width: "min(420px, 85vw)",
- minHeight: "375px",
- display: "flex",
- flexDirection: "column",
- gap: theme.spacing(4),
- boxShadow: "none",
- borderRadius: "20px",
-}));
-
export const PublicAccessLayoutTitle: React.FC<React.PropsWithChildren> = ({
children,
-}) => (
- <Typography variant="h3" sx={{ flex: 1 }}>
- {children}
- </Typography>
-);
+}) => <Typography sx={v2TitleSx}>{children}</Typography>;
diff --git a/web/apps/embed/src/components/EmbedPasswordForm.tsx b/web/apps/embed/src/components/EmbedPasswordForm.tsx
index 657bd13911..5655d45968 100644
--- a/web/apps/embed/src/components/EmbedPasswordForm.tsx
+++ b/web/apps/embed/src/components/EmbedPasswordForm.tsx
@@ -1,8 +1,9 @@
-import { Stack, Typography } from "@mui/material";
+import { Typography } from "@mui/material";
+import { AccountsPageContents } from "ente-accounts/components/layouts/centered-paper";
import {
- AccountsPageContents,
- AccountsPageTitle,
-} from "ente-accounts/components/layouts/centered-paper";
+ singleInputPagePaperSx,
+ v2TitleSx,
+} from "ente-base/components/SingleInput.styles";
import {
SingleInputForm,
type SingleInputFormProps,
@@ -32,21 +33,18 @@ export const EmbedPasswordForm: React.FC<EmbedPasswordFormProps> = ({
};
return (
- <AccountsPageContents>
- <AccountsPageTitle>{t("password")}</AccountsPageTitle>
- <Stack>
- <Typography variant="small" sx={{ color: "text.muted", mb: 2 }}>
- {t("link_password_description")}
- </Typography>
- <SingleInputForm
- autoFocus={false}
- inputType="password"
- label={t("password")}
- submitButtonColor="primary"
- submitButtonTitle={t("unlock")}
- onSubmit={handleSubmit}
- />
- </Stack>
+ <AccountsPageContents paperSx={singleInputPagePaperSx}>
+ <Typography sx={v2TitleSx}>{t("password")}</Typography>
+ <Typography sx={{ color: "text.muted" }}>
+ {t("link_password_description")}
+ </Typography>
+ <SingleInputForm
+ autoFocus={false}
+ inputType="password"
+ label={t("password")}
+ submitButtonTitle={t("unlock")}
+ onSubmit={handleSubmit}
+ />
</AccountsPageContents>
);
};
diff --git a/web/apps/photos/src/components/collections/AlbumCastDialog.tsx b/web/apps/photos/src/components/collections/AlbumCastDialog.tsx
index 7ebe7f28b2..da2b1cf526 100644
--- a/web/apps/photos/src/components/collections/AlbumCastDialog.tsx
+++ b/web/apps/photos/src/components/collections/AlbumCastDialog.tsx
@@ -345,7 +345,6 @@ const AlbumCastDialogContents: React.FC<AlbumCastDialogProps> = ({
</Stack>
</Stack>
<SingleInputForm
- variant="v2"
label={t("code")}
placeholder={ut("123456")}
submitButtonTitle={t("pair_device_to_tv")}
diff --git a/web/apps/photos/src/components/collections/CollectionShare.tsx b/web/apps/photos/src/components/collections/CollectionShare.tsx
index c0aeb67b94..3efb58e3ce 100644
--- a/web/apps/photos/src/components/collections/CollectionShare.tsx
+++ b/web/apps/photos/src/components/collections/CollectionShare.tsx
@@ -10,7 +10,6 @@ import AddIcon from "@mui/icons-material/Add";
import AdminPanelSettingsIcon from "@mui/icons-material/AdminPanelSettings";
import BlockIcon from "@mui/icons-material/Block";
import ChevronRightIcon from "@mui/icons-material/ChevronRight";
-import CloseIcon from "@mui/icons-material/Close";
import CodeIcon from "@mui/icons-material/Code";
import ContentCopyIcon from "@mui/icons-material/ContentCopyOutlined";
import DoneIcon from "@mui/icons-material/Done";
@@ -22,16 +21,7 @@ import Photo, { default as PhotoIcon } from "@mui/icons-material/Photo";
import PublicIcon from "@mui/icons-material/Public";
import RemoveCircleOutlinedIcon from "@mui/icons-material/RemoveCircleOutlined";
import WorkspacesIcon from "@mui/icons-material/Workspaces";
-import {
- Box,
- Dialog,
- DialogTitle,
- IconButton,
- Stack,
- styled,
- ThemeProvider,
- Typography,
-} from "@mui/material";
+import { Box, Stack, styled, ThemeProvider, Typography } from "@mui/material";
import NumberAvatar from "@mui/material/Avatar";
import TextField from "@mui/material/TextField";
import type { LocalUser } from "ente-accounts/services/user";
@@ -57,12 +47,7 @@ import {
RowLabel,
RowSwitch,
} from "ente-base/components/RowButton";
-import {
- v2CloseButtonSx,
- v2HeaderRowSx,
- v2PaperSx,
- v2TitleSx,
-} from "ente-base/components/SingleInput.styles";
+import { SingleInputDialog } from "ente-base/components/SingleInputDialog";
import {
SingleInputForm,
type SingleInputFormProps,
@@ -107,7 +92,6 @@ import React, {
useCallback,
useContext,
useEffect,
- useId,
useMemo,
useRef,
useState,
@@ -2406,13 +2390,9 @@ const SetPublicLinkPassword: React.FC<SetPublicLinkPasswordProps> = ({
onUpdate,
}) => {
const inPanel = !!useContext(SidebarPanelContext);
- const titleID = useId();
const savePassword: SingleInputFormProps["onSubmit"] = async (password) => {
await enablePublicUrlPassword(password);
publicURL.passwordEnabled = true;
- onClose();
- // Keep the submitting state through the close animation.
- return wait(1000);
};
const enablePublicUrlPassword = async (password: string) => {
@@ -2425,65 +2405,7 @@ const SetPublicLinkPassword: React.FC<SetPublicLinkPasswordProps> = ({
});
};
- const passwordForm = (
- <SingleInputForm
- variant={variant == "inline" ? "people" : undefined}
- inputType="password"
- label={t("password")}
- submitButtonColor="primary"
- submitButtonTitle={t("lock")}
- onCancel={onClose}
- onSubmit={savePassword}
- />
- );
-
- if (variant == "inline") {
- return (
- <Dialog
- {...{ open, onClose }}
- aria-labelledby={titleID}
- maxWidth={false}
- slotProps={{ paper: { sx: v2PaperSx } }}
- >
- <Stack sx={{ p: "20px", gap: "20px" }}>
- <Stack direction="row" sx={v2HeaderRowSx}>
- <DialogTitle
- id={titleID}
- sx={{
- ...v2TitleSx,
- fontSize: "20px",
- lineHeight: "28px",
- "&&": { p: 0 },
- }}
- >
- {t("password_lock")}
- </DialogTitle>
- <IconButton
- aria-label={t("close")}
- onClick={onClose}
- sx={v2CloseButtonSx}
- >
- <CloseIcon sx={{ fontSize: 18 }} />
- </IconButton>
- </Stack>
- {passwordForm}
- </Stack>
- </Dialog>
- );
- }
-
- const form = (
- <Stack sx={{ gap: 3, p: 1.5 }}>
- {!inPanel && (
- <Typography id={titleID} variant="h3" sx={{ px: 1, py: 0.5 }}>
- {t("password_lock")}
- </Typography>
- )}
- {passwordForm}
- </Stack>
- );
-
- if (inPanel) {
+ if (inPanel && variant != "inline") {
return (
<TitledNestedSidebarDrawer
anchor="right"
@@ -2491,26 +2413,34 @@ const SetPublicLinkPassword: React.FC<SetPublicLinkPasswordProps> = ({
onRootClose={onClose}
title={t("password_lock")}
>
- {form}
+ <Stack sx={{ gap: 3, p: 1.5 }}>
+ <SingleInputForm
+ key={open ? "open" : "closed"}
+ inputType="password"
+ label={t("password")}
+ submitButtonTitle={t("lock")}
+ onCancel={onClose}
+ onSubmit={async (password, setFieldError) => {
+ await savePassword(password, setFieldError);
+ onClose();
+ // Keep the submitting state through the close animation.
+ return wait(1000);
+ }}
+ />
+ </Stack>
</TitledNestedSidebarDrawer>
);
}
return (
- <Dialog
+ <SingleInputDialog
{...{ open, onClose }}
- aria-labelledby={titleID}
- disablePortal
- slotProps={{
- backdrop: { sx: { position: "absolute" } },
- paper: { sx: { "&&": { padding: "4px" } } },
- }}
- sx={{ position: "absolute" }}
- maxWidth={"sm"}
- fullWidth
- >
- {form}
- </Dialog>
+ title={t("password_lock")}
+ inputType="password"
+ label={t("password")}
+ submitButtonTitle={t("lock")}
+ onSubmit={savePassword}
+ />
);
};
diff --git a/web/apps/photos/src/components/sidebar/ReferralSettings.tsx b/web/apps/photos/src/components/sidebar/ReferralSettings.tsx
index c45723df0d..c527b7ade2 100644
--- a/web/apps/photos/src/components/sidebar/ReferralSettings.tsx
+++ b/web/apps/photos/src/components/sidebar/ReferralSettings.tsx
@@ -11,16 +11,13 @@ import CheckCircleOutlinedIcon from "@mui/icons-material/CheckCircleOutlined";
import ChevronRightIcon from "@mui/icons-material/ChevronRight";
import EditIcon from "@mui/icons-material/Edit";
import { Box, Button, IconButton, Stack, Typography } from "@mui/material";
-import { TitledMiniDialog } from "ente-base/components/MiniDialog";
import {
RowButton,
RowButtonDivider,
RowButtonGroup,
} from "ente-base/components/RowButton";
-import {
- SingleInputForm,
- type SingleInputFormProps,
-} from "ente-base/components/SingleInputForm";
+import { SingleInputDialog } from "ente-base/components/SingleInputDialog";
+import type { SingleInputFormProps } from "ente-base/components/SingleInputForm";
import { ActivityIndicator } from "ente-base/components/mui/ActivityIndicator";
import {
TitledNestedSidebarDrawer,
@@ -595,7 +592,6 @@ const EditCodeDialog: React.FC<EditCodeDialogProps> = ({
const normalized = normalizeReferralCode(code);
if (normalized == normalizeReferralCode(currentCode)) {
- onClose();
return;
}
@@ -611,7 +607,6 @@ const EditCodeDialog: React.FC<EditCodeDialogProps> = ({
try {
await changeReferralCode(normalized);
await onChanged();
- onClose();
} catch (e) {
log.error("Failed to change referral code", e);
@@ -637,15 +632,14 @@ const EditCodeDialog: React.FC<EditCodeDialogProps> = ({
};
return (
- <TitledMiniDialog {...{ open, onClose }} title={t("change_code")}>
- <SingleInputForm
- initialValue={currentCode}
- label={t("enter_referral_code")}
- submitButtonTitle={t("save")}
- onCancel={onClose}
- onSubmit={handleSubmit}
- />
- </TitledMiniDialog>
+ <SingleInputDialog
+ {...{ open, onClose }}
+ title={t("change_code")}
+ initialValue={currentCode}
+ label={t("enter_referral_code")}
+ submitButtonTitle={t("save")}
+ onSubmit={handleSubmit}
+ />
);
};
@@ -710,29 +704,15 @@ const ApplyCodeDialog: React.FC<ApplyCodeDialogProps> = ({
};
return (
- <TitledMiniDialog {...{ open, onClose }} title={t("apply_code")}>
- <Stack sx={{ gap: 1 }}>
- <Typography
- id="referral-code-apply-description"
- sx={{ color: "text.muted" }}
- >
- {t("apply_code_description")}
- </Typography>
- <SingleInputForm
- label={t("enter_referral_code")}
- slotProps={{
- htmlInput: {
- "aria-describedby":
- "referral-code-apply-description",
- style: { textTransform: "uppercase" },
- },
- }}
- submitButtonTitle={t("apply_code")}
- onCancel={onClose}
- onSubmit={handleSubmit}
- />
- </Stack>
- </TitledMiniDialog>
+ <SingleInputDialog
+ {...{ open, onClose }}
+ title={t("apply_code")}
+ description={t("apply_code_description")}
+ label={t("enter_referral_code")}
+ inputProps={{ style: { textTransform: "uppercase" } }}
+ submitButtonTitle={t("apply_code")}
+ onSubmit={handleSubmit}
+ />
);
};
diff --git a/web/apps/photos/tests/single-input-dialog.test.tsx b/web/apps/photos/tests/single-input-dialog.test.tsx
new file mode 100644
index 0000000000..3167ac0ba2
--- /dev/null
+++ b/web/apps/photos/tests/single-input-dialog.test.tsx
@@ -0,0 +1,86 @@
+import { ThemeProvider } from "@mui/material";
+import { SingleInputDialog } from "ente-base/components/SingleInputDialog";
+import type { SingleInputFormProps } from "ente-base/components/SingleInputForm";
+import { photosTheme } from "ente-base/components/utils/theme";
+import type { PropsWithChildren } from "react";
+import { renderToStaticMarkup } from "react-dom/server";
+import { beforeEach, describe, expect, test, vi } from "vitest";
+
+// Exercise the dialog's submission contract independently of browser form events.
+const captured = vi.hoisted(() => ({
+ props: undefined as SingleInputFormProps | undefined,
+}));
+vi.mock("@mui/material", async (importOriginal) => ({
+ ...(await importOriginal<typeof import("@mui/material")>()),
+ Dialog: ({ children }: PropsWithChildren) => <div>{children}</div>,
+}));
+vi.mock("ente-base/components/SingleInputForm", () => ({
+ SingleInputForm: (props: SingleInputFormProps) => {
+ captured.props = props;
+ return null;
+ },
+}));
+
+const prepare = (onSubmit: SingleInputFormProps["onSubmit"]) => {
+ const onClose = vi.fn();
+ renderToStaticMarkup(
+ <ThemeProvider theme={photosTheme}>
+ <SingleInputDialog
+ open
+ title="Apply code"
+ label="Referral code"
+ submitButtonTitle="Apply"
+ onClose={onClose}
+ onSubmit={onSubmit}
+ />
+ </ThemeProvider>,
+ );
+ const submit = captured.props?.onSubmit;
+ if (!submit) throw new Error("The dialog did not render its form");
+ return { submit, onClose };
+};
+
+beforeEach(() => {
+ captured.props = undefined;
+});
+
+describe("single input dialog submission", () => {
+ test("closes after a successful submission", async () => {
+ const onSubmit = vi.fn();
+ const { submit, onClose } = prepare(onSubmit);
+ await submit("FRIEND", vi.fn());
+ expect(onSubmit).toHaveBeenCalledWith("FRIEND", expect.any(Function));
+ expect(onClose).toHaveBeenCalledOnce();
+ });
+
+ test("keeps referral validation errors visible", async () => {
+ const { submit, onClose } = prepare((_value, setFieldError) => {
+ setFieldError("Invalid code");
+ });
+ const setFieldError = vi.fn();
+ await submit("bad-code", setFieldError);
+ expect(setFieldError).toHaveBeenCalledWith("Invalid code");
+ expect(onClose).not.toHaveBeenCalled();
+ });
+
+ test("keeps asynchronous server errors visible", async () => {
+ const { submit, onClose } = prepare(async (_value, setFieldError) => {
+ await Promise.resolve();
+ setFieldError("Code unavailable");
+ });
+ const setFieldError = vi.fn();
+ await submit("TAKEN", setFieldError);
+ expect(setFieldError).toHaveBeenCalledWith("Code unavailable");
+ expect(onClose).not.toHaveBeenCalled();
+ });
+
+ test("leaves thrown failures to the form without closing", async () => {
+ const { submit, onClose } = prepare(() => {
+ throw new Error("Request failed");
+ });
+ await expect(submit("FRIEND", vi.fn())).rejects.toThrow(
+ "Request failed",
+ );
+ expect(onClose).not.toHaveBeenCalled();
+ });
+});
diff --git a/web/apps/share/src/components/file-share/CollectionShareView.tsx b/web/apps/share/src/components/file-share/CollectionShareView.tsx
index e8fd0054b1..4dec7644f7 100644
--- a/web/apps/share/src/components/file-share/CollectionShareView.tsx
+++ b/web/apps/share/src/components/file-share/CollectionShareView.tsx
@@ -175,7 +175,7 @@ export const CollectionShareView: React.FC = () => {
<SingleInputForm
inputType="password"
label={passwordTitle}
- submitButtonColor="primary"
+
submitButtonTitle={unlockLabel}
onSubmit={handleSubmitPassword}
/>
diff --git a/web/packages/base/components/SingleInput.styles.ts b/web/packages/base/components/SingleInput.styles.ts
index 4ca890452f..8a7706e005 100644
--- a/web/packages/base/components/SingleInput.styles.ts
+++ b/web/packages/base/components/SingleInput.styles.ts
@@ -19,6 +19,16 @@ export const v2PaperSx = ((theme: Theme) => ({
backgroundColor: "#1b1b1b",
}),
})) satisfies SxProps<Theme>;
+export const singleInputPagePaperSx = ((theme: Theme) => ({
+ ...v2PaperSx(theme),
+ boxSizing: "border-box",
+ p: "20px",
+ minHeight: "auto",
+ display: "flex",
+ flexDirection: "column",
+ gap: "20px",
+})) satisfies SxProps<Theme>;
+
export const v2HeaderRowSx = {
alignItems: "center",
justifyContent: "space-between",
diff --git a/web/packages/base/components/SingleInputDialog.tsx b/web/packages/base/components/SingleInputDialog.tsx
index 7309f0a3e1..2c51f95325 100644
--- a/web/packages/base/components/SingleInputDialog.tsx
+++ b/web/packages/base/components/SingleInputDialog.tsx
@@ -19,8 +19,9 @@ import {
import { SingleInputForm, type SingleInputFormProps } from "./SingleInputForm";
type SingleInputDialogProps = ModalVisibilityProps &
- Omit<SingleInputFormProps, "onCancel" | "variant" | "submitButtonColor"> & {
+ Omit<SingleInputFormProps, "onCancel"> & {
title: string;
+ description?: string;
sx?: SxProps<Theme>;
};
@@ -29,14 +30,21 @@ export const SingleInputDialog: React.FC<SingleInputDialogProps> = ({
onClose,
onSubmit,
title,
+ description,
+ inputProps,
sx,
...rest
}) => {
const titleID = useId();
+ const descriptionID = useId();
const handleSubmit: SingleInputFormProps["onSubmit"] = useCallback(
async (value, setFieldError) => {
- await onSubmit(value, setFieldError);
- onClose();
+ const submission = { hasError: false };
+ await onSubmit(value, (message) => {
+ submission.hasError = true;
+ setFieldError(message);
+ });
+ if (!submission.hasError) onClose();
},
[onClose, onSubmit],
);
@@ -47,6 +55,7 @@ export const SingleInputDialog: React.FC<SingleInputDialogProps> = ({
onClose={onClose}
maxWidth={false}
aria-labelledby={titleID}
+ aria-describedby={description ? descriptionID : undefined}
sx={sx}
slotProps={{ paper: { sx: v2PaperSx } }}
>
@@ -63,11 +72,25 @@ export const SingleInputDialog: React.FC<SingleInputDialogProps> = ({
<CloseIcon sx={{ fontSize: 18 }} />
</IconButton>
</Stack>
+ {description && (
+ <Typography id={descriptionID} sx={{ color: "text.muted" }}>
+ {description}
+ </Typography>
+ )}
<SingleInputForm
key={open ? "open" : "closed"}
- variant="v2"
onCancel={onClose}
onSubmit={handleSubmit}
+ inputProps={{
+ ...inputProps,
+ "aria-describedby":
+ [
+ inputProps?.["aria-describedby"],
+ description ? descriptionID : undefined,
+ ]
+ .filter(Boolean)
+ .join(" ") || undefined,
+ }}
{...rest}
/>
</Stack>
diff --git a/web/packages/base/components/SingleInputForm.tsx b/web/packages/base/components/SingleInputForm.tsx
index c8e3f681db..576951fc20 100644
--- a/web/packages/base/components/SingleInputForm.tsx
+++ b/web/packages/base/components/SingleInputForm.tsx
@@ -3,13 +3,10 @@ import {
CircularProgress,
InputBase,
Stack,
- TextField,
Typography,
- type ButtonProps,
+ type InputBaseProps,
type TextFieldProps,
} from "@mui/material";
-import { FocusVisibleButton } from "ente-base/components/mui/FocusVisibleButton";
-import { LoadingButton } from "ente-base/components/mui/LoadingButton";
import log from "ente-base/log";
import { useFormik } from "formik";
import { t } from "i18next";
@@ -26,14 +23,13 @@ import { ShowHidePasswordInputAdornment } from "./mui/PasswordInputAdornment";
export type SingleInputFormProps = Pick<
TextFieldProps,
- "label" | "placeholder" | "autoComplete" | "autoFocus" | "slotProps"
+ "label" | "placeholder" | "autoComplete" | "autoFocus"
> & {
- variant?: "default" | "v2" | "people";
+ inputProps?: InputBaseProps["inputProps"];
endAdornment?: React.ReactNode;
selectOnFocus?: boolean;
inputType?: TextFieldProps["type"];
initialValue?: string;
- submitButtonColor?: ButtonProps["color"];
submitButtonTitle: string;
onCancel?: () => void;
// Do not throw after setFieldError; the catch replaces it with a generic error.
@@ -46,14 +42,13 @@ export type SingleInputFormProps = Pick<
};
export const SingleInputForm: React.FC<SingleInputFormProps> = ({
- variant = "default",
inputType,
initialValue,
autoFocus,
endAdornment,
selectOnFocus,
submitButtonTitle,
- submitButtonColor,
+ inputProps,
onCancel,
onSubmit,
...rest
@@ -69,7 +64,7 @@ export const SingleInputForm: React.FC<SingleInputFormProps> = ({
const formik = useFormik({
initialValues: { value: initialValue ?? "" },
- enableReinitialize: variant !== "default",
+ enableReinitialize: true,
onSubmit: async (values, { setFieldError }) => {
const value = values.value;
const setValueFieldError = (message: string) =>
@@ -88,147 +83,92 @@ export const SingleInputForm: React.FC<SingleInputFormProps> = ({
},
});
- if (variant !== "default") {
- const error = formik.errors.value;
- return (
- <Stack
- component="form"
- onSubmit={formik.handleSubmit}
- sx={{ gap: "20px" }}
- >
- <Stack sx={{ gap: "8px" }}>
- {rest.label && (
- <Typography
- component="label"
- htmlFor={inputID}
- sx={v2LabelSx}
- >
- {rest.label}
- </Typography>
- )}
- <InputBase
- id={inputID}
- aria-describedby={error ? helperID : undefined}
- inputProps={{ "aria-invalid": !!error }}
- name="value"
- value={formik.values.value}
- onChange={formik.handleChange}
- onFocus={
- selectOnFocus
- ? (event) => event.target.select()
- : undefined
- }
- type={showPassword ? "text" : (inputType ?? "text")}
- placeholder={rest.placeholder}
- autoComplete={rest.autoComplete}
- autoFocus={autoFocus ?? true}
- disabled={formik.isSubmitting}
- error={!!error}
- sx={v2InputSx}
- endAdornment={
- inputType == "password" ? (
- <ShowHidePasswordInputAdornment
- showPassword={showPassword}
- onToggle={handleToggleShowHidePassword}
- />
- ) : (
- endAdornment
- )
- }
- />
+ const error = formik.errors.value;
+ return (
+ <Stack
+ component="form"
+ onSubmit={formik.handleSubmit}
+ sx={{ gap: "20px" }}
+ >
+ <Stack sx={{ gap: "8px" }}>
+ {rest.label && (
<Typography
- id={helperID}
- aria-live="polite"
- sx={v2HelperSx(!!error)}
+ component="label"
+ htmlFor={inputID}
+ sx={v2LabelSx}
>
- {error ?? ""}
+ {rest.label}
</Typography>
- </Stack>
- <Stack direction="row" sx={{ gap: "12px" }}>
- {onCancel && (
- <ButtonBase
- onClick={onCancel}
- disabled={formik.isSubmitting}
- sx={[v2CancelButtonSx, peopleActionFocusSx]}
- >
- {t("cancel")}
- </ButtonBase>
- )}
- <ButtonBase
- type="submit"
- disabled={formik.isSubmitting}
- sx={[v2SubmitButtonSx, peopleActionFocusSx]}
- >
- {formik.isSubmitting ? (
- <CircularProgress
- size={20}
- sx={{ color: "#fff" }}
+ )}
+ <InputBase
+ id={inputID}
+ inputProps={{
+ ...inputProps,
+ "aria-invalid": !!error,
+ "aria-describedby":
+ [
+ inputProps?.["aria-describedby"],
+ error ? helperID : undefined,
+ ]
+ .filter(Boolean)
+ .join(" ") || undefined,
+ }}
+ name="value"
+ value={formik.values.value}
+ onChange={formik.handleChange}
+ onFocus={
+ selectOnFocus
+ ? (event) => event.target.select()
+ : undefined
+ }
+ type={showPassword ? "text" : (inputType ?? "text")}
+ placeholder={rest.placeholder}
+ autoComplete={rest.autoComplete}
+ autoFocus={autoFocus ?? true}
+ disabled={formik.isSubmitting}
+ error={!!error}
+ sx={v2InputSx}
+ endAdornment={
+ inputType == "password" ? (
+ <ShowHidePasswordInputAdornment
+ showPassword={showPassword}
+ onToggle={handleToggleShowHidePassword}
/>
) : (
- submitButtonTitle
- )}
- </ButtonBase>
- </Stack>
+ endAdornment
+ )
+ }
+ />
+ <Typography
+ id={helperID}
+ aria-live="polite"
+ sx={v2HelperSx(!!error)}
+ >
+ {error ?? ""}
+ </Typography>
</Stack>
- );
- }
-
- const submitButton = (
- <LoadingButton
- fullWidth
- type="submit"
- loading={formik.isSubmitting}
- color={submitButtonColor ?? "accent"}
- >
- {submitButtonTitle}
- </LoadingButton>
- );
-
- return (
- <form onSubmit={formik.handleSubmit}>
- <TextField
- name="value"
- value={formik.values.value}
- onChange={formik.handleChange}
- type={showPassword ? "text" : (inputType ?? "text")}
- fullWidth
- autoFocus={autoFocus ?? true}
- margin="normal"
- disabled={formik.isSubmitting}
- error={!!formik.errors.value}
- // The space reserves helper-text height before an error appears.
- helperText={formik.errors.value ?? " "}
- slotProps={{
- input:
- inputType == "password"
- ? {
- endAdornment: (
- <ShowHidePasswordInputAdornment
- showPassword={showPassword}
- onToggle={
- handleToggleShowHidePassword
- }
- />
- ),
- }
- : {},
- }}
- {...rest}
- />
- {onCancel ? (
- <Stack direction="row" sx={{ gap: "12px" }}>
- <FocusVisibleButton
- fullWidth
- color="secondary"
+ <Stack direction="row" sx={{ gap: "12px" }}>
+ {onCancel && (
+ <ButtonBase
onClick={onCancel}
+ disabled={formik.isSubmitting}
+ sx={[v2CancelButtonSx, peopleActionFocusSx]}
>
{t("cancel")}
- </FocusVisibleButton>
- {submitButton}
- </Stack>
- ) : (
- submitButton
- )}
- </form>
+ </ButtonBase>
+ )}
+ <ButtonBase
+ type="submit"
+ disabled={formik.isSubmitting}
+ sx={[v2SubmitButtonSx, peopleActionFocusSx]}
+ >
+ {formik.isSubmitting ? (
+ <CircularProgress size={20} sx={{ color: "#fff" }} />
+ ) : (
+ submitButtonTitle
+ )}
+ </ButtonBase>
+ </Stack>
+ </Stack>
);
};