Skip to content

Commit

Permalink
fix: validate input numbers (#170)
Browse files Browse the repository at this point in the history
* fix: do not allow more than one decimal point

* fix: use touchable instead of pressable

---------

Co-authored-by: René Aaron <[email protected]>
  • Loading branch information
im-adithya and reneaaron authored Oct 30, 2024
1 parent 87e4431 commit 3ae4502
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
19 changes: 15 additions & 4 deletions components/DualCurrencyInput.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import React from "react";
import { Pressable, StyleSheet, View } from "react-native";
import { StyleSheet, TouchableOpacity, View } from "react-native";
import { useGetFiatAmount, useGetSatsAmount } from "~/hooks/useGetFiatAmount";
import { CURSOR_COLOR, DEFAULT_CURRENCY } from "~/lib/constants";
import {
CURSOR_COLOR,
DEFAULT_CURRENCY,
FIAT_REGEX,
SATS_REGEX,
} from "~/lib/constants";
import { useAppStore } from "~/lib/state/appStore";
import { RefreshCw } from "./Icons";
import { Input } from "./ui/input";
Expand Down Expand Up @@ -29,8 +34,14 @@ export function DualCurrencyInput({

function onChangeText(text: string) {
if (inputMode === "sats") {
if (!SATS_REGEX.test(text)) {
return;
}
setAmount(text);
} else {
if (!FIAT_REGEX.test(text)) {
return;
}
setFiatAmount(text);
if (getSatsAmount) {
setAmount(getSatsAmount(+text)?.toString() || "");
Expand Down Expand Up @@ -63,14 +74,14 @@ export function DualCurrencyInput({
readOnly={readOnly}
// aria-errormessage="inputError"
/>
<Pressable onPress={toggleInputMode}>
<TouchableOpacity onPress={toggleInputMode}>
<View className="flex flex-row gap-2 items-center justify-center">
<Text className="font-semibold2 text-2xl text-muted-foreground">
{inputMode === "fiat" ? fiatCurrency : "sats"}
</Text>
<RefreshCw className="text-muted-foreground" width={16} height={16} />
</View>
</Pressable>
</TouchableOpacity>
{
<Text className="text-muted-foreground text-2xl font-semibold2">
{inputMode === "fiat"
Expand Down
4 changes: 4 additions & 0 deletions lib/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,7 @@ export const REQUIRED_CAPABILITIES: Nip47Capability[] = [
"pay_invoice",
"list_transactions",
];

export const SATS_REGEX = /^\d*$/;

export const FIAT_REGEX = /^\d*(\.\d{0,2})?$/;

0 comments on commit 3ae4502

Please sign in to comment.