-
Notifications
You must be signed in to change notification settings - Fork 54
Expand file tree
/
Copy pathamount.tsx
More file actions
171 lines (163 loc) · 4.33 KB
/
amount.tsx
File metadata and controls
171 lines (163 loc) · 4.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import { Button } from "@/components/button";
import { NumericKeyboard } from "@/components/numeric-keyboard";
import { ThemedText } from "@/components/themed-text";
import { BorderRadius, Spacing } from "@/constants/spacing";
import { useTheme } from "@/hooks/use-theme-color";
import { router } from "expo-router";
import { Controller, useForm } from "react-hook-form";
import { StyleSheet, View } from "react-native";
interface FormData {
amount: string;
}
const formatAmount = (amount: string) => {
if (!amount.includes(".")) {
return `${amount}.00`;
}
const [whole, decimal] = amount.split(".");
if (decimal.length === 0) {
return `${whole}.00`;
} else if (decimal.length === 1) {
return `${whole}.${decimal}0`;
}
const trimmedDecimal = decimal.replace(/0+$/, "");
const paddedDecimal =
trimmedDecimal.length >= 2 ? trimmedDecimal : trimmedDecimal.padEnd(2, "0");
return `${whole}.${paddedDecimal}`;
};
export default function AmountScreen() {
const Theme = useTheme();
const {
control,
handleSubmit,
watch,
formState: { isValid },
} = useForm<FormData>({
defaultValues: {
amount: "",
},
});
const watchAmount = watch("amount");
const onSubmit = ({ amount }: FormData) => {
const formattedAmount = formatAmount(amount);
router.push({
pathname: "/scan",
params: {
amount: formattedAmount,
},
});
};
return (
<View style={styles.container}>
<View
style={[
styles.amountContainer,
{ borderColor: Theme["border-primary"] },
]}
>
<ThemedText
numberOfLines={1}
style={[
styles.amountText,
{
color:
watchAmount === ""
? Theme["text-secondary"]
: Theme["text-primary"],
},
]}
>
${watchAmount || "0.00"}
</ThemedText>
</View>
<Controller
control={control}
name="amount"
rules={{
validate: (value) => {
if (
!value ||
value === "0" ||
value === "" ||
Number(value) === 0
) {
return "Amount is required";
}
return true;
},
}}
render={({ field: { onChange, value: prev } }) => (
<NumericKeyboard
onKeyPress={(key) => {
let newDisplay;
if (key === "erase") {
newDisplay = prev?.slice(0, -1) || "";
onChange?.(newDisplay);
} else if (key === ".") {
if (prev.includes(".")) return; // Don't add multiple commas
if (prev === "") {
newDisplay = "0.";
} else {
newDisplay = prev + ".";
}
onChange?.(newDisplay);
} else {
const newDisplay = prev === "0" ? key : prev + key;
onChange?.(newDisplay);
}
}}
/>
)}
/>
<Button
testID="charge-button"
onPress={handleSubmit(onSubmit)}
disabled={!isValid}
style={[
styles.button,
{
backgroundColor: Theme["bg-accent-primary"],
opacity: isValid ? 1 : 0.6,
},
]}
>
<ThemedText
fontSize={18}
lineHeight={20}
style={{ color: Theme["text-invert"] }}
>
{isValid ? `Charge $${formatAmount(watchAmount)}` : "Enter amount"}
</ThemedText>
</Button>
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: "space-between",
alignItems: "center",
paddingHorizontal: Spacing["spacing-5"],
paddingVertical: Spacing["spacing-5"],
},
amountContainer: {
flex: 1,
width: "100%",
alignItems: "center",
justifyContent: "center",
paddingTop: Spacing["spacing-4"],
paddingHorizontal: Spacing["spacing-5"],
},
amountText: {
fontSize: 50,
textAlign: "center",
lineHeight: 50,
},
button: {
width: "100%",
marginTop: Spacing["spacing-6"],
paddingVertical: Spacing["spacing-4"],
paddingHorizontal: Spacing["spacing-5"],
alignItems: "center",
borderRadius: BorderRadius["5"],
},
});