card field.md

Card Field

A one-row card input for checkout. The number groups the way the brand prints it, then slides to its last four to make room for expiry and CVC.

Installation

npx shadcn@latest add @kokonutui/card-field

Props

Prop Type Default Description
amount? number 48 What the button charges.
currency? string "$"
onPay? (card: CardDetails) => Promise<void> | void Charge the card. Return a promise and the button waits for it.
testMode? boolean true Shows a "Use test card" link that types 4242… in for you.
className? string Extra classes for the root element.

CardDetails

Field Type Description
brand Brand
last4 string
expiry string MM/YY

Brand

type Brand = "visa" | "mastercard" | "amex" | "discover" | "unknown";

Source

"use client";

/**
 * @author: @dorianbaffier
 * @description: Card Field
 * @version: 1.0.0
 * @date: 2026-10-22
 * @license: MIT
 * @website: https://kokonutui.com
 * @github: https://github.com/kokonut-labs/kokonutui
 */

//... source code continues...

export interface CardFieldProps {
  /** What the button charges. */
  amount?: number;
  currency?: string;
  /** Charge the card. Return a promise and the button waits for it. */
  onPay?: (card: CardDetails) => Promise<void> | void;
  /** Shows a "Use test card" link that types 4242… in for you. */
  testMode?: boolean;
  className?: string;
}

//... more source code...

const pay = async () => {
        if (!ready || status !== "idle") {
            return;
        }
        setStatus("paying");
        const details = {
            brand: spec.brand,
            last4: number.slice(-4),
            expiry: `${expiry.slice(0, 2)}/${expiry.slice(2)}`,
        };
        try {
            await (onPay?.(details) ??
                new Promise((resolve) => setTimeout(resolve, 1100)));
        } catch {
            // A declined charge leaves the card as typed, ready to try again.
            setStatus("idle");
            return;
        }
        setStatus("paid");
        clearTimeout(resetTimer.current);
        resetTimer.current = setTimeout(() => {
            setNumber("");
            setExpiry("");
            setCvc("");
            setEditingNumber(true);
            setStatus("idle");
        }, 2400);
    };
//... final implementation of the component