From 80d6bb691dd23dc54621b09b357eeee61d4ebbab Mon Sep 17 00:00:00 2001 From: Valtteri Karesto Date: Thu, 2 Jun 2022 15:22:28 +0300 Subject: [PATCH 1/2] Prevent inputing decimals --- components/SetHookDialog.tsx | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/components/SetHookDialog.tsx b/components/SetHookDialog.tsx index 17fb5fb..3fad8f5 100644 --- a/components/SetHookDialog.tsx +++ b/components/SetHookDialog.tsx @@ -121,7 +121,7 @@ export const SetHookDialog: React.FC<{ accountAddress: string }> = React.memo( } const res = await estimateFee(tx, account); if (res && res.base_fee) { - setValue("Fee", res.base_fee); + setValue("Fee", Math.round(Number(res.base_fee || "")).toString()); } })(); } @@ -256,6 +256,12 @@ export const SetHookDialog: React.FC<{ accountAddress: string }> = React.memo( type="number" {...register("Fee", { required: true })} autoComplete={"off"} + onKeyPress={(e) => { + if (e.key === "." || e.key === ",") { + e.preventDefault(); + } + }} + step="1" defaultValue={10000} css={{ "-moz-appearance": "textfield", @@ -295,7 +301,12 @@ export const SetHookDialog: React.FC<{ accountAddress: string }> = React.memo( const res = await estimateFee(tx, account); if (res && res.base_fee) { - setValue("Fee", res.base_fee); + setValue( + "Fee", + Math.round( + Number(res.base_fee || "") + ).toString() + ); } } } catch (err) {} From 92a167d47a419e939b00945be17188b7fbef359f Mon Sep 17 00:00:00 2001 From: Valtteri Karesto Date: Fri, 3 Jun 2022 15:33:46 +0300 Subject: [PATCH 2/2] Prevent also pasting --- components/SetHookDialog.tsx | 10 +++++++++- components/Transaction/ui.tsx | 21 +++++++++++++-------- 2 files changed, 22 insertions(+), 9 deletions(-) diff --git a/components/SetHookDialog.tsx b/components/SetHookDialog.tsx index 3fad8f5..bdaf04c 100644 --- a/components/SetHookDialog.tsx +++ b/components/SetHookDialog.tsx @@ -80,7 +80,7 @@ export const SetHookDialog: React.FC<{ accountAddress: string }> = React.memo( }); const [formInitialized, setFormInitialized] = useState(false); const [estimateLoading, setEstimateLoading] = useState(false); - + const watchedFee = watch("Fee"); // Update value if activeWat changes useEffect(() => { setValue( @@ -89,6 +89,14 @@ export const SetHookDialog: React.FC<{ accountAddress: string }> = React.memo( ); setFormInitialized(true); }, [snap.activeWat, snap.files, setValue]); + useEffect(() => { + if ( + watchedFee && + (watchedFee.includes(".") || watchedFee.includes(",")) + ) { + setValue("Fee", watchedFee.replaceAll(".", "").replaceAll(",", "")); + } + }, [watchedFee, setValue]); // const { // fields: grantFields, // append: grantAppend, diff --git a/components/Transaction/ui.tsx b/components/Transaction/ui.tsx index ee08688..321ddd6 100644 --- a/components/Transaction/ui.tsx +++ b/components/Transaction/ui.tsx @@ -37,22 +37,22 @@ export const TxUI: FC = ({ txFields, } = txState; - const transactionsOptions = transactionsData.map(tx => ({ + const transactionsOptions = transactionsData.map((tx) => ({ value: tx.TransactionType, label: tx.TransactionType, })); - const accountOptions: SelectOption[] = accounts.map(acc => ({ + const accountOptions: SelectOption[] = accounts.map((acc) => ({ label: acc.name, value: acc.address, })); const destAccountOptions: SelectOption[] = accounts - .map(acc => ({ + .map((acc) => ({ label: acc.name, value: acc.address, })) - .filter(acc => acc.value !== selectedAccount?.value); + .filter((acc) => acc.value !== selectedAccount?.value); const [feeLoading, setFeeLoading] = useState(false); @@ -107,7 +107,7 @@ export const TxUI: FC = ({ const specialFields = ["TransactionType", "Account", "Destination"]; const otherFields = Object.keys(txFields).filter( - k => !specialFields.includes(k) + (k) => !specialFields.includes(k) ) as [keyof TxFields]; return ( @@ -190,7 +190,7 @@ export const TxUI: FC = ({ /> )} - {otherFields.map(field => { + {otherFields.map((field) => { let _value = txFields[field]; let value: string | undefined; @@ -223,8 +223,13 @@ export const TxUI: FC = ({ { - handleSetField(field, e.target.value); + onChange={(e) => { + let value = e.target.value; + if (value && (value.includes(".") || value.includes(","))) { + value = value.replaceAll(".", "").replaceAll(",", ""); + } + + handleSetField(field, value); }} css={{ width: "70%", flex: "inherit" }} />