-
Notifications
You must be signed in to change notification settings - Fork 146
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
58 changed files
with
596 additions
and
655 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
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
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
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 was deleted.
Oops, something went wrong.
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,62 +1,73 @@ | ||
import { ChangeEvent } from 'react'; | ||
|
||
import { Input, Stack, color } from '@stacks/ui'; | ||
import { useField, useFormikContext } from 'formik'; | ||
import { Stack, styled } from 'leather-styles/jsx'; | ||
|
||
import { isDefined, isUndefined } from '@shared/utils'; | ||
|
||
import { useShowFieldError } from '@app/common/form-utils'; | ||
import { Caption } from '@app/components/typography'; | ||
|
||
import { SwapFormValues } from '../hooks/use-swap'; | ||
import { SwapFormValues } from '../hooks/use-swap-form'; | ||
import { useSwapContext } from '../swap.context'; | ||
|
||
function getPlaceholderValue(name: string, values: SwapFormValues) { | ||
if (name === 'swapAmountFrom' && isDefined(values.swapAssetFrom)) return '0'; | ||
if (name === 'swapAmountTo' && isDefined(values.swapAssetTo)) return '0'; | ||
return '-'; | ||
} | ||
|
||
interface SwapAmountFieldProps { | ||
amountAsFiat: string; | ||
isDisabled?: boolean; | ||
name: string; | ||
} | ||
export function SwapAmountField({ amountAsFiat, isDisabled, name }: SwapAmountFieldProps) { | ||
const { fetchToAmount } = useSwapContext(); | ||
const { setFieldValue, values } = useFormikContext<SwapFormValues>(); | ||
const { fetchToAmount, onSetIsSendingMax } = useSwapContext(); | ||
const { setErrors, setFieldValue, values } = useFormikContext<SwapFormValues>(); | ||
const [field] = useField(name); | ||
const showError = useShowFieldError(name); | ||
const showError = useShowFieldError(name) && name === 'swapAmountFrom' && values.swapAssetTo; | ||
|
||
async function onChange(event: ChangeEvent<HTMLInputElement>) { | ||
field.onChange(event); | ||
const value = event.currentTarget.value; | ||
const { swapAssetFrom, swapAssetTo } = values; | ||
if (swapAssetFrom != null && swapAssetTo && !isNaN(Number(value))) { | ||
await setFieldValue('swapAmountTo', ''); | ||
const toAmount = await fetchToAmount(swapAssetFrom, swapAssetTo, value); | ||
await setFieldValue('swapAmountTo', toAmount); | ||
} | ||
if (isUndefined(swapAssetFrom) || isUndefined(swapAssetTo)) return; | ||
onSetIsSendingMax(false); | ||
const value = event.currentTarget.value; | ||
const toAmount = await fetchToAmount(swapAssetFrom, swapAssetTo, value); | ||
await setFieldValue('swapAmountTo', Number(toAmount)); | ||
field.onChange(event); | ||
setErrors({}); | ||
} | ||
|
||
return ( | ||
<Stack alignItems="flex-end" spacing="extra-tight" width="50%"> | ||
<Caption as="label" hidden htmlFor={name}> | ||
<Stack alignItems="flex-end" gap="space.01" width="65%"> | ||
<styled.label hidden htmlFor={name}> | ||
{name} | ||
</Caption> | ||
<Input | ||
_disabled={{ border: 'none', color: color('text-caption') }} | ||
_focus={{ border: 'none' }} | ||
</styled.label> | ||
<styled.input | ||
_disabled={{ | ||
bg: 'transparent', | ||
border: 'none', | ||
color: 'accent.text-subdued', | ||
}} | ||
autoComplete="off" | ||
border="none" | ||
color={showError ? color('feedback-error') : 'unset'} | ||
color={showError ? 'error' : 'accent.text-primary'} | ||
display="block" | ||
fontSize="20px" | ||
height="28px" | ||
isDisabled={isDisabled} | ||
disabled={isDisabled} | ||
p="0px" | ||
placeholder="0" | ||
placeholder={getPlaceholderValue(name, values)} | ||
ring="none" | ||
textAlign="right" | ||
type="number" | ||
textStyle="heading.05" | ||
width="100%" | ||
{...field} | ||
onChange={onChange} | ||
/> | ||
<Caption color={showError ? color('feedback-error') : color('text-caption')}> | ||
{amountAsFiat} | ||
</Caption> | ||
{amountAsFiat ? ( | ||
<styled.span color={showError ? 'error' : 'accent.text-subdued'} textStyle="caption.02"> | ||
{amountAsFiat} | ||
</styled.span> | ||
) : null} | ||
</Stack> | ||
); | ||
} |
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
Oops, something went wrong.