Quantity Stepper
Keyboard-free numeric stepper with bounded whole-number values and native adjustable actions.
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 quantity-stepperSource files
Expand a file to read the source the installer copies into your app.
components/ui/quantity-stepper/QuantityStepper.tsx
import * as React from 'react'
import { Platform, View, type ViewProps } from 'react-native'
import { Minus, Plus } from 'lucide-react-native'
import { IconButton } from '@/components/ui/icon-button'
import { Text } from '@/components/ui/text'
import { cn } from '@/lib/utils'
const numberFormatter = new Intl.NumberFormat()
export interface QuantityStepperProps extends Omit<ViewProps, 'children'> {
className?: string
value?: number
defaultValue?: number
/** Inclusive, non-negative integer bounds. */
min?: number
max?: number
disabled?: boolean
onValueChange?: (value: number) => void
decrementLabel?: string
incrementLabel?: string
formatValue?: (value: number) => string
}
/** Bounded, whole-number adjustment for touch and assistive technology. */
export const QuantityStepper = React.forwardRef<React.ComponentRef<typeof View>, QuantityStepperProps>(
(
{
className,
value,
defaultValue,
min = 0,
max = 99,
disabled = false,
onValueChange,
decrementLabel = 'Decrease quantity',
incrementLabel = 'Increase quantity',
formatValue,
accessibilityLabel = 'Quantity',
accessibilityState,
onAccessibilityAction,
...props
},
ref,
) => {
const minimum = Number.isSafeInteger(min) ? Math.max(0, min) : 0
const maximum = Math.max(minimum, Number.isSafeInteger(max) ? max : 99)
const [internal, setInternal] = React.useState(defaultValue ?? minimum)
const internalRef = React.useRef(internal)
const clamp = (candidate: number) =>
Number.isFinite(candidate) ? Math.min(maximum, Math.max(minimum, Math.round(candidate))) : minimum
const count = clamp(value ?? internal)
const countText = formatValue ? formatValue(count) : numberFormatter.format(count)
const canDecrease = !disabled && count > minimum
const canIncrease = !disabled && count < maximum
const nativeAdjustable = Platform.OS !== 'web'
function adjust(direction: -1 | 1) {
if (disabled) return
const current = clamp(value ?? internalRef.current)
const next = clamp(current + direction)
if (next === current) return
if (value === undefined) {
internalRef.current = next
setInternal(next)
}
onValueChange?.(next)
}
return (
<View
ref={ref}
{...props}
className={cn('flex-row items-center gap-[8px]', className)}
accessible={nativeAdjustable || undefined}
accessibilityRole={nativeAdjustable ? 'adjustable' : undefined}
accessibilityLabel={nativeAdjustable ? accessibilityLabel : undefined}
accessibilityState={{ ...accessibilityState, disabled: disabled || minimum === maximum }}
accessibilityValue={
nativeAdjustable
? {
min: minimum,
max: maximum,
now: count,
// VoiceOver otherwise speaks a percentage. TalkBack uses the numeric range to announce changes.
...(Platform.OS === 'ios' ? { text: countText } : {}),
}
: undefined
}
accessibilityActions={
nativeAdjustable
? [
...(canDecrease ? [{ name: 'decrement', label: decrementLabel }] : []),
...(canIncrease ? [{ name: 'increment', label: incrementLabel }] : []),
]
: undefined
}
onAccessibilityAction={(event) => {
if (event.nativeEvent.actionName === 'decrement') adjust(-1)
if (event.nativeEvent.actionName === 'increment') adjust(1)
onAccessibilityAction?.(event)
}}
>
<IconButton
icon={Minus}
variant="outline"
accessibilityLabel={decrementLabel}
aria-hidden={nativeAdjustable || undefined}
disabled={!canDecrease}
onPress={() => adjust(-1)}
/>
<Text
aria-hidden={nativeAdjustable || undefined}
accessibilityLiveRegion={nativeAdjustable ? undefined : 'polite'}
className="min-w-[40px] shrink text-center font-semibold"
style={{ fontVariant: ['tabular-nums'] }}
>
{countText}
</Text>
<IconButton
icon={Plus}
variant="outline"
accessibilityLabel={incrementLabel}
aria-hidden={nativeAdjustable || undefined}
disabled={!canIncrease}
onPress={() => adjust(1)}
/>
</View>
)
},
)
QuantityStepper.displayName = 'QuantityStepper'
components/ui/quantity-stepper/index.ts
export { QuantityStepper, type QuantityStepperProps } from './QuantityStepper'
Dependencies
Registry items
npm packages
lucide-react-native