Install Numora
Add the Numora TypeScript numeric input library to any project. No framework required.
Install
Numora has zero runtime dependencies. Pick your package manager:
pnpm add numoraBasic usage
Import the NumoraInput class and pass it a container element:
Options
The second argument is an optional options object (NumoraInputOptions). It extends NumoraOptions with the callbacks, defaultValue and a few native attributes. You can leave any of them out.
Formatting
| Option | 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. Pass it alone and, if it lands on the decimal separator, the decimal separator switches to the other of . and ,. Pass both separators as the same character and Numora throws. Every error is in the invalid options table. |
decimalSeparator | '.' | The character between the integer and decimal parts. Exactly one character, with the same rules as thousandSeparator. |
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
| Option | 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. A typed e is never accepted. 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
| Option | What it does |
|---|---|
defaultValue | The initial value, as a raw value ('1234.5' in every locale). Later writes go through setValue(). Numora ignores a value option. See Value types. |
onValueChange | Called with { value, formattedValue } when the raw value changes. value is the raw value: ASCII digits, a . decimal, no grouping. An update() that changes the raw value calls it too. |
onFormattedValueChange | Called with the field's text every time Numora rewrites it and the text is new. That includes the rewrites onValueChange says nothing about: grouping on blur, removing separators on focus, setValue(), and an update() that changes only the format. Not called on creation. |
Native attributes
Numora copies these onto the <input>: id, name, className, placeholder, disabled, readOnly, required, tabIndex, title, autofocus and ariaLabel, which sets aria-label. Set anything else on numoraInput.element.
Don't set the native maxLength on the element. It counts grouping separators, and the Numora option doesn't.
The instance
Pass a container and Numora creates an <input> inside it. Pass an existing <input> and Numora adopts it. The instance has three read-only properties and three methods:
update() follows four rules:
- A separator you passed yourself wins over a
localeset later. A field created withthousandSeparator: ','anddecimalSeparator: '.'still shows2,500afterupdate({ locale: 'de-DE' }). - A key set to
undefinedgoes back to its default:update({ locale: undefined })removes the locale. - If the merged options are invalid,
update()throws. The field keeps its old options and its value. - Numora calls
onValueChangeonly if the raw value changed, andonFormattedValueChangeonly if the text changed. A new locale changes the text and not the raw value. A lowerdecimalMaxLengthcan change both.
Everything the package exports
numora has three entry points.
numoraexports theNumoraInputclass and three functions that need no input element:formatValue,parseValueandgetSeparatorsFromLocale. It exports two constants,GroupingStyleandFormatOn, which hold the option values:GroupingStyle.Lakhis'lakh'. It exports the typesNumoraOptions,NumoraInputOptions,NumoraValuesandLocaleTag.numora/pipelineexports each sanitization step as its own function. It has no semver guarantee: a step can change or go in a minor release. See Sanitization.numora/internalholds code shared withnumora-react. It has no semver guarantee either. Don't import from it.
TypeScript
Numora ships its own declarations. Import types directly: