Open History to see the last commit
1import { useGT } from "gt-react-native";
2import { View } from "react-native";
3
4import { AppText } from "./AppText";
5import { IconButton } from "./IconButton";
6import type { NumberFieldProps } from "./NumberField.types";
7
8export function NumberField(props: NumberFieldProps) {
9 const gt = useGT();
10 const adjust = (amount: number) => {
11 if (props.disabled) return;
12 const next = Math.max(props.min, Math.min(props.max, props.value + amount));
13 if (next !== props.value) props.onValueChange(next);
14 };
15
16 return (
17 <View className="shrink-0 flex-row items-center">
18 <View
19 pointerEvents="none"
20 className="absolute inset-x-0 rounded-full bg-subtle"
21 style={{ height: 32 }}
22 />
23 <IconButton
24 icon="minus"
25 accessibilityLabel={gt`Decrease ${{ label: props.label }}`}
26 disabled={props.disabled || props.value <= props.min}
27 onPress={() => adjust(-1)}
28 />
29 <AppText
30 textRole="body"
31 className="min-w-8 text-center"
32 style={{ fontVariant: ["tabular-nums"] }}
33 accessibilityLabel={gt`${{ label: props.label }}: ${{ value: props.formatValue?.(props.value) ?? props.value }}`}
34 accessibilityLiveRegion="polite"
35 >
36 {props.value}
37 </AppText>
38 <IconButton
39 icon="plus"
40 accessibilityLabel={gt`Increase ${{ label: props.label }}`}
41 disabled={props.disabled || props.value >= props.max}
42 onPress={() => adjust(1)}
43 />
44 </View>
45 );
46}