Skip to content
العربية
App patterns

Keypad

Digit keys on a touch screen for an amount or a code, so the system keyboard does not cover a dialog. value is the draft text of the field it serves (Western digits and “.”, as MoneyInput) and onValueChange gets the next draft. mode="amount" has 1 to 9, “.”, 0, “00” and a backspace key; a leading zero is replaced by the next digit, a second “.” is refused, and decimals (default 2) is the most digits after it (0 hides the “.” key). mode="pin" has 1 to 9, 0 and backspace. At maxLength the digit keys do nothing. The keys are buttons with names in both languages and stay left to right in Arabic, like a phone. A hardware keyboard works too (digits, “.” and Backspace) while the focus is on the page or inside the keypad, and never when a text field, dialog or menu has the focus, on a key repeat, or with Ctrl, Meta or Alt. Use it on a coarse pointer only; a desktop uses the keyboard. PinPad (a masked code with checking) uses the same key grid.

import { Keypad } from "@percentsa/ui";

Examples

Due: 230

Still due: 230

Code
export function KeypadExample() {
  const t = useT();
  const [draft, setDraft] = useState("");
  const due = 230;
  const paid = parseAmount(draft) ?? 0;
  return (
    <Stack gap={4} className="w-full max-w-xs">
      <MoneyInput aria-label={t("Cash received", "النقد المستلم")} value={draft} onValueChange={setDraft} placeholder="0.00" />
      <Text size="sm" tone="muted">
        {t("Due", "المطلوب")}: <Money amount={due} />
      </Text>
      <Text size="sm" tone={paid >= due ? "default" : "danger"} aria-live="polite">
        {paid >= due ? t("Change", "الباقي") : t("Still due", "متبقٍّ على العميل")}: <Money amount={Math.abs(due - paid)} />
      </Text>
      <Keypad label={t("Amount keypad", "لوحة المبلغ")} value={draft} onValueChange={setDraft} maxLength={9} />
    </Stack>
  );
}

/** The same keypad as a PIN pad (no point, no 00), for a code that the app checks itself. */

0 of 4 digits

Code
export function KeypadPinExample() {
  const t = useT();
  const [pin, setPin] = useState("");
  return (
    <Stack gap={3} className="w-full max-w-xs">
      <Text size="sm" tone="muted" aria-live="polite">
        {t(`${pin.length} of 4 digits`, `${pin.length} من 4 أرقام`)}
      </Text>
      <Keypad label={t("PIN keypad", "لوحة الرمز")} mode="pin" maxLength={4} value={pin} onValueChange={setPin} />
    </Stack>
  );
}

/** A work tool fills the screen: the shell is as high as its box, the page box takes the rest and each region scrolls by itself. */