Skip to content

Commit

Permalink
refactor: extract components from swap page, improve input formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
chybisov committed Feb 21, 2022
1 parent 68d20e9 commit 07e5d69
Show file tree
Hide file tree
Showing 21 changed files with 392 additions and 340 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { HelpOutline as HelpOutlineIcon } from '@mui/icons-material';
import { Box, FormControl, MenuItem, Typography } from '@mui/material';
import { useFormContext } from 'react-hook-form';
import { useTranslation } from 'react-i18next';
import { SwapFormKey } from '../../providers/SwapFormProvider';
import { Select } from '../Select';

export const RoutePrioritySelect: React.FC<{ fullWidth?: boolean }> = ({
fullWidth,
}) => {
const { t } = useTranslation();
const { register } = useFormContext();

return (
<Box
sx={{
display: fullWidth ? 'block' : 'flex',
justifyContent: fullWidth ? 'unset' : 'space-between',
alignItems: fullWidth ? 'unset' : 'center',
}}
>
<Box
sx={{ display: 'flex', alignItems: 'center' }}
mb={fullWidth ? 1 : 0}
>
<HelpOutlineIcon sx={{ color: 'grey.500' }} />
<Typography
lineHeight="normal"
variant="subtitle1"
color="text.primary"
ml={1}
>
{t(`settings.routePriority.title`)}
</Typography>
</Box>
<FormControl sx={{ width: fullWidth ? '100%' : '50%' }} fullWidth>
<Select
defaultValue={1}
MenuProps={{ elevation: 2 }}
inputProps={{ ...register(SwapFormKey.RoutePriority) }}
>
<MenuItem value={1}>
{t(`settings.routePriority.recommended`)}
</MenuItem>
</Select>
</FormControl>
</Box>
);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './RoutePrioritySelect';
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export const SearchTokenInput = ({ formType }: SwapFormTypeProps) => {
</InputAdornment>
}
inputProps={{
inputMode: 'search',
...register(SwapFormKey.FromSearchTokensFilter),
}}
autoComplete="off"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
MenuItem,
Typography,
} from '@mui/material';
import { forwardRef, MutableRefObject, useState } from 'react';
import { forwardRef, MutableRefObject, useEffect, useState } from 'react';
import { useFormContext } from 'react-hook-form';
import { useTranslation } from 'react-i18next';
import { useResizeDetector } from 'react-resize-detector';
Expand Down Expand Up @@ -35,13 +35,13 @@ export const SelectTokenDrawer = forwardRef<SelectTokenDrawerBase, DrawerProps>(
const closeDrawer = (ref as MutableRefObject<SelectTokenDrawerBase | null>)
.current?.closeDrawer;

useEffect(() => {
register(SwapFormKey.FromToken);
register(SwapFormKey.ToToken);
}, [register]);

return (
<ContainerDrawer
elementRef={drawerRef}
ref={ref}
onOpen={setFormType}
route="selectToken"
>
<ContainerDrawer elementRef={drawerRef} ref={ref} onOpen={setFormType}>
<Box role="presentation">
<TokenList
height={drawerHeight ?? 0}
Expand Down
44 changes: 0 additions & 44 deletions packages/widget/src/components/SendToRecipientForm.tsx

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,8 @@ export const AdvancedPreferences = () => {
};

return (
<Box px={3}>
<Box p={3}>
<Box
mt={3}
sx={{
display: 'flex',
justifyContent: 'space-between',
Expand Down
41 changes: 5 additions & 36 deletions packages/widget/src/components/SettingsDrawer/SettingsDrawer.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,9 @@
import { HelpOutline as HelpOutlineIcon } from '@mui/icons-material';
import {
Box,
Divider,
DrawerProps,
FormControl,
MenuItem,
Typography,
} from '@mui/material';
import { Box, Divider, DrawerProps, Typography } from '@mui/material';
import { forwardRef } from 'react';
import { useFormContext } from 'react-hook-form';
import { useTranslation } from 'react-i18next';
import { SwapFormKey } from '../../providers/SwapFormProvider';
import { ContainerDrawer } from '../ContainerDrawer';
import { Select } from '../Select';
import { RoutePrioritySelect } from '../RoutePrioritySelect';
import { AdvancedPreferences } from './AdvancedPreferences';
import { GasPriceButtonGroup } from './GasPriceButtonGroup';
import { SlippageInput } from './SlippageInput';
Expand All @@ -21,35 +12,13 @@ import { SettingsDrawerBase } from './types';
export const SettingsDrawer = forwardRef<SettingsDrawerBase, DrawerProps>(
(_, ref) => {
const { t } = useTranslation();
const { register } = useFormContext();

return (
<ContainerDrawer ref={ref} route="settings">
<Box role="presentation">
<Box px={3}>
<Box sx={{ display: 'flex', alignItems: 'center' }} mt={3} mb={1}>
<HelpOutlineIcon sx={{ color: 'grey.500' }} />
<Typography
variant="subtitle1"
color="text.secondary"
lineHeight="normal"
ml={1}
>
{t(`settings.routePriority.title`)}
</Typography>
</Box>
<FormControl fullWidth>
<Select
defaultValue={1}
MenuProps={{ elevation: 2 }}
inputProps={{ ...register(SwapFormKey.SwapRoute) }}
>
<MenuItem value={1}>
{t(`settings.routePriority.recommended`)}
</MenuItem>
</Select>
</FormControl>
<Box sx={{ display: 'flex', alignItems: 'center' }} my={3}>
<Box p={3}>
<RoutePrioritySelect fullWidth />
<Box sx={{ display: 'flex', alignItems: 'center' }} mt={3}>
<Box pr={2}>
<Box sx={{ display: 'flex', alignItems: 'center' }} mb={1}>
<HelpOutlineIcon
Expand Down
33 changes: 6 additions & 27 deletions packages/widget/src/components/SettingsDrawer/SlippageInput.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { formatSlippage } from '@lifinance/widget/utils/format';
import { FormControl, InputAdornment } from '@mui/material';
import { ChangeEvent, useEffect, useRef } from 'react';
import { useFormContext, useWatch } from 'react-hook-form';
Expand All @@ -21,38 +22,19 @@ export const SlippageInput = () => {
event: ChangeEvent<HTMLInputElement | HTMLTextAreaElement>,
) => {
const { value } = event.target;
const parsedValue = parseFloat(value);
console.log(formatSlippage(value, defaultValue.current));
setValue(
SwapFormKey.Slippage,
`${
isNaN(parsedValue)
? defaultValue.current
: parsedValue > 100
? 100
: parsedValue < 0
? Math.abs(parsedValue)
: value
}`,
formatSlippage(value, defaultValue.current, true),
);
};

const handleBlur = (
event: ChangeEvent<HTMLInputElement | HTMLTextAreaElement>,
) => {
const { value } = event.target;
const parsedValue = parseFloat(value);
setValue(
SwapFormKey.Slippage,
`${
isNaN(parsedValue)
? defaultValue.current
: parsedValue > 100
? 100
: parsedValue < 0
? Math.abs(parsedValue)
: parsedValue
}`,
);
console.log(formatSlippage(value, defaultValue.current));
setValue(SwapFormKey.Slippage, formatSlippage(value, defaultValue.current));
};

return (
Expand All @@ -63,10 +45,7 @@ export const SlippageInput = () => {
endAdornment={<InputAdornment position="end">%</InputAdornment>}
autoComplete="off"
inputProps={{
type: 'number',
inputMode: 'numeric',
min: 0,
step: 0.1,
inputMode: 'decimal',
}}
onChange={handleChange}
onBlur={handleBlur}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { styled } from '@mui/material/styles';
import { Input } from './Input';
import { Input as InputBase } from '../Input';

export const SwapInput = styled(Input)({
export const Input = styled(InputBase)({
borderRadius: '0 0 8px 8px',
borderWidth: '1px 2px 2px 2px',
'& input[type="number"]::-webkit-outer-spin-button, & input[type="number"]::-webkit-inner-spin-button':
Expand Down
63 changes: 63 additions & 0 deletions packages/widget/src/components/SwapInput/SwapInput.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { FormControl } from '@mui/material';
import { ChangeEvent, useEffect } from 'react';
import { useFormContext, useWatch } from 'react-hook-form';
import { useTranslation } from 'react-i18next';
import {
SwapFormKeyHelper,
SwapFormTypeProps,
} from '../../providers/SwapFormProvider';
import { formatAmount, formatTokenAmount } from '../../utils/format';
import { SwapInputAdornment } from '../SwapInputAdornment';
import { Input } from './SwapInput.style';

export const SwapInput: React.FC<SwapFormTypeProps> = ({ formType }) => {
const { t } = useTranslation();
const {
register,
setValue,
formState: { isSubmitting },
} = useFormContext();
const amountKey = SwapFormKeyHelper.getAmountKey(formType);
const value = useWatch({
name: amountKey,
});

useEffect(() => {
register(amountKey, { required: true });
}, [amountKey, register]);
console.log(formatTokenAmount('5'));

const handleChange = (
event: ChangeEvent<HTMLInputElement | HTMLTextAreaElement>,
) => {
const { value } = event.target;
setValue(amountKey, formatAmount(value, true));
};

const handleBlur = (
event: ChangeEvent<HTMLInputElement | HTMLTextAreaElement>,
) => {
const { value } = event.target;
setValue(amountKey, formatAmount(value));
};

return (
<FormControl disabled={isSubmitting} fullWidth>
<Input
size="small"
autoComplete="off"
placeholder={t(`swap.enterAmount`)}
endAdornment={<SwapInputAdornment formType={formType} />}
inputProps={{
inputMode: 'decimal',
}}
onChange={handleChange}
onBlur={handleBlur}
value={value}
name={amountKey}
required
/>
{/* <FormHelperText id="swap-from-helper-text">Text</FormHelperText> */}
</FormControl>
);
};
1 change: 1 addition & 0 deletions packages/widget/src/components/SwapInput/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './SwapInput';
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { InputAdornment, Skeleton, Typography } from '@mui/material';
import BigNumber from 'bignumber.js';
import { useMemo } from 'react';
import { useWatch } from 'react-hook-form';
import { useTranslation } from 'react-i18next';
Expand Down Expand Up @@ -31,10 +30,8 @@ export const SwapInputAdornment: React.FC<SwapFormTypeProps> = ({

const amount = useMemo(
() =>
token
? formatTokenAmount(token, new BigNumber(tokenWithBalance?.amount ?? 0))
: null,
[token, tokenWithBalance?.amount],
tokenWithBalance ? formatTokenAmount(tokenWithBalance.amount) : null,
[tokenWithBalance],
);

return (
Expand Down
Loading

0 comments on commit 07e5d69

Please sign in to comment.