Skip to content

Commit

Permalink
fix: do not allow more than one decimal point
Browse files Browse the repository at this point in the history
  • Loading branch information
im-adithya committed Oct 29, 2024
1 parent af17fb9 commit 62c3596
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
13 changes: 12 additions & 1 deletion 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 { 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
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 62c3596

Please sign in to comment.