A React Native TextInput that formats and displays only numeric inputs, including i18n currency formatting. Using the native number-pad keyboard type, this component adds or removes a number from the right side of the total, and gives options for the number of decimal places to use or whether to display locale-appropriate nuber grouping.
npm install @wwdrew/react-native-numeric-textinput
Android currently does not implement the Intl object so a polyfill
is required. At the moment it only includes the en locale.
constExample=()=>{const[value,setValue]=useState()return(<View><NumericInputtype='decimal'decimalPlaces={3}value={value}onUpdate={(value)=>setValue(value)}/><Text>Decimal Value: {value}</Text></View>)}The only required prop is the onUpdate callback, the rest are optional.
onUpdate: (value) => void (Required) - called when the value of the TextInput changes.value: Number - if not provided, will default to0.type: String - eitherdecimalorcurrency. If not provided, will default todecimal. If type iscurrency, you'll also need to supply acurrencyprop.decimalPlaces: Number - the number of decimal places to display. Only valid fordecimaltype.currency: String - the ISO 4217 currency code of the currency to display. Defaults toGBP.locale: String - a BCP 78 language tag specifying the locale used for number formatting. Defaults toen-GB.useGrouping: Boolean - whether to use grouping separators. Defaults totrue.
<NumericInputtype='currency'locale='ja-JP'currency='JPY'value={value}onUpdate={(value)=>setValue(value)}/><NumericInputtype='decimal'decimalPlaces={1}useGrouping={false}value={value}onUpdate={(value)=>setValue(value)}/>MIT