Command palette

@yasinsensoy/native

React Native · Components

NumberInput

A field that accepts only numbers and is formatted in the language's style while typing (the same rules as NumberInput/NumberField on web, @yasinsensoy/core/number-format).

3 exports

Live example

The preview runs in the browser with the web component; the native component takes the same props and looks the same on iOS and Android.

Preview

₺

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

Code

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

<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/native";

NumberInput

component

A field that accepts only numbers and is formatted in the language's style while typing (the same rules as NumberInput/NumberField on web, @yasinsensoy/core/number-format).

  • Thousands separators are added while typing (1.234.567,89 in Turkish); both , and . work for decimals; a pasted ₺1.234,56 turns into the right number.
  • On blur the value is clamped to the limits and the format is completed.
  • stepper adds − / + buttons (a 44 px touch area, accelerating when held).
NumberInput(props: NumberInputProps): ReactElement<unknown, string | JSXElementConstructor<any>>

Examples

<NumberInput label="Quantity" defaultValue={1} min={1} max={99} decimals={0} stepper />
<NumberInput label="Amount" decimals={2} fixedDecimals min={0} 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 NumberInput.

Extends: FieldProps, Omit<TextInputProps, "value" | "defaultValue" | "onChangeText" | "editable" | "style" | "keyboardType">

NameTypeDefaultDescription
ref?Ref<TextInputRef>Ref of the underlying native element.
variant?FieldVariant"outline"Appearance. See FieldVariant.
value?number | nullValue (controlled); null for an empty field.
defaultValue?number | nullInitial value (uncontrolled).
onValueChange?(value: number | null) => voidWhen the value changes; null for an empty field.
min?numberMinimum value. At 0 or above the minus sign cannot be typed.
max?numberMaximum value.
step?number1Amount the step buttons change.
decimals?numberMaximum decimal places; 0 allows integers only (keyboard without decimals).
fixedDecimals?booleanfalseAlways show decimals with decimals places (12 → 12.00).
grouping?booleantrueThousands separator (1,234,567).
clamp?booleantrueOn blur, clamp a value outside min/max to the limit.
stepper?booleanfalse− and + buttons; holding repeats with acceleration.
locale?string{@link UIProvider} `localeLanguage of the separators.
icon?IconPropIcon at the start of the box.
iconEnd?IconPropIcon at the end of the box.
prefix?ReactNodeShort text before the number (e.g. "$"). Not part of the value.
suffix?ReactNodeUnit after the number (e.g. "kg", "%"). Not part of the value.
readOnly?booleanRead-only.
style?StyleProp<ViewStyle>Style of the box.