Skip to content

Value types

Numora React works with strings. onValueChange gives you { value, formattedValue }, and onChange gives you the same two strings on an event, for form libraries. In both, value (e.target.value) is the canonical raw value: ASCII digits, . as the decimal mark, - as the sign and no grouping, in every locale and with any decimalSeparator. The formatted display string is in formattedValue (e.target.formattedValue). You get both on every change. There is no mode to choose.

The raw value format

The raw value is the one to store and do math on. It has one format, and the locale and the separators do not change it:

  • ASCII digits 0 to 9, even if the user types Arabic-Indic or fullwidth digits
  • . as the decimal mark, even if the field shows ,
  • a leading - for a negative number, which needs allowNegative
  • no thousand separators and no spaces
PropsThe field showsRaw value
none1,234,567.89"1234567.89"
locale="de-DE"1.234.567,89"1234567.89"
locale="fr-FR"1 234 567,89"1234567.89"
locale="en-IN"12,34,567.89"1234567.89"
locale="ar-EG"1٬234٬567٫89"1234567.89"

The raw value is what the user has typed so far, so it is not always a complete number. It can be "" (empty field), "-", ".", "1." or ".5". Check for these before you parse it. Numora keeps trailing zeros, so 0.50 stays "0.50". With decimalMinLength it pads on blur and when your app sets the value, and the padding is part of the raw value, so 5 becomes "5.00". A blur that pads the value or strips a trailing separator changes the raw value, so it calls onChange and onValueChange. On blur "-", "." and "-." reset to "", and "-0" becomes "0".

The value and defaultValue props take this same format, so the value you get from onValueChange can go straight back into value.

onValueChange

Use onValueChange in your own components. It has the same shape as the vanilla package's callback.

import { NumoraInput } from 'numora-react'

<NumoraInput
  onValueChange={({ value, formattedValue }) => {
    console.log(value)          // "1234.56" (raw)
    console.log(formattedValue) // "1,234.56" (formatted)
  }}
/>

e.target.value and e.target.formattedValue

Use onChange where a library expects a change event, such as React Hook Form's field.onChange. It fires a NumoraInputChangeEvent (structurally compatible with React's ChangeEvent<HTMLInputElement>). e.target.value is the raw numeric string, so you can pass it to a form library or state setter without post-processing. e.target.formattedValue is the string the input shows.

import { NumoraInput } from 'numora-react'

<NumoraInput
  onChange={(e) => {
    console.log(e.target.value)          // "1234.56" (raw)
    console.log(e.target.formattedValue) // "1,234.56" (formatted)
  }}
/>

Type below. Both readouts update on every keystroke:

e.target.formattedValue
"1,234,567.89"
e.target.value
"1234567.89"

Reading values via ref

The ref is the <input> element with two extra read-only properties and one method. rawValue is the raw value and formattedValue is the display string. setValue(value) writes a value from code. It fires onFormattedValueChange only, and skips onChange, onValueChange and isAllowed. ref.current.value is the DOM value, so it is the display string too. Only e.target.value inside onChange is raw. Type the ref as NumoraHTMLInputElement and no cast is needed.

import { useRef } from 'react'
import { NumoraInput, type NumoraHTMLInputElement } from 'numora-react'

function App() {
  const ref = useRef<NumoraHTMLInputElement>(null)

  return (
    <NumoraInput
      ref={ref}
      onChange={() => {
        console.log(ref.current?.rawValue)       // "1234.56"
        console.log(ref.current?.formattedValue) // "1,234.56"
      }}
    />
  )
}

Native form submission

A plain <form> submit or new FormData(form) reads the input's DOM value, which is the display string ("1,234.56", or "1.234,56" in de-DE). To submit the raw value, leave the visible input without a name and mirror the raw value into a hidden input. Or read the posted string back with parseValue(formData.get('amount'), options), using the field's options:

const [amount, setAmount] = useState('')

<form action="/pay">
  <NumoraInput value={amount} onValueChange={({ value }) => setAmount(value)} />
  <input type="hidden" name="amount" value={amount} />
</form>

Parsing the value

Numora React does not provide a numeric value such as valueAsNumber. The raw value is canonical in every locale (. decimal, no grouping), so parse value yourself with a decimal library:

import Decimal from 'decimal.js'
import { NumoraInput } from 'numora-react'

<NumoraInput
  onValueChange={({ value: raw }) => {
    // skip intermediate states such as "", "-" or "."
    if (!/^-?\d+(\.\d+)?$/.test(raw)) return
    const amount = new Decimal(raw) // exact, no float rounding
    console.log(amount.toString())
  }}
/>

Avoid Number() and parseFloat. They are subject to IEEE 754 limits, so integers above Number.MAX_SAFE_INTEGER and long decimals lose precision with no error.

The value and defaultValue props

value and defaultValue take a string. Pass the canonical raw value: ASCII digits, . as the decimal mark, no grouping, in every locale. value="1234.5" under locale="de-DE" displays 1.234,5. A number is a type error, so convert it yourself with String(n) or pass the string your decimal library gives you.