Skip to content

Installation

Install Numora React and render NumoraInput in any React project.

Install

Numora React has one peer dependency, react (17 or later). The core engine, numora, is a regular dependency and installs with it. You don't install numora yourself. Numora React re-exports its helpers, listed under Everything the package exports. Pick your package manager:

pnpm add numora-react

Basic usage

Type a number to see it formatted

import { NumoraInput } from 'numora-react'

function App() {
  return (
    <NumoraInput
      decimalMaxLength={2}
    />
  )
}

Controlled component

Use useState with NumoraInput like any controlled input. Store the raw value, which has no separators. Get started explains raw and formatted values.

import { useState } from 'react'
import { NumoraInput } from 'numora-react'

function AmountField() {
  const [amount, setAmount] = useState('')

  return (
    <>
      <NumoraInput
        value={amount}
        onValueChange={({ value }) => setAmount(value)}
        decimalMaxLength={6}
      />
      <p>Raw value: {amount}</p>
    </>
  )
}

Props

NumoraInput takes every core option as a prop. NumoraInputProps extends NumoraOptions. You can leave any of them out.

Formatting

PropDefaultWhat it does
localeunsetA BCP 47 tag ('de-DE') or true for the runtime's locale. Sets thousandSeparator, decimalSeparator and groupingStyle. If you also pass one of those, yours wins. See Locale.
formatOn'change'Adds separators on every keystroke ('change') or when the field loses focus ('blur'). See Formatting.
groupingStyle'thousand''thousand' (1,234,567), 'lakh' (12,34,567), 'wan' (123,4567) or 'none'. 'none' is the only way to turn grouping off.
thousandSeparator','The character between digit groups. Exactly one character. It cannot be a digit or one of - + e E k K m M b B t T. If you pass only one separator and it equals the other one's default, the other one swaps: thousandSeparator="." alone gives a , decimal. Passing both with the same character is invalid. In development Numora throws on an invalid value. In production it logs the error and keeps the last valid options. See Invalid options.
decimalSeparator'.'The character between the integer and decimal parts. Exactly one character, with the same rules as thousandSeparator. decimalSeparator="," alone gives a . thousand separator.
decimalMaxLength18Maximum number of decimal digits. Numora cuts extra digits off and never rounds. See Decimals.
decimalMinLength0Pads decimals with zeros up to this many digits. Numora pads on blur and when your app sets the value, never while the user types.

What the user can enter

PropDefaultWhat it does
allowNegativefalseAllows a leading -.
allowLeadingZerosfalseKeeps leading zeros (007) instead of removing them. See Leading zeros.
padLeadingZerofalsePuts a 0 in front of a bare decimal separator (.5 becomes 0.5).
allowCompactNotationfalseExpands 1k to 1000. See Compact notation.
allowScientificNotationtrueExpands a pasted or app-set 1.5e-7 to 0.00000015. An e typed at the end of the field is ignored. false rejects such a paste. See Scientific notation.
maxLengthunsetThe most characters the raw value can have. Digits, the decimal separator and a leading - count. Grouping separators and decimalMinLength padding don't. See Max length.
isAllowedunset(rawValue) => boolean. Return false to reject a keystroke or paste. See Validation.

Value and callbacks

PropWhat it does
valueThe controlled value, as a raw string ('1234.5' in every locale), never a number. The field shows this value after every change, so an edit your handler doesn't store is put back. See Value types.
defaultValueThe initial value of an uncontrolled field, as a raw string. Later writes go through ref.current.setValue().
onChangeCalled with a change event when the raw value changes. e.target.value is the raw value and e.target.formattedValue is the field's text.
onValueChangeCalled with { value, formattedValue } alongside onChange. value is the raw value: ASCII digits, a . decimal, no grouping.
onFormattedValueChangeCalled with the field's text every time Numora rewrites it and the text is new. That includes the rewrites onChange and onValueChange say nothing about: grouping on blur, removing separators on focus, setValue(), a new value prop and a changed formatting prop. Not called on mount.

Native attributes

Every other prop goes to the <input>: id, name, className, placeholder, disabled, aria-*, data-* and the rest of the standard input attributes. Numora sets type="text" and inputMode="decimal" itself, so those two are not props. It also sets spellCheck={false} and autoComplete="off" as defaults. Pass either prop to override it.

Your onKeyDown and onPaste run before Numora's. Call e.preventDefault() in one to turn Numora's handling of that event off. Your onBlur runs after Numora's blur settle. By then the field is padded and onChange and onValueChange have fired if the raw value changed.

The ref

The ref points at the <input>, with two read-only properties and one method added:

const ref = useRef<NumoraHTMLInputElement>(null)

ref.current.rawValue        // '1234.5', the value to store or send
ref.current.formattedValue  // '1,234.5', the text in the field

// Write a raw value into an uncontrolled field. Numora sanitizes and formats it,
// and calls neither onChange nor onValueChange. For a controlled field, change `value`.
ref.current.setValue('2500')

Everything the package exports

numora-react has one entry point. It exports the component, the headless helpers from numora, and the types:

import {
  NumoraInput,
  formatValue,              // raw string -> display string, no DOM
  parseValue,               // display string -> raw string, no DOM
  getSeparatorsFromLocale,  // the separators a locale tag gives
  FormatOn,                 // { Blur: 'blur', Change: 'change' }
  GroupingStyle,            // { None, Thousand, Lakh, Wan }
} from 'numora-react'

import type {
  NumoraInputProps,
  NumoraOptions,
  NumoraValues,             // { value, formattedValue }
  LocaleTag,
  NumoraInputChangeEvent,
  NumoraHTMLInputElement,
} from 'numora-react'

Add numora to your own dependencies only to import the numora/pipeline entry point, which Numora React does not re-export.