DDBS UI
commerce

Checkout Form

useCheckoutForm (state, formatting, blur validation, focus the first error), CheckoutField and CheckoutSection.

npx shadcn@latest add https://ui.develop-better-solutions.com/r/store-checkout-form.json
Fullscreen

1.Contact

2.Payment

Usage

store-checkout-form-example.tsx
"use client";

import { useState } from "react";
import {
  CheckoutField,
  CheckoutSection,
  required,
  useCheckoutForm,
  validateCardNumber,
  validateCvc,
  validateEmail,
  validateExpiry,
} from "@dbs/ui";
import { StoreScope } from "./_store";

type Field = "email" | "name" | "card" | "expiry" | "cvc";

const ORDER: readonly Field[] = ["email", "name", "card", "expiry", "cvc"];
const input = (invalid: boolean) =>
  `h-12 w-full rounded-lg border bg-[var(--store-panel)] px-4 text-sm outline-none focus:border-[var(--store-ink)] ${
    invalid ? "border-[var(--store-red)]" : "border-[var(--store-line2)]"
  }`;

export default function StoreCheckoutFormExample() {
  const [done, setDone] = useState(false);
  const form = useCheckoutForm<Field>({
    initial: { email: "", name: "", card: "", expiry: "", cvc: "" },
    order: ORDER,
    check: (name, v) => {
      switch (name) {
        case "email":
          return validateEmail(v.email);
        case "name":
          return required(v.name, "the name on the card");
        case "card":
          return validateCardNumber(v.card);
        case "expiry":
          return validateExpiry(v.expiry);
        case "cvc":
          return validateCvc(v.cvc);
      }
    },
  });
  const props = (name: Field) => ({
    name,
    value: form.values[name],
    error: form.errors[name],
    onChange: form.onChange,
    onBlur: form.onBlur,
    inputClassName: input,
  });
  return (
    <div className="w-full max-w-xl">
      <StoreScope minHeight={0}>
        <form
          noValidate
          onSubmit={(e) => {
            e.preventDefault();
            setDone(form.validateAll());
          }}
          className="flex flex-col gap-6"
        >
          <CheckoutSection title="Contact" step={1}>
            <CheckoutField {...props("email")} label="Email" type="email" autoComplete="email" />
          </CheckoutSection>
          <CheckoutSection title="Payment" step={2}>
            <div className="grid gap-4 sm:grid-cols-2">
              <CheckoutField {...props("name")} label="Name on card" className="sm:col-span-2" />
              <CheckoutField {...props("card")} label="Card number" inputMode="numeric" placeholder="4242 4242 4242 4242" className="sm:col-span-2" />
              <CheckoutField {...props("expiry")} label="Expiry" placeholder="MM/YY" />
              <CheckoutField {...props("cvc")} label="CVC" inputMode="numeric" maxLength={4} />
            </div>
          </CheckoutSection>
          <button
            type="submit"
            className="h-12 rounded-full bg-[var(--store-ink)] px-8 text-sm font-medium text-[var(--store-on-ink)]"
          >
            Pay now
          </button>
          {done && <p role="status" className="text-sm text-[var(--store-soft)]">Valid. The order would be placed now.</p>}
        </form>
      </StoreScope>
    </div>
  );
}