|
| 1 | +import { forwardRef, ElementRef, useState, useEffect } from 'react' |
| 2 | +import type { NativeSyntheticEvent, TextInputKeyPressEventData } from 'react-native' |
| 3 | +import { TextInput } from './TextInput.styled' |
| 4 | +import { z, schema } from '@green-stack/schemas' |
| 5 | +import { cn, View, Pressable } from '../components/styled' |
| 6 | +import { AddFilled } from '../icons/AddFilled' |
| 7 | +import { RemoveFilled } from '../icons/RemoveFilled' |
| 8 | + |
| 9 | +/* --- Schema ---------------------------------------------------------------------------------- */ |
| 10 | + |
| 11 | +export const NumberStepperProps = schema('NumberStepperProps', { |
| 12 | + value: z.number().default(0), |
| 13 | + min: z.number().default(0), |
| 14 | + max: z.number().optional(), |
| 15 | + step: z.number().default(1), |
| 16 | + placeholder: z.string().optional().example('Enter number...'), |
| 17 | + className: z.string().optional(), |
| 18 | + placeholderClassName: z.string().optional(), |
| 19 | + placeholderTextColor: z.string().optional(), |
| 20 | + hasError: z.boolean().default(false), |
| 21 | + readOnly: z.boolean().default(false), |
| 22 | + disabled: z.boolean().default(false), |
| 23 | +}) |
| 24 | + |
| 25 | +type NumberStepperProps = z.input<typeof NumberStepperProps> & { |
| 26 | + onChange: (value: number) => void |
| 27 | +} |
| 28 | + |
| 29 | +/* --- <NumberStepper/> ------------------------------------------------------------------------ */ |
| 30 | + |
| 31 | +export const NumberStepper = forwardRef< |
| 32 | + ElementRef<typeof TextInput>, |
| 33 | + NumberStepperProps |
| 34 | +>((rawProps, ref) => { |
| 35 | + // Props |
| 36 | + const props = NumberStepperProps.applyDefaults(rawProps) |
| 37 | + const { min, max, step, onChange, ...restProps } = props |
| 38 | + |
| 39 | + // State |
| 40 | + const [value, setValue] = useState(props.value) |
| 41 | + |
| 42 | + // Helpers |
| 43 | + const constrainValue = (value: number) => Math.min(Math.max(value, min), max || Infinity) |
| 44 | + |
| 45 | + // Vars |
| 46 | + const numberValue = constrainValue(value) |
| 47 | + |
| 48 | + // Flags |
| 49 | + const hasMinValue = typeof rawProps.min !== undefined |
| 50 | + const hasMaxValue = typeof rawProps.max !== undefined |
| 51 | + const hasReachedMin = hasMinValue && numberValue === min |
| 52 | + const hasReachedMax = hasMaxValue && numberValue === max |
| 53 | + |
| 54 | + // -- Handlers -- |
| 55 | + |
| 56 | + const onIncrement = () => setValue(constrainValue(numberValue + step)) |
| 57 | + |
| 58 | + const onDecrement = () => setValue(constrainValue(numberValue - step)) |
| 59 | + |
| 60 | + const onKeyPress = ({ nativeEvent }: NativeSyntheticEvent<TextInputKeyPressEventData>) => { |
| 61 | + const isUpKey = nativeEvent.key === 'ArrowUp' |
| 62 | + const isDownKey = nativeEvent.key === 'ArrowDown' |
| 63 | + if (isUpKey) return onIncrement() |
| 64 | + if (isDownKey) return onDecrement() |
| 65 | + } |
| 66 | + |
| 67 | + // -- Effects -- |
| 68 | + |
| 69 | + useEffect(() => { |
| 70 | + if (value) onChange(value) |
| 71 | + }, [value]) |
| 72 | + |
| 73 | + // -- Render -- |
| 74 | + |
| 75 | + return ( |
| 76 | + <View |
| 77 | + className={cn( |
| 78 | + 'h-10 native:h-12', |
| 79 | + 'web:flex web:w-full', |
| 80 | + 'web:max-w-[200px]', |
| 81 | + )} |
| 82 | + > |
| 83 | + <Pressable |
| 84 | + className={cn( |
| 85 | + "absolute top-0 left-0 items-center justify-center select-none z-10", |
| 86 | + "w-10 h-10 native:w-12 native:h-12", |
| 87 | + "border-r border-r-input", |
| 88 | + hasReachedMin && 'opacity-50', |
| 89 | + props.hasError && 'border-r-error', |
| 90 | + )} |
| 91 | + onPress={onDecrement} |
| 92 | + disabled={hasReachedMin} |
| 93 | + hitSlop={10} |
| 94 | + > |
| 95 | + <RemoveFilled size={20} /> |
| 96 | + </Pressable> |
| 97 | + <TextInput |
| 98 | + ref={ref} |
| 99 | + inputMode="numeric" |
| 100 | + {...restProps} |
| 101 | + className={cn( |
| 102 | + 'text-center', |
| 103 | + 'px-10 native:px-12', |
| 104 | + 'web:max-w-[200px]', |
| 105 | + props.className, |
| 106 | + )} |
| 107 | + onKeyPress={onKeyPress} |
| 108 | + value={value ? `${value}` : ''} |
| 109 | + onChangeText={(newValue = '') => { |
| 110 | + // Strip non-numeric characters |
| 111 | + const strippedValue = newValue.replace(/[^0-9]/g, '') |
| 112 | + // If empty, show placeholder |
| 113 | + if (!strippedValue) setValue(undefined as unknown as number) |
| 114 | + // Convert to number |
| 115 | + const newNumberValue = +strippedValue |
| 116 | + // @ts-ignore |
| 117 | + setValue(newNumberValue) |
| 118 | + }} |
| 119 | + /> |
| 120 | + <Pressable |
| 121 | + className={cn( |
| 122 | + "absolute top-0 right-0 items-center justify-center select-none z-10", |
| 123 | + "w-10 h-10 native:w-12 native:h-12", |
| 124 | + "border-l border-l-input", |
| 125 | + hasReachedMax && 'opacity-50', |
| 126 | + props.hasError && 'border-l-error', |
| 127 | + )} |
| 128 | + onPress={onIncrement} |
| 129 | + disabled={hasReachedMax} |
| 130 | + hitSlop={10} |
| 131 | + > |
| 132 | + <AddFilled size={20} /> |
| 133 | + </Pressable> |
| 134 | + </View> |
| 135 | + ) |
| 136 | +}) |
| 137 | + |
| 138 | + |
0 commit comments