-
Notifications
You must be signed in to change notification settings - Fork 90
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: extract components from swap page, improve input formatting
- Loading branch information
Showing
21 changed files
with
392 additions
and
340 deletions.
There are no files selected for viewing
49 changes: 49 additions & 0 deletions
49
packages/widget/src/components/RoutePrioritySelect/RoutePrioritySelect.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,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> | ||
); | ||
}; |
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 @@ | ||
export * from './RoutePrioritySelect'; |
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
4 changes: 2 additions & 2 deletions
4
packages/widget/src/components/SwapInput.tsx → .../components/SwapInput/SwapInput.style.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
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,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> | ||
); | ||
}; |
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 @@ | ||
export * from './SwapInput'; |
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.