Skip to Content

Quantity Field

Quantity stepper input: minus and plus buttons around a numeric field that accepts only in-range whole numbers, grouped as one input. Read-only locks the buttons; with a name it submits through a hidden input in cart and checkout forms.

Installation

pnpm dlx shadcn@latest add @01orbital/quantity-field

Usage

"use client"

import { useState } from "react"

import { QuantityField } from "@/components/store/quantity-field"

// The copy: change text here, not in the markup.
const COPY = {
  name: "Ink Rings",
  detail: "A2, 42 × 59 cm",
  left: "left",
  decrease: "Decrease quantity",
  increase: "Increase quantity",
  quantity: "Quantity",
} as const

// Knobs: the print's price in dollars and how many are in stock.
const PRICE = 72
const STOCK = 5

const IMAGE = {
  url: "https://01orbital.dev/art/kit/thomas-v2-596378.png",
  alt: "Print of concentric rings of heavy black dashes on white.",
}

const MONEY = new Intl.NumberFormat("en-US", {
  style: "currency",
  currency: "USD",
  maximumFractionDigits: 0,
})

// One cart line: the stepper stops at the stock left, and the line total
// follows the quantity.
export default function QuantityFieldDemo() {
  const [quantity, setQuantity] = useState(1)
  return (
    <div className="flex w-full max-w-md items-center gap-4 p-6">
      <img
        src={IMAGE.url}
        alt={IMAGE.alt}
        className="aspect-4/5 w-16 shrink-0 bg-muted object-cover"
      />
      <div className="grid min-w-0 flex-1 gap-0.5">
        <p className="text-sm/5 font-medium">{COPY.name}</p>
        <p className="text-sm/5 text-muted-foreground">{COPY.detail}</p>
        <p className="text-sm/5 text-muted-foreground tabular-nums">
          {MONEY.format(PRICE)} · {STOCK} {COPY.left}
        </p>
      </div>
      <div className="grid justify-items-end gap-2">
        <QuantityField
          aria-label={COPY.quantity}
          value={quantity}
          onValueChange={setQuantity}
          max={STOCK}
          decrementLabel={COPY.decrease}
          incrementLabel={COPY.increase}
        />
        <p aria-live="polite" className="text-sm/5 tabular-nums">
          {MONEY.format(PRICE * quantity)}
        </p>
      </div>
    </div>
  )
}

This example installs with everything it uses:

pnpm dlx shadcn@latest add @01orbital/quantity-field-demo

API reference

A stepper, never a select: minus, a typed number, plus. Typing keeps a draft; only an in-range number is applied, and leaving the field or pressing Enter restores it. With a name, a form submits the applied quantity, never the draft.

Prop

Type

It also accepts the HTML attributes of the element it renders.

Dependencies

npm: @phosphor-icons/react, cn. The CLI installs them for you.

Requires these pieces. The CLI adds each one that is missing and won't replace a file you already have unless you choose to:

The shadcn parts here are their Base UI versions, and this piece uses Base UI's API. A Radix version you already have won't work with it.

Used in blocks

5 blocks are built with Quantity Field:

Last updated on