Skip to main content

React Number Format

Solution for onChange without reset input's selectionIndex

import { NumericFormat, PatternFormat } from 'react-number-format'

// NumbericFormat -> currency
// PatternFormat -> card numbers & phone

Common Props

PropsValueDesc
getInputRefrefcontrol input's ref
defaultValuestring | number
type"text" | "tel" | "pasword"
valuestring | number
displayType'input' | 'text'OPTIONAL
onValueChange({ value, floatValue, formattedValue }, { event, source: "prop" | "event" }) => void
``
``
``

NumericFormat

PropsValueDesc
isAllowed({ value, floatValue, formattedValue }) => booleanValidate input value before change (stop if false)
allowedNegativeboolean
allowedDecimalSeparatorsstring[]OPTIONAL: Press key in list turn to separator character
decimalScalenumberlimits the number of digits after decimal point
decimalSeparatorstring
fixedDecimalScalebooleanExp: decimalScale={3} turn 1.1 -> 1.100
customInputComponentExp: Form.Control of react-bootstrap
prefixstringcharacter start
subfixstringend character
thousandsSeparatorstring
thousandsGroupStyle'thousand' | 'lakh' (India) | 'wan' (China)

PatternFormat

PropsValueDesc
formatstringExp: "+1 (###) ###-####", value will fit all #
patternCharstringExp: format="%% (%%%)" patternChar="%"
valueIsNumericStringbooleanInput number only
allowEmptyFormattingbooleanfit empty space with mask character
maskstring | string[]