-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* fix(minifront): #1675: render decimals validation errors in swap * chore: changeset
- Loading branch information
Showing
6 changed files
with
56 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'minifront': patch | ||
--- | ||
|
||
Render the error in swap page in case of incorrect decimals or insufficient funds |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 3 additions & 28 deletions
31
apps/minifront/src/components/shared/number-input/index.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,9 @@ | ||
import type { FC, KeyboardEventHandler } from 'react'; | ||
import type { FC } from 'react'; | ||
import { Input, InputProps } from '@penumbra-zone/ui/components/ui/input'; | ||
import { useWheelPrevent } from './use-wheel-prevent'; | ||
|
||
export interface NumberInputProps extends InputProps { | ||
/** If present, prevents users from entering the fractional number part longer than `maxExponent` */ | ||
maxExponent?: number; | ||
} | ||
|
||
export const NumberInput: FC<NumberInputProps> = ({ maxExponent, ...props }) => { | ||
export const NumberInput: FC<InputProps> = props => { | ||
const inputRef = useWheelPrevent(); | ||
|
||
const onKeyDown: KeyboardEventHandler<HTMLInputElement> = event => { | ||
if (maxExponent === 0 && (event.key === '.' || event.key === ',')) { | ||
event.preventDefault(); | ||
return; | ||
} | ||
|
||
if ( | ||
typeof maxExponent !== 'undefined' && | ||
typeof props.value === 'string' && | ||
!Number.isNaN(Number(event.key)) | ||
) { | ||
const fraction = `${props.value}${event.key}`.split('.')[1]?.length; | ||
if (fraction && fraction > maxExponent) { | ||
event.preventDefault(); | ||
return; | ||
} | ||
} | ||
props.onKeyDown?.(event); | ||
}; | ||
|
||
return <Input ref={inputRef} {...props} type='number' onKeyDown={onKeyDown} />; | ||
return <Input ref={inputRef} {...props} type='number' />; | ||
}; |
23 changes: 23 additions & 0 deletions
23
apps/minifront/src/components/swap/swap-form/token-input-error.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { useStoreShallow } from '../../../utils/use-store-shallow.ts'; | ||
import { swapErrorSelector } from '../../../state/swap'; | ||
|
||
export const TokenInputError = () => { | ||
const { | ||
incorrectDecimalErr, | ||
amountMoreThanBalanceErr, | ||
swappableAssetsError, | ||
balanceResponsesError, | ||
} = useStoreShallow(swapErrorSelector); | ||
|
||
const error = | ||
amountMoreThanBalanceErr || | ||
incorrectDecimalErr || | ||
swappableAssetsError || | ||
balanceResponsesError; | ||
|
||
if (!error) { | ||
return null; | ||
} | ||
|
||
return <div className='ml-auto text-xs text-red-400'>{error}</div>; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters