Number Input
Numeric editing with blank drafts, blur/submit commits, bounded decimal steps and increment/decrement controls.
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 number-inputSource files
Expand a file to read the source the installer copies into your app.
components/ui/number-input/NumberInput.tsx
import * as React from 'react'
import { Platform, TextInput, View, type ViewProps } from 'react-native'
import { Minus, Plus } from 'lucide-react-native'
import { Input, type InputProps } from '@/components/ui/input'
import { IconButton } from '@/components/ui/icon-button'
import { cn } from '@/lib/utils'
export interface NumberInputProps extends Omit<
InputProps,
'value' | 'defaultValue' | 'onChangeText' | 'prefixIcon' | 'suffixIcon' | 'style'
> {
value?: number | null
defaultValue?: number | null
/** Commits on blur, submit, or a step action. Blank commits null; incomplete signs restore the previous value. */
onValueChange?: (value: number | null) => void
min?: number
max?: number
step?: number
disabled?: boolean
decrementLabel?: string
incrementLabel?: string
inputClassName?: string
style?: ViewProps['style']
}
function decimalPlaces(value: number) {
const [coefficient, exponent = '0'] = value.toString().split('e')
return Math.max(0, (coefficient.split('.')[1]?.length ?? 0) - Number(exponent))
}
export const NumberInput = React.forwardRef<React.ComponentRef<typeof TextInput>, NumberInputProps>(
(
{
value,
defaultValue = null,
onValueChange,
min,
max,
step = 1,
disabled = false,
editable = true,
readOnly,
className,
inputClassName,
style,
decrementLabel = 'Decrease value',
incrementLabel = 'Increase value',
keyboardType,
onBlur,
onSubmitEditing,
...props
},
ref,
) => {
if (!Number.isFinite(step) || step <= 0)
throw new RangeError('NumberInput step must be finite and greater than zero')
if (
(min !== undefined && !Number.isFinite(min)) ||
(max !== undefined && !Number.isFinite(max)) ||
(min !== undefined && max !== undefined && min > max)
)
throw new RangeError('NumberInput bounds must be finite and min must not exceed max')
if ((value != null && !Number.isFinite(value)) || (defaultValue != null && !Number.isFinite(defaultValue)))
throw new RangeError('NumberInput values must be finite numbers or null')
const clamp = (number: number) => Math.min(max ?? Infinity, Math.max(min ?? -Infinity, number))
const [internal, setInternal] = React.useState(defaultValue)
const resolved = value === undefined ? internal : value
const [draft, setDraft] = React.useState(() => (resolved == null ? '' : String(resolved)))
const submittedDraft = React.useRef<string | undefined>(undefined)
const canEdit = editable && !disabled && !readOnly
React.useEffect(() => {
setDraft(resolved == null ? '' : String(resolved))
}, [resolved])
function parseDraft() {
if (draft.trim() === '') return null
const parsed = Number(draft.replace(',', '.'))
return Number.isFinite(parsed) ? parsed : undefined
}
function commit(next: number | null) {
if (!canEdit) return
if (value === undefined) setInternal(next)
if (!Object.is(next, resolved)) onValueChange?.(next)
const accepted = value === undefined ? next : value
setDraft(accepted == null ? '' : String(accepted))
}
function commitDraft() {
const parsed = parseDraft()
if (parsed === undefined) {
setDraft(resolved == null ? '' : String(resolved))
return
}
commit(parsed === null ? null : clamp(parsed))
}
function adjust(direction: 1 | -1) {
if (!canEdit) return
submittedDraft.current = undefined
const parsed = parseDraft()
const current = parsed === undefined ? resolved : parsed
if (current == null) {
commit(clamp(direction > 0 ? (min ?? step) : (max ?? -step)))
return
}
const sum = current + direction * step
if (!Number.isFinite(sum)) return
const precision = Math.max(decimalPlaces(current), decimalPlaces(step))
commit(clamp(precision <= 100 ? Number(sum.toFixed(precision)) : sum))
}
const parsed = parseDraft()
const current = parsed === undefined ? resolved : parsed
return (
<View style={style} className={cn('flex-row items-center gap-[8px]', className)}>
<IconButton
icon={Minus}
variant="outline"
accessibilityLabel={decrementLabel}
disabled={!canEdit || (current != null && min !== undefined && current <= min)}
onPress={() => adjust(-1)}
/>
<View className="min-w-0 flex-1">
<Input
{...props}
ref={ref}
editable={canEdit}
readOnly={readOnly}
value={draft}
className={cn('text-center', inputClassName)}
keyboardType={
keyboardType ??
(Platform.OS === 'ios' && (min === undefined || min < 0) ? 'numbers-and-punctuation' : 'numeric')
}
onChangeText={(next) => {
if (canEdit && /^[+-]?\d*(?:[.,]\d*)?$/.test(next)) {
submittedDraft.current = undefined
setDraft(next)
}
}}
onBlur={(event) => {
if (submittedDraft.current !== draft) commitDraft()
submittedDraft.current = undefined
onBlur?.(event)
}}
onSubmitEditing={(event) => {
submittedDraft.current = draft
commitDraft()
onSubmitEditing?.(event)
}}
/>
</View>
<IconButton
icon={Plus}
variant="outline"
accessibilityLabel={incrementLabel}
disabled={!canEdit || (current != null && max !== undefined && current >= max)}
onPress={() => adjust(1)}
/>
</View>
)
},
)
NumberInput.displayName = 'NumberInput'
components/ui/number-input/index.ts
export { NumberInput } from './NumberInput'
export type { NumberInputProps } from './NumberInput'
Dependencies
Registry items
npm packages
lucide-react-native