Currency Input
A currency-labeled decimal field that emits whole minor units and preserves invalid text for correction.
Install
First install? Set up the registry in your app.
npx --yes --package=https://native.uipkge.dev/cli/uipkge-native-0.1.1.tgz uipkge-native add currency-inputSource files
Expand a file to read the source the installer copies into your app.
components/ui/currency-input/CurrencyInput.tsx
import * as React from 'react'
import { TextInput, View } from 'react-native'
import { Input, type InputProps } from '@/components/ui/input'
import { Text } from '@/components/ui/text'
export interface CurrencyInputProps extends Omit<
InputProps,
'defaultValue' | 'onChangeText' | 'prefixIcon' | 'suffixIcon' | 'multiline'
> {
label: string
/** An ISO 4217 currency code passed to Intl.NumberFormat. */
currency: string
locale?: string
defaultValue?: string
/** Receives each draft and its exact non-negative minor units, or null for empty, incomplete, or invalid text. */
onValueChange?: (text: string, minorUnits: number | null) => void
invalidMessage?: string
}
function amountFormat(locale: string | undefined, currency: string) {
const format = new Intl.NumberFormat(locale, { style: 'currency', currency, useGrouping: false })
const { currency: code = currency, maximumFractionDigits: precision = 2 } = format.resolvedOptions()
const digitsFormat = new Intl.NumberFormat(locale, { useGrouping: false })
// Some native Hermes runtimes provide format but not formatToParts.
const one = digitsFormat.format(1)
const decimal = digitsFormat.format(1.1).slice(one.length, -one.length) || '.'
const digits = new Map(Array.from({ length: 10 }, (_, index) => [digitsFormat.format(index), String(index)]))
return { code, precision, decimal, digits }
}
function parseAmount(text: string, { precision, decimal, digits }: ReturnType<typeof amountFormat>) {
const trimmed = text.trim()
if (!trimmed) return { minorUnits: null, invalid: false }
const normalized = Array.from(trimmed, (char) => digits.get(char) ?? char).join('')
const [whole, fraction, ...extra] = normalized.split(decimal)
if (
extra.length ||
!/^\d*$/.test(whole) ||
(fraction !== undefined && (precision === 0 || !/^\d*$/.test(fraction) || fraction.length > precision))
) {
return { minorUnits: null, invalid: true }
}
if (fraction === '') return { minorUnits: null, invalid: false }
const amount = Number((whole || '0') + (fraction ?? '').padEnd(precision, '0'))
return Number.isSafeInteger(amount) ? { minorUnits: amount, invalid: false } : { minorUnits: null, invalid: true }
}
export const CurrencyInput = React.forwardRef<React.ComponentRef<typeof TextInput>, CurrencyInputProps>(
(
{
label,
currency,
locale,
value,
defaultValue = '',
onValueChange,
accessibilityLabel = label,
accessibilityHint,
inputMode,
keyboardType,
editable = true,
readOnly,
invalid,
invalidMessage,
onBlur,
...props
},
ref,
) => {
const [draft, setDraft] = React.useState(defaultValue)
const [blurred, setBlurred] = React.useState(false)
const format = React.useMemo(() => amountFormat(locale, currency), [locale, currency])
const text = value ?? draft
const result = parseAmount(text, format)
const hasError = result.invalid || (blurred && text.trim() !== '' && result.minorUnits === null)
const formatMessage =
format.precision === 0
? 'Enter a whole amount using digits only.'
: `Enter up to ${format.precision} decimal places using โ${format.decimal}โ without grouping separators.`
const error = (hasError || invalid) && (invalidMessage ?? (hasError ? formatMessage : undefined))
const canEdit = editable && !readOnly
return (
<View className="gap-2">
<Text size="sm" className="font-medium" aria-hidden>
{label}
</Text>
<Input
{...props}
ref={ref}
value={text}
editable={canEdit}
readOnly={readOnly}
inputMode={inputMode ?? (keyboardType ? undefined : format.precision === 0 ? 'numeric' : 'decimal')}
keyboardType={keyboardType}
autoCorrect={false}
invalid={invalid || hasError}
shakeOnInvalid={false}
accessibilityLabel={`${accessibilityLabel}, ${format.code}`}
accessibilityHint={[accessibilityHint, error].filter(Boolean).join(' ') || undefined}
suffixIcon={
<Text size="sm" variant="muted" aria-hidden>
{format.code}
</Text>
}
onChangeText={(next) => {
if (!canEdit) return
setBlurred(false)
if (value === undefined) setDraft(next)
onValueChange?.(next, parseAmount(next, format).minorUnits)
}}
onBlur={(event) => {
setBlurred(true)
onBlur?.(event)
}}
/>
{error ? (
<Text size="sm" className="text-destructive" accessibilityLiveRegion="polite">
{error}
</Text>
) : null}
</View>
)
},
)
CurrencyInput.displayName = 'CurrencyInput'
components/ui/currency-input/index.ts
export { CurrencyInput, type CurrencyInputProps } from './CurrencyInput'