mirror of
https://github.com/kremalicious/blog.git
synced 2024-11-22 01:46:51 +01:00
handle number validation
This commit is contained in:
parent
83e50e74c3
commit
021321f0d9
@ -12,7 +12,7 @@ export function Conversion(): ReactElement {
|
||||
const [euro, setEuro] = useState('0.00')
|
||||
|
||||
useEffect(() => {
|
||||
if (!selectedToken?.price || !amount) {
|
||||
if (!selectedToken?.price || !amount || amount === '') {
|
||||
setDollar('0.00')
|
||||
setEuro('0.00')
|
||||
return
|
||||
|
@ -3,7 +3,12 @@ import { useAccount } from 'wagmi'
|
||||
import { InputGroup } from '../Input'
|
||||
import styles from './Form.module.css'
|
||||
import { useStore } from '@nanostores/react'
|
||||
import { $selectedToken, $isInitSend, $amount } from '@features/Web3/stores'
|
||||
import {
|
||||
$selectedToken,
|
||||
$isInitSend,
|
||||
$amount,
|
||||
$setAmount
|
||||
} from '@features/Web3/stores'
|
||||
import siteConfig from '@config/blog.config'
|
||||
import { Send } from '../Send'
|
||||
import { RainbowKit } from '../RainbowKit/RainbowKit'
|
||||
@ -18,6 +23,7 @@ export function Web3Form(): ReactElement {
|
||||
|
||||
const [error, setError] = useState<string>()
|
||||
|
||||
// Error Validation
|
||||
useEffect(() => {
|
||||
if (!amount || amount === '' || !selectedToken?.balance) {
|
||||
setError(undefined)
|
||||
@ -34,7 +40,7 @@ export function Web3Form(): ReactElement {
|
||||
// reset amount whenever token changes
|
||||
useEffect(() => {
|
||||
if (!selectedToken) return
|
||||
$amount.set('')
|
||||
$setAmount('')
|
||||
}, [selectedToken])
|
||||
|
||||
return (
|
||||
|
@ -3,7 +3,12 @@ import Input from '@components/Input'
|
||||
import { Conversion } from '../Conversion'
|
||||
import styles from './InputGroup.module.css'
|
||||
import { TokenSelect } from '../TokenSelect'
|
||||
import { $amount, $isInitSend, $selectedToken } from '@features/Web3/stores'
|
||||
import {
|
||||
$amount,
|
||||
$setAmount,
|
||||
$isInitSend,
|
||||
$selectedToken
|
||||
} from '@features/Web3/stores'
|
||||
import { useStore } from '@nanostores/react'
|
||||
|
||||
export function InputGroup({
|
||||
@ -19,7 +24,7 @@ export function InputGroup({
|
||||
const [isFocus, setIsFocus] = useState(false)
|
||||
|
||||
function handleChange(newAmount: string) {
|
||||
$amount.set(newAmount)
|
||||
$setAmount(newAmount)
|
||||
}
|
||||
|
||||
return (
|
||||
@ -36,6 +41,7 @@ export function InputGroup({
|
||||
type="text"
|
||||
inputMode="decimal"
|
||||
pattern="[0-9.]*"
|
||||
lang="en-US"
|
||||
value={amount}
|
||||
placeholder="0.00"
|
||||
onChange={(e) => handleChange(e.target.value)}
|
||||
|
@ -11,9 +11,10 @@ interface SelectItemProps extends HTMLAttributes<HTMLDivElement> {
|
||||
|
||||
export const Token = forwardRef<HTMLDivElement, SelectItemProps>(
|
||||
({ className, token, ...props }, forwardedRef) => {
|
||||
const locale = window?.navigator?.language || 'en'
|
||||
const balance =
|
||||
token?.balance && token?.symbol
|
||||
? formatCurrency(token.balance, token.symbol, 'en', false, {
|
||||
? formatCurrency(token.balance, token.symbol, locale, false, {
|
||||
decimalPlaces: 3,
|
||||
significantFigures: 3
|
||||
})
|
||||
@ -23,7 +24,7 @@ export const Token = forwardRef<HTMLDivElement, SelectItemProps>(
|
||||
token?.balance && token?.price?.usd
|
||||
? token?.balance * token?.price.usd
|
||||
: 0
|
||||
const valueInUsdFormatted = formatCurrency(valueInUsd, 'USD', 'en')
|
||||
const valueInUsdFormatted = formatCurrency(valueInUsd, 'USD', locale)
|
||||
|
||||
return balance && parseInt(balance) !== 0 && valueInUsd >= 1 ? (
|
||||
<Select.Item
|
||||
|
1
src/features/Web3/lib/normalizeAmount/index.ts
Normal file
1
src/features/Web3/lib/normalizeAmount/index.ts
Normal file
@ -0,0 +1 @@
|
||||
export * from './normalizeAmount'
|
@ -0,0 +1,17 @@
|
||||
import { test, expect } from 'vitest'
|
||||
import { normalizeAmount } from './normalizeAmount'
|
||||
|
||||
test('normalizeAmount replaces comma with a period', () => {
|
||||
const result = normalizeAmount('1,234')
|
||||
expect(result).toBe('1.234')
|
||||
})
|
||||
|
||||
test('normalizeAmount removes non-digit and non-decimal characters', () => {
|
||||
const result = normalizeAmount('1234.56abc')
|
||||
expect(result).toBe('1234.56')
|
||||
})
|
||||
|
||||
test('normalizeAmount removes non-digit and non-decimal characters', () => {
|
||||
const result = normalizeAmount('1234,56abc')
|
||||
expect(result).toBe('1234.56')
|
||||
})
|
9
src/features/Web3/lib/normalizeAmount/normalizeAmount.ts
Normal file
9
src/features/Web3/lib/normalizeAmount/normalizeAmount.ts
Normal file
@ -0,0 +1,9 @@
|
||||
export function normalizeAmount(amount: string): string {
|
||||
// Replace comma used as a decimal separator with a period
|
||||
amount = amount.replace(',', '.')
|
||||
|
||||
// Remove any non-digit or non-decimal characters
|
||||
amount = amount.replace(/[^\d.]/g, '')
|
||||
|
||||
return amount
|
||||
}
|
@ -1,5 +1,12 @@
|
||||
import { atom } from 'nanostores'
|
||||
import { action, atom } from 'nanostores'
|
||||
import { normalizeAmount } from '../lib/normalizeAmount'
|
||||
|
||||
export const $isInitSend = atom<boolean>(false)
|
||||
export const $amount = atom<string>('')
|
||||
export const $txHash = atom<string | undefined>()
|
||||
|
||||
export const $setAmount = action($amount, 'setAmount', (store, amount) => {
|
||||
const normalizedAmount = normalizeAmount(amount)
|
||||
store.set(normalizedAmount)
|
||||
return store.get()
|
||||
})
|
||||
|
Loading…
Reference in New Issue
Block a user