mirror of
https://github.com/kremalicious/blog.git
synced 2024-11-22 09:56: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')
|
const [euro, setEuro] = useState('0.00')
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!selectedToken?.price || !amount) {
|
if (!selectedToken?.price || !amount || amount === '') {
|
||||||
setDollar('0.00')
|
setDollar('0.00')
|
||||||
setEuro('0.00')
|
setEuro('0.00')
|
||||||
return
|
return
|
||||||
|
@ -3,7 +3,12 @@ import { useAccount } from 'wagmi'
|
|||||||
import { InputGroup } from '../Input'
|
import { InputGroup } from '../Input'
|
||||||
import styles from './Form.module.css'
|
import styles from './Form.module.css'
|
||||||
import { useStore } from '@nanostores/react'
|
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 siteConfig from '@config/blog.config'
|
||||||
import { Send } from '../Send'
|
import { Send } from '../Send'
|
||||||
import { RainbowKit } from '../RainbowKit/RainbowKit'
|
import { RainbowKit } from '../RainbowKit/RainbowKit'
|
||||||
@ -18,6 +23,7 @@ export function Web3Form(): ReactElement {
|
|||||||
|
|
||||||
const [error, setError] = useState<string>()
|
const [error, setError] = useState<string>()
|
||||||
|
|
||||||
|
// Error Validation
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!amount || amount === '' || !selectedToken?.balance) {
|
if (!amount || amount === '' || !selectedToken?.balance) {
|
||||||
setError(undefined)
|
setError(undefined)
|
||||||
@ -34,7 +40,7 @@ export function Web3Form(): ReactElement {
|
|||||||
// reset amount whenever token changes
|
// reset amount whenever token changes
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!selectedToken) return
|
if (!selectedToken) return
|
||||||
$amount.set('')
|
$setAmount('')
|
||||||
}, [selectedToken])
|
}, [selectedToken])
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
@ -3,7 +3,12 @@ import Input from '@components/Input'
|
|||||||
import { Conversion } from '../Conversion'
|
import { Conversion } from '../Conversion'
|
||||||
import styles from './InputGroup.module.css'
|
import styles from './InputGroup.module.css'
|
||||||
import { TokenSelect } from '../TokenSelect'
|
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'
|
import { useStore } from '@nanostores/react'
|
||||||
|
|
||||||
export function InputGroup({
|
export function InputGroup({
|
||||||
@ -19,7 +24,7 @@ export function InputGroup({
|
|||||||
const [isFocus, setIsFocus] = useState(false)
|
const [isFocus, setIsFocus] = useState(false)
|
||||||
|
|
||||||
function handleChange(newAmount: string) {
|
function handleChange(newAmount: string) {
|
||||||
$amount.set(newAmount)
|
$setAmount(newAmount)
|
||||||
}
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
@ -36,6 +41,7 @@ export function InputGroup({
|
|||||||
type="text"
|
type="text"
|
||||||
inputMode="decimal"
|
inputMode="decimal"
|
||||||
pattern="[0-9.]*"
|
pattern="[0-9.]*"
|
||||||
|
lang="en-US"
|
||||||
value={amount}
|
value={amount}
|
||||||
placeholder="0.00"
|
placeholder="0.00"
|
||||||
onChange={(e) => handleChange(e.target.value)}
|
onChange={(e) => handleChange(e.target.value)}
|
||||||
|
@ -11,9 +11,10 @@ interface SelectItemProps extends HTMLAttributes<HTMLDivElement> {
|
|||||||
|
|
||||||
export const Token = forwardRef<HTMLDivElement, SelectItemProps>(
|
export const Token = forwardRef<HTMLDivElement, SelectItemProps>(
|
||||||
({ className, token, ...props }, forwardedRef) => {
|
({ className, token, ...props }, forwardedRef) => {
|
||||||
|
const locale = window?.navigator?.language || 'en'
|
||||||
const balance =
|
const balance =
|
||||||
token?.balance && token?.symbol
|
token?.balance && token?.symbol
|
||||||
? formatCurrency(token.balance, token.symbol, 'en', false, {
|
? formatCurrency(token.balance, token.symbol, locale, false, {
|
||||||
decimalPlaces: 3,
|
decimalPlaces: 3,
|
||||||
significantFigures: 3
|
significantFigures: 3
|
||||||
})
|
})
|
||||||
@ -23,7 +24,7 @@ export const Token = forwardRef<HTMLDivElement, SelectItemProps>(
|
|||||||
token?.balance && token?.price?.usd
|
token?.balance && token?.price?.usd
|
||||||
? token?.balance * token?.price.usd
|
? token?.balance * token?.price.usd
|
||||||
: 0
|
: 0
|
||||||
const valueInUsdFormatted = formatCurrency(valueInUsd, 'USD', 'en')
|
const valueInUsdFormatted = formatCurrency(valueInUsd, 'USD', locale)
|
||||||
|
|
||||||
return balance && parseInt(balance) !== 0 && valueInUsd >= 1 ? (
|
return balance && parseInt(balance) !== 0 && valueInUsd >= 1 ? (
|
||||||
<Select.Item
|
<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 $isInitSend = atom<boolean>(false)
|
||||||
export const $amount = atom<string>('')
|
export const $amount = atom<string>('')
|
||||||
export const $txHash = atom<string | undefined>()
|
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