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-reactBasic usage
Type a number to see it formatted
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.
Props
NumoraInput takes every core option as a prop. NumoraInputProps extends NumoraOptions. You can leave any of them out.
Formatting
| Prop | Default | What it does |
|---|---|---|
locale | unset | A 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. |
decimalMaxLength | 18 | Maximum number of decimal digits. Numora cuts extra digits off and never rounds. See Decimals. |
decimalMinLength | 0 | Pads 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
| Prop | Default | What it does |
|---|---|---|
allowNegative | false | Allows a leading -. |
allowLeadingZeros | false | Keeps leading zeros (007) instead of removing them. See Leading zeros. |
padLeadingZero | false | Puts a 0 in front of a bare decimal separator (.5 becomes 0.5). |
allowCompactNotation | false | Expands 1k to 1000. See Compact notation. |
allowScientificNotation | true | Expands 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. |
maxLength | unset | The 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. |
isAllowed | unset | (rawValue) => boolean. Return false to reject a keystroke or paste. See Validation. |
Value and callbacks
| Prop | What it does |
|---|---|
value | The 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. |
defaultValue | The initial value of an uncontrolled field, as a raw string. Later writes go through ref.current.setValue(). |
onChange | Called 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. |
onValueChange | Called with { value, formattedValue } alongside onChange. value is the raw value: ASCII digits, a . decimal, no grouping. |
onFormattedValueChange | Called 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:
Everything the package exports
numora-react has one entry point. It exports the component, the headless helpers from numora, and the types:
Add numora to your own dependencies only to import the numora/pipeline entry point, which Numora React does not re-export.