Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Limit input in Bridge and Send to 4 decimals #616

Merged
merged 2 commits into from
Apr 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 11 additions & 10 deletions wallet/src/components/BridgeComponent/index.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useContext, useState, useCallback, useEffect } from 'react';
import React, { useContext, useState, useEffect } from 'react';
import Modal from 'react-bootstrap/Modal';
import { MdArrowForwardIos } from 'react-icons/md';
import { toast } from 'react-toastify';
Expand Down Expand Up @@ -54,7 +54,7 @@ const BridgeComponent = () => {

const [token, setToken] = useState(initialToken);
const [txType, setTxType] = useState(initialTx);
const [transferValue, setTransferValue] = useState(0);
const [transferValue, setTransferValue] = useState('0');
const [show, setShow] = useState(false);

const [showTokensListModal, setShowTokensListModal] = useState(false);
Expand Down Expand Up @@ -212,13 +212,6 @@ const BridgeComponent = () => {
return false;
}

const handleChange = useCallback(
e => {
setTransferValue(e.target.value);
},
[transferValue],
);

const handleShow = () => {
if (
(txType === 'deposit' && transferValue > l1Balance) ||
Expand Down Expand Up @@ -338,7 +331,15 @@ const BridgeComponent = () => {
name="price"
prefix="$"
placeholder="0,00"
onChange={handleChange}
onKeyDown={e => {
if (
(transferValue.toString().split('.')[1]?.length ?? 0) > 3 &&
/^[0-9]$/i.test(e.key)
) {
e.preventDefault(); // If exceed input count then stop updates.
}
}}
onChange={e => setTransferValue(e.target.value)}
/>
<div className="amount_details_max" onClick={() => updateInputValue()}>
<span>MAX</span>
Expand Down
2 changes: 1 addition & 1 deletion wallet/src/components/Input/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ interface InputProps extends InputHTMLAttributes<HTMLInputElement> {
prefix?: string;
}

const Input: React.FC<InputProps> = ({ mask, prefix, ...props }) => {
const Input: React.FC<InputProps> = ({ mask, ...props }) => {
const handleKeyUp = useCallback(
(e: React.FormEvent<HTMLInputElement>) => {
if (mask === 'cep') {
Expand Down
6 changes: 3 additions & 3 deletions wallet/src/components/Input/masks.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from 'react';

export function cep(e: React.FormEvent<HTMLInputElement>) {
export function cep(e: React.FormEvent<HTMLInputElement>): React.FormEvent<HTMLInputElement> {
e.currentTarget.maxLength = 9;
let { value } = e.currentTarget;
value = value.replace(/\D/g, '');
Expand All @@ -9,7 +9,7 @@ export function cep(e: React.FormEvent<HTMLInputElement>) {
return e;
}

export function currency(e: React.FormEvent<HTMLInputElement>) {
export function currency(e: React.FormEvent<HTMLInputElement>): React.FormEvent<HTMLInputElement> {
let { value } = e.currentTarget;
value = value.replace(/\D/g, '');
value = value.replace(/(\d)(\d{2})$/, '$1,$2');
Expand All @@ -19,7 +19,7 @@ export function currency(e: React.FormEvent<HTMLInputElement>) {
return e;
}

export function cpf(e: React.FormEvent<HTMLInputElement>) {
export function cpf(e: React.FormEvent<HTMLInputElement>): React.FormEvent<HTMLInputElement> {
e.currentTarget.maxLength = 14;
let { value } = e.currentTarget;
if (!value.match(/^(\d{3}).(\d{3}).(\d{3})-(\d{2})$/)) {
Expand Down
12 changes: 10 additions & 2 deletions wallet/src/components/Modals/sendModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ const SendModal = (props: SendModalProps): JSX.Element => {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
const [state] = useContext(UserContext); // Why does typescript think this is an object?
const [valueToSend, setTransferValue] = useState(0);
const [valueToSend, setTransferValue] = useState('0');
const [recipient, setRecipient] = useState('');
const { onHide, show, ...initialSendToken } = props;
const [sendToken, setSendToken] = useState(initialSendToken);
Expand Down Expand Up @@ -236,7 +236,15 @@ const SendModal = (props: SendModalProps): JSX.Element => {
<input
type="text"
placeholder="0.00"
onChange={e => setTransferValue(Number(e.target.value))}
onKeyDown={e => {
if (
(valueToSend.toString().split('.')[1]?.length ?? 0) > 3 &&
/^[0-9]$/i.test(e.key)
) {
e.preventDefault(); // If exceed input count then stop updates.
}
}}
onChange={e => setTransferValue(e.target.value)}
id="TokenItem_modalSend_tokenAmount"
/>
<div className={stylesModal.maxButton}>MAX</div>
Expand Down
5 changes: 3 additions & 2 deletions wallet/src/hooks/Account/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ const AccountContext = createContext<AccountContextType>({} as AccountContextTyp
* context use
* @param children
*/
const AccountProvider = ({ children }: any) => {
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
const AccountProvider = ({ children }: any): JSX.Element => {
const [accountInstance, setAccountInstance] = useState<AccountType>(accountType);

return (
Expand All @@ -38,7 +39,7 @@ const AccountProvider = ({ children }: any) => {
/**
* Hook to allow the context call
*/
const useAccount = () => {
const useAccount = (): AccountContextType => {
const context = useContext(AccountContext);

if (!context) {
Expand Down