Synertech Web/ui v0.57.5 Site Storybook

Documentation / Forms

NumberInput

Client@synertech/uiOpen in Storybook

The numeric input: it shows the formatted value (thousands separator, currency, percentage) when it is not focused, and the raw value while typing. It filters keystrokes, clamps the value to min / max and hands back a number, never a string.

It is controlled: value is a number | null and onChange receives the parsed number (or null when the field is empty). Formatting goes through Intl.NumberFormat with locale ("en-US" by default): pass locale and currency to match your audience, e.g. locale="fr-FR" and currency="EUR" for a French one. Typing follows the same locale: with fr-FR, the decimal separator is a comma.

Client component ("use client"), with the same presentation options as Input (label, helperText, error, size, icon…).

Examples

Basic usage

The value is a number; null stands for an empty field.

Open in a tab
import { useState } from "react";
import { NumberInput } from "@synertech/ui";

export function Basic() {
  const [quantity, setQuantity] = useState<number | null>(12);

  return (
    <NumberInput
      label="Quantité"
      locale="fr-FR"
      value={quantity}
      onChange={setQuantity}
      min={1}
      max={999}
      helperText="Entre 1 et 999 unités."
    />
  );
}

Display formats

format picks between number, currency, percentage and decimal; decimals sets the precision.

Open in a tab
import { useState } from "react";
import { NumberInput } from "@synertech/ui";

export function Formats() {
  const [amount, setAmount] = useState<number | null>(1250);
  const [rate, setRate] = useState<number | null>(20);
  const [weight, setWeight] = useState<number | null>(3);

  return (
    <div className="flex flex-col gap-4">
      <NumberInput
        label="Montant HT"
        format="currency"
        currency="EUR"
        locale="fr-FR"
        value={amount}
        onChange={setAmount}
      />
      <NumberInput
        label="Taux de TVA"
        format="percentage"
        locale="fr-FR"
        value={rate}
        onChange={setRate}
        max={100}
      />
      <NumberInput
        label="Poids"
        format="decimal"
        decimals={2}
        locale="fr-FR"
        suffix=" kg"
        value={weight}
        onChange={setWeight}
      />
    </div>
  );
}

Prefix, suffix and negatives

prefix / suffix frame the formatted value; allowNegative={false} refuses the minus sign.

Open in a tab
import { useState } from "react";
import { NumberInput } from "@synertech/ui";

export function PrefixSuffix() {
  const [temperature, setTemperature] = useState<number | null>(-4);
  const [stock, setStock] = useState<number | null>(48000);

  return (
    <div className="flex flex-col gap-4">
      <NumberInput
        label="Température"
        locale="fr-FR"
        suffix=" °C"
        value={temperature}
        onChange={setTemperature}
      />
      <NumberInput
        label="Stock disponible"
        locale="fr-FR"
        suffix=" pièces"
        allowNegative={false}
        thousandSeparator
        value={stock}
        onChange={setStock}
      />
    </div>
  );
}

Live calculation

Because onChange returns a number, the value can be used in a calculation directly, with no parsing.

Open in a tab
import { useState } from "react";
import { NumberInput } from "@synertech/ui";

export function LiveTotal() {
  const [unitPrice, setUnitPrice] = useState<number | null>(45);
  const [quantity, setQuantity] = useState<number | null>(3);
  const total = (unitPrice ?? 0) * (quantity ?? 0);

  return (
    <div className="flex flex-col gap-4">
      <NumberInput
        label="Prix unitaire"
        format="currency"
        currency="EUR"
        locale="fr-FR"
        value={unitPrice}
        onChange={setUnitPrice}
        min={0}
      />
      <NumberInput
        label="Quantité"
        locale="fr-FR"
        value={quantity}
        onChange={setQuantity}
        min={0}
      />
      <p className="text-sm text-gray-700 dark:text-gray-200">
        Total : {total.toLocaleString("fr-FR", { style: "currency", currency: "EUR" })}
      </p>
    </div>
  );
}

Error and states

error and helperText flag a value that breaks a rule; readOnly and disabled freeze the field.

Open in a tab
import { useState } from "react";
import { NumberInput } from "@synertech/ui";

export function States() {
  const [budget, setBudget] = useState<number | null>(15000);
  const overBudget = (budget ?? 0) > 10000;

  return (
    <div className="flex flex-col gap-4">
      <NumberInput
        label="Budget demandé"
        format="currency"
        currency="EUR"
        locale="fr-FR"
        value={budget}
        onChange={setBudget}
        error={overBudget}
        helperText={overBudget ? "Au-delà de 10 000 €, une validation est requise." : undefined}
      />
      <NumberInput
        label="Budget validé"
        format="currency"
        currency="EUR"
        locale="fr-FR"
        value={8000}
        readOnly
      />
      <NumberInput label="Remise" format="percentage" locale="fr-FR" value={null} disabled />
    </div>
  );
}

API reference

Components

NumberInput

ComponentClient
import { NumberInput } from "@synertech/ui";

Props — NumberInputProps

PropTypeDefaultDescription
allowNegative boolean true
borderColor string —
currency string "USD"
decimals number —
defaultValue number | null —

The value the field starts with when value is not passed. The field then keeps what is typed in it by itself; onChange still reports it.

error boolean | string —

true marks the field invalid; a message also does, and is written under the field in place of helperText. The field is then aria-invalid and described by the message. false and "" are no error.

format "number" | "currency" | "percentage" | "decimal" "number"
helperText string —

Written under the field; a message passed as error takes its place.

hideLabel boolean false

Keeps the label for screen readers only: it is still the field's name.

icon React.ReactNode —
iconPosition "left" | "right" "left"
inline boolean false
label string —
labelColor string —
locale string —

BCP 47 tag the value is written and read in: its decimal separator, its grouping, its currency. Defaults to the locale of UILabelsProvider, then to en-US.

max number —
min number —
onChange (value: number | null) => void —
prefix string —
size "sm" | "md" | "lg" "md"
step number 1

How much the up and down arrow keys add or take away, within min and max.

suffix string —
textColor string —
thousandSeparator boolean true
value number | null —

+ 301 inherited native attributes from HTMLAttributes, AriaAttributes, DOMAttributes (className, id, aria-*, event handlers…).