Skip to content

Commit

Permalink
fix: allow dot and comma as decimal separators on number inputs (#7087)
Browse files Browse the repository at this point in the history
  • Loading branch information
NeOMakinG authored Jun 10, 2024
1 parent 5fe8334 commit 1f18408
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/components/DeFi/components/Allocation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import { RawText } from 'components/Text'
import { useLocaleFormatter } from 'hooks/useLocaleFormatter/useLocaleFormatter'
import { useToggle } from 'hooks/useToggle/useToggle'
import { bn, bnOrZero } from 'lib/bignumber/bignumber'
import { allowedDecimalSeparators } from 'state/slices/preferencesSlice/preferencesSlice'
import { colors } from 'theme/colors'

const cryptoInputStyle = { caretColor: colors.blue[200] }
Expand Down Expand Up @@ -182,6 +183,7 @@ export const Allocation: React.FC<AllocationProps> = ({
prefix={isFiat ? localeParts.prefix : ''}
decimalSeparator={localeParts.decimal}
inputMode='decimal'
allowedDecimalSeparators={allowedDecimalSeparators}
thousandSeparator={localeParts.group}
value={isFiat ? bnOrZero(fiatAmount).toFixed(2) : formattedCryptoAmount}
onValueChange={handleNumberFormatValueChange}
Expand Down
2 changes: 2 additions & 0 deletions src/components/DeFi/components/AssetInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { AssetIcon } from 'components/AssetIcon'
import { useLocaleFormatter } from 'hooks/useLocaleFormatter/useLocaleFormatter'
import { useToggle } from 'hooks/useToggle/useToggle'
import { bnOrZero } from 'lib/bignumber/bignumber'
import { allowedDecimalSeparators } from 'state/slices/preferencesSlice/preferencesSlice'
import { colors } from 'theme/colors'

import { Balance } from './Balance'
Expand Down Expand Up @@ -170,6 +171,7 @@ export const AssetInput: React.FC<AssetInputProps> = ({
prefix={isFiat ? localeParts.prefix : ''}
decimalSeparator={localeParts.decimal}
inputMode='decimal'
allowedDecimalSeparators={allowedDecimalSeparators}
thousandSeparator={localeParts.group}
value={isFiat ? bnOrZero(fiatAmount).toFixed(2) : formattedCryptoAmount}
onValueChange={handleValueChange}
Expand Down
2 changes: 2 additions & 0 deletions src/components/FeeExplainer/components/FeeInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type { NumberFormatValues } from 'react-number-format'
import NumberFormat from 'react-number-format'
import { useTranslate } from 'react-polyglot'
import { useLocaleFormatter } from 'hooks/useLocaleFormatter/useLocaleFormatter'
import { allowedDecimalSeparators } from 'state/slices/preferencesSlice/preferencesSlice'

const CryptoInput = (props: InputProps) => {
const translate = useTranslate()
Expand Down Expand Up @@ -60,6 +61,7 @@ export const FeeInput: React.FC<FeeInputProps> = ({ isFiat, onChange, value }) =
prefix={isFiat ? localeParts.prefix : ''}
decimalSeparator={localeParts.decimal}
inputMode='decimal'
allowedDecimalSeparators={allowedDecimalSeparators}
thousandSeparator={localeParts.group}
value={value}
onValueChange={handleValueChange}
Expand Down
6 changes: 5 additions & 1 deletion src/components/Modals/Settings/BalanceThresholdInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ import { FaGreaterThanEqual } from 'react-icons/fa'
import type { NumberFormatValues } from 'react-number-format'
import NumberFormat from 'react-number-format'
import { useLocaleFormatter } from 'hooks/useLocaleFormatter/useLocaleFormatter'
import { preferences } from 'state/slices/preferencesSlice/preferencesSlice'
import {
allowedDecimalSeparators,
preferences,
} from 'state/slices/preferencesSlice/preferencesSlice'
import { selectBalanceThreshold } from 'state/slices/selectors'
import { useAppDispatch, useAppSelector } from 'state/store'

Expand Down Expand Up @@ -44,6 +47,7 @@ export const BalanceThresholdInput = () => {
thousandSeparator={localeParts.group}
decimalSeparator={localeParts.decimal}
customInput={InputComponent}
allowedDecimalSeparators={allowedDecimalSeparators}
isNumericString={true}
value={balanceThreshold}
prefix={localeParts.prefix}
Expand Down
2 changes: 2 additions & 0 deletions src/components/MultiHopTrade/components/TradeAmountInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import { Balance } from 'components/DeFi/components/Balance'
import { PercentOptionsButtonGroup } from 'components/DeFi/components/PercentOptionsButtonGroup'
import { useLocaleFormatter } from 'hooks/useLocaleFormatter/useLocaleFormatter'
import { bnOrZero } from 'lib/bignumber/bignumber'
import { allowedDecimalSeparators } from 'state/slices/preferencesSlice/preferencesSlice'
import { selectMarketDataByAssetIdUserCurrency } from 'state/slices/selectors'
import { useAppSelector } from 'state/store'
import { colors } from 'theme/colors'
Expand Down Expand Up @@ -232,6 +233,7 @@ export const TradeAmountInput: React.FC<TradeAmountInputProps> = memo(
prefix={isFiat ? localeParts.prefix : ''}
decimalSeparator={localeParts.decimal}
inputMode='decimal'
allowedDecimalSeparators={allowedDecimalSeparators}
thousandSeparator={localeParts.group}
value={isFiat ? bnOrZero(fiatAmount).toFixed(2) : formattedCryptoAmount}
// this is already within a useCallback, we don't need to memo this
Expand Down
2 changes: 2 additions & 0 deletions src/components/TokenRow/TokenRow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { Controller } from 'react-hook-form'
import NumberFormat from 'react-number-format'
import { useTranslate } from 'react-polyglot'
import { useLocaleFormatter } from 'hooks/useLocaleFormatter/useLocaleFormatter'
import { allowedDecimalSeparators } from 'state/slices/preferencesSlice/preferencesSlice'

const CryptoInput = (props: InputProps) => {
const translate = useTranslate()
Expand Down Expand Up @@ -59,6 +60,7 @@ export function TokenRow<C extends FieldValues>({
inputMode='decimal'
thousandSeparator={localeParts.group}
decimalSeparator={localeParts.decimal}
allowedDecimalSeparators={allowedDecimalSeparators}
customInput={CryptoInput}
isNumericString={true}
value={value}
Expand Down
2 changes: 2 additions & 0 deletions src/state/slices/preferencesSlice/preferencesSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ export enum CurrencyFormats {
CommaDecimalDotThousands = 'de-DE', // 123.456,78 $
}

export const allowedDecimalSeparators = ['.', ',']

export enum HomeMarketView {
TopAssets = 'TopAssets',
Watchlist = 'Watchlist',
Expand Down

0 comments on commit 1f18408

Please sign in to comment.