Command palette

@yasinsensoy/web

Web · Components

NumberInput

Input that accepts only numbers and formats them in the locale's format while typing.

3 exports

Live example

Preview

₺

Try typing, pasting ($1,234.56) and the ↑/↓ keys.

Code

import { NumberField } from "@yasinsensoy/web";

<NumberField label="Amount" name="amount" decimals={2} suffix="₺" placeholder="0,00"
  onValueChange={setAmount} />

Events

  1. Events appear here when you interact with the preview.

Quantity picker: integer, 1–99, speeds up when held

Price: two fixed decimals, no minus

₺

Percentage: show an error instead of clamping

%

Controlled usage

$

Value: 2500

Languages: same value, different separators

tr-TR
en-US
de-DE
fr-FR
en-IN

Import

import { NumberInput, parseNumber } from "@yasinsensoy/web";

NumberInput

componentClient component

Input that accepts only numbers and formats them in the locale's format while typing. Looks the same as Input; gives the value as a number.

  • Format: Thousands separators are added automatically while typing (tr-TR: 1.234.567,89); the caret stays in place. Deleting over a separator deletes the digit next to it.
  • Numbers only: Letters and other symbols can't be typed. Both , and . keys work for the decimal. The minus sign can be typed anywhere and toggles the sign (it can't be typed if min is 0 or more).
  • Pasting: Texts like ₺1.234,56, 1,234.56, 1 234,5, (1.234) are converted to the right number; currency symbols and spaces are dropped. See parseNumber.
  • Keyboard: ↑/↓ change by step, 10 times with Shift or PageUp/PageDown. Enter and blur clamp the value to the bounds and complete the format (12, → 12).
  • Mobile: A number keyboard opens (without decimals if decimals={0}). The iOS number keyboard has no minus key; if negative values are needed use stepper or inputMode="text".
  • Form: With name, the value enters FormData without separators, with a dot ("1234.56"); read it on the server with Number(). required, min, max and step are checked by browser validation; it returns to the initial value when the form is reset.

For use with a label, description and error message, see NumberField.

NumberInput({ value, defaultValue, onValueChange, min, max, step, decimals, fixedDecimals, grouping, clamp, stepper, locale: localeProp, icon, iconEnd, prefix, suffix, id, name, form, required, disabled, readOnly, className, ref, inputMode, onKeyDown, onBlur, onPaste, variant, effect, ...props }: NumberInputProps): JSX.Element

Examples

<NumberInput name="quantity" defaultValue={1} min={1} max={99} decimals={0} stepper aria-label="Quantity" />

<NumberInput name="amount" decimals={2} fixedDecimals min={0} suffix="₺" aria-label="Amount" />

<NumberInput value={rate} onValueChange={setRate} min={0} max={100} step={0.5} suffix="%" />

parseNumber

function

Parses number text the user typed or pasted. Currencies, spaces and letters are dropped; non-Latin digits are understood.

The decimal separator is found like this:

  • If the text has both . and ,, the last one is the decimal (1.234,56, 1,234.56).
  • If a single kind of separator appears more than once, it is the group separator (1.000.000).
  • If it appears once: it is the decimal if it is the locale's decimal separator; otherwise it is the group separator when exactly three digits follow (1.234 → 1234 in tr-TR), else the decimal (12.5 → 12.5).
parseNumber(text: string, locale?: string): number | null

Examples

parseNumber("₺1.234,56") // 1234.56
parseNumber("1,234.56") // 1234.56
parseNumber("12.5") // 12.5
parseNumber("1.234") // 1234
parseNumber("1.234", "en-US") // 1.234
parseNumber("abc") // null

Props of the NumberInput component.

Extends: Omit<ComponentProps<"input">, "value" | "defaultValue" | "onChange" | "type" | "min" | "max" | "step" | "children" | "prefix">

NameTypeDefaultDescription
variant?FieldVariant | CustomVariant<"field">Appearance: outline (bordered), filled, underline or ghost (plain). If omitted, the setting of the nearest data-field-variant container is used, or outline if there is none. See FieldVariant.
effect?EffectPropEffect or effects of the field box (e.g. "glow", ["lift", "beam"]). The accent color is the focus color (--ui-field-focus). If omitted, UIProvider appearance.defaults.field.effect. See BuiltinEffect.
value?number | nullValue when controlled; null for an empty field.
defaultValue?number | nullInitial value when uncontrolled.
onValueChange?(value: number | null) => voidCalled when the value changes: on every key while typing, on the step buttons and on blur (when clamped to the bounds). null if the field is empty.
min?numberMinimum value. If it is 0 or more, a minus sign can't be typed.
max?numberMaximum value.
step?number1Amount changed by the ↑/↓ keys and step buttons. 10 times with Shift or PageUp/PageDown. If given, browser validation also requires the value to match this step (stepMismatch).
decimals?numberMaximum decimal places. If 0, only integers can be typed and on mobile a digit keyboard without a decimal key opens. If omitted, up to 10 decimal places are accepted.
fixedDecimals?booleanfalseAlways show decimals with decimals places (12 → 12,00). For money amounts.
grouping?booleantrueShow the thousands separator (1.234.567)?
clamp?booleantrueClamp a value outside min/max to the bound on blur? If false, the value is left and browser validation gives a rangeUnderflow / rangeOverflow error.
stepper?booleanfalseShow − and + buttons at the end? Holding repeats with acceleration.
locale?stringLocale of the separators (BCP 47). @defaultValue UIProvider's locale ("tr-TR")
icon?IconPropIcon at the start of the field: a name ("money") or an element.
iconEnd?IconPropIcon at the end of the field: a name or an element.
prefix?ReactNodeShort text before the number (e.g. "₺", "$"). Not part of the value or FormData.
suffix?ReactNodeUnit after the number (e.g. "kg", "%", "TL"). Not part of the value or FormData.