Skip to content

#1 PDF Application on GitHub that lets you edit PDFs on any device anywhere

5.5 KB
Raw Blame
import { useEffect } from "react";
import "@editor/auth/ui/auth.css";
import { useTranslation } from "react-i18next";
import { Checkbox, TextInput, PasswordInput } from "@mantine/core";
import { Button } from "@editor/ui/Button";
import { SignupFieldErrors } from "@editor/routes/signup/SignupFormValidation";
import { privateContentClassName } from "@editor/components/shared/privateContentClassName";

interface SignupFormProps {
  name?: string;
  email: string;
  password: string;
  confirmPassword: string;
  agree?: boolean;
  setName?: (name: string) => void;
  setEmail: (email: string) => void;
  setPassword: (password: string) => void;
  setConfirmPassword: (password: string) => void;
  setAgree?: (agree: boolean) => void;
  onSubmit: () => void;
  isSubmitting: boolean;
  fieldErrors?: SignupFieldErrors;
  showName?: boolean;
  showTerms?: boolean;
}

export default function SignupForm({
  name = "",
  email,
  password,
  confirmPassword,
  agree = true,
  setName,
  setEmail,
  setPassword,
  setConfirmPassword,
  setAgree,
  onSubmit,
  isSubmitting,
  fieldErrors = {},
  showName = false,
  showTerms = false,
}: SignupFormProps) {
  const { t } = useTranslation();
  const showConfirm = password.length >= 4;

  useEffect(() => {
    if (!showConfirm && confirmPassword) {
      setConfirmPassword("");
    }
  }, [showConfirm, confirmPassword, setConfirmPassword]);

  return (
    <>
      <div className="auth-fields">
        {showName && (
          <div className="auth-field">
            <TextInput
              id="name"
              label={t("signup.name")}
              name="name"
              autoComplete="name"
              placeholder={t("signup.enterName")}
              value={name}
              onChange={(e) => setName?.(e.target.value)}
              error={fieldErrors.name}
              classNames={{
                label: "auth-label",
                input: privateContentClassName,
              }}
            />
          </div>
        )}

        <div className="auth-field">
          <TextInput
            id="email"
            label={t("signup.email")}
            type="email"
            name="email"
            autoComplete="email"
            placeholder={t("signup.enterEmail")}
            value={email}
            onChange={(e) => setEmail(e.target.value)}
            onKeyDown={(e) => e.key === "Enter" && !isSubmitting && onSubmit()}
            error={fieldErrors.email}
            classNames={{ label: "auth-label", input: privateContentClassName }}
          />
        </div>

        <div className="auth-field">
          <PasswordInput
            id="password"
            label={t("signup.password")}
            name="new-password"
            autoComplete="new-password"
            placeholder={t("signup.enterPassword")}
            value={password}
            onChange={(e) => setPassword(e.target.value)}
            onKeyDown={(e) => e.key === "Enter" && !isSubmitting && onSubmit()}
            error={fieldErrors.password}
            classNames={{ label: "auth-label", input: privateContentClassName }}
          />
        </div>

        {/* Collapsed to zero height rather than unmounted so it can animate open,
            so `inert` is what keeps the field out of the tab order while
            aria-hidden keeps it off the accessibility tree. */}
        <div
          aria-hidden={!showConfirm}
          inert={!showConfirm}
          className="auth-confirm"
          style={{
            maxHeight: showConfirm ? 96 : 0,
            opacity: showConfirm ? 1 : 0,
          }}
        >
          <div className="auth-field">
            <PasswordInput
              id="confirmPassword"
              label={t("signup.confirmPassword")}
              name="new-password"
              autoComplete="new-password"
              placeholder={t("signup.confirmPasswordPlaceholder")}
              value={confirmPassword}
              onChange={(e) => setConfirmPassword(e.target.value)}
              onKeyDown={(e) =>
                e.key === "Enter" && !isSubmitting && onSubmit()
              }
              error={fieldErrors.confirmPassword}
              classNames={{
                label: "auth-label",
                input: privateContentClassName,
              }}
            />
          </div>
        </div>
      </div>

      {/* Terms - only show if showTerms is true */}
      {showTerms && (
        <div className="auth-terms">
          <Checkbox
            id="agree"
            checked={agree}
            onChange={(e) => setAgree?.(e.currentTarget.checked)}
            className="auth-checkbox"
            label={
              <span className="auth-terms-label">
                {t("legal.iAgreeToThe", "I agree to all of the")}{" "}
                <a
                  href="https://www.stirlingpdf.com/terms"
                  target="_blank"
                  rel="noopener noreferrer"
                >
                  {t("legal.terms", "Terms and Conditions")}
                </a>
              </span>
            }
          />
        </div>
      )}

      {/* Sign Up Button */}
      <Button
        onClick={onSubmit}
        disabled={
          isSubmitting ||
          !email ||
          !password ||
          !confirmPassword ||
          (showTerms && !agree)
        }
        className="auth-submit"
        fullWidth
        size="lg"
        fontSize="sm"
        accent="brand"
        loading={isSubmitting}
      >
        {isSubmitting ? t("signup.creatingAccount") : t("signup.signUp")}
      </Button>
    </>
  );
}