From 16b2ed3bc29a428e0e72af7698bdac5cbdf18bdc Mon Sep 17 00:00:00 2001 From: Adw8 Date: Wed, 21 Feb 2024 17:49:44 +0530 Subject: [PATCH] Combines states --- components/HDPath.tsx | 38 +++++++++++++++++++++++++------------- types.ts | 16 +++++++++++----- 2 files changed, 36 insertions(+), 18 deletions(-) diff --git a/components/HDPath.tsx b/components/HDPath.tsx index e8ea58a..34f89a3 100644 --- a/components/HDPath.tsx +++ b/components/HDPath.tsx @@ -3,8 +3,7 @@ import { ScrollView, View, Text, StyleSheet } from 'react-native'; import { Button, TextInput } from 'react-native-paper'; import { addAccountFromHDPath } from '../utils/Accounts'; -import { Account } from '../types'; -import { HDNode } from 'ethers/lib/utils'; +import { Account, PathState } from '../types'; const HDPath = ({ pathCode, @@ -18,13 +17,26 @@ const HDPath = ({ hideDialog: () => void; }) => { const [isAccountCreating, setIsAccountCreating] = useState(false); - const [firstNumber, setFirstNumber] = useState(''); - const [secondNumber, setSecondNumber] = useState(''); - const [thirdNumber, setThirdNumber] = useState(''); + const [path, setPath] = useState({ + firstNumber: '', + secondNumber: '', + thirdNumber: '', + }); + + const handleChange = (key: keyof PathState, value: string) => { + setPath({ + ...path, + [key]: value.replace(/[^0-9]/g, ''), // Allow only numeric characters + }); + }; const createFromHDPathHandler = async () => { setIsAccountCreating(true); - const hdPath = pathCode + `${firstNumber}'/${secondNumber}/${thirdNumber}`; + + const hdPath = + pathCode + + `${path.firstNumber}'/${path.secondNumber}/${path.thirdNumber}`; + try { const newAccount = await addAccountFromHDPath(hdPath); if (newAccount) { @@ -46,8 +58,8 @@ const HDPath = ({ handleChange('firstNumber', text)} + value={path.firstNumber} style={styles.textInput} maxLength={1} /> @@ -55,17 +67,17 @@ const HDPath = ({ handleChange('secondNumber', text)} + value={path.secondNumber} style={styles.textInput} maxLength={1} /> - '/ + / handleChange('thirdNumber', text)} + value={path.thirdNumber} style={styles.textInput} maxLength={1} /> diff --git a/types.ts b/types.ts index a21f152..46f7fe1 100644 --- a/types.ts +++ b/types.ts @@ -2,11 +2,11 @@ export type StackParamsList = { Laconic: undefined; SignMessage: { selectedNetwork: string; accountInfo: Account } | undefined; HDPath: - | { - updateIndex: (index: number) => void; - updateAccounts: (account: Account) => void; - } - | undefined; + | { + updateIndex: (index: number) => void; + updateAccounts: (account: Account) => void; + } + | undefined; }; export type Account = { @@ -71,3 +71,9 @@ export type HDPathDialogProps = { export type GridViewProps = { words: string[]; }; + +export type PathState = { + firstNumber: string; + secondNumber: string; + thirdNumber: string; +};