diff --git a/packages/widget/src/components/SettingsDrawer/SettingsDrawer.tsx b/packages/widget/src/components/SettingsDrawer/SettingsDrawer.tsx index 2f948be76..b370c64ba 100644 --- a/packages/widget/src/components/SettingsDrawer/SettingsDrawer.tsx +++ b/packages/widget/src/components/SettingsDrawer/SettingsDrawer.tsx @@ -12,10 +12,10 @@ import { useFormContext } from 'react-hook-form'; import { useTranslation } from 'react-i18next'; import { SwapFormKey } from '../../providers/SwapFormProvider'; import { ContainerDrawer } from '../ContainerDrawer'; -import { Input } from '../Input'; import { Select } from '../Select'; import { AdvancedPreferences } from './AdvancedPreferences'; import { GasPriceButtonGroup } from './GasPriceButtonGroup'; +import { SlippageInput } from './SlippageInput'; import { SettingsDrawerBase } from './types'; export const SettingsDrawer = forwardRef( @@ -63,14 +63,7 @@ export const SettingsDrawer = forwardRef( {t(`settings.slippage`)} - - - + diff --git a/packages/widget/src/components/SettingsDrawer/SlippageInput.tsx b/packages/widget/src/components/SettingsDrawer/SlippageInput.tsx new file mode 100644 index 000000000..96435a203 --- /dev/null +++ b/packages/widget/src/components/SettingsDrawer/SlippageInput.tsx @@ -0,0 +1,78 @@ +import { FormControl, InputAdornment } from '@mui/material'; +import { ChangeEvent, useEffect, useRef } from 'react'; +import { useFormContext, useWatch } from 'react-hook-form'; +import { useTranslation } from 'react-i18next'; +import { SwapFormKey } from '../../providers/SwapFormProvider'; +import { Input } from '../Input'; + +export const SlippageInput = () => { + const { t } = useTranslation(); + const { register, setValue } = useFormContext(); + const value = useWatch({ + name: SwapFormKey.Slippage, + }); + const defaultValue = useRef(value); + + useEffect(() => { + register(SwapFormKey.Slippage); + }, [register]); + + const handleChange = ( + event: ChangeEvent, + ) => { + const { value } = event.target; + const parsedValue = parseFloat(value); + setValue( + SwapFormKey.Slippage, + `${ + isNaN(parsedValue) + ? defaultValue.current + : parsedValue > 100 + ? 100 + : parsedValue < 0 + ? Math.abs(parsedValue) + : value + }`, + ); + }; + + const handleBlur = ( + event: ChangeEvent, + ) => { + const { value } = event.target; + const parsedValue = parseFloat(value); + setValue( + SwapFormKey.Slippage, + `${ + isNaN(parsedValue) + ? defaultValue.current + : parsedValue > 100 + ? 100 + : parsedValue < 0 + ? Math.abs(parsedValue) + : parsedValue + }`, + ); + }; + + return ( + + %} + autoComplete="off" + inputProps={{ + type: 'number', + inputMode: 'numeric', + min: 0, + step: 0.1, + }} + onChange={handleChange} + onBlur={handleBlur} + value={value} + name={SwapFormKey.Slippage} + /> + + ); +}; diff --git a/packages/widget/src/providers/SwapFormProvider/SwapFormProvider.tsx b/packages/widget/src/providers/SwapFormProvider/SwapFormProvider.tsx index 80646f5bf..852346c38 100644 --- a/packages/widget/src/providers/SwapFormProvider/SwapFormProvider.tsx +++ b/packages/widget/src/providers/SwapFormProvider/SwapFormProvider.tsx @@ -6,7 +6,7 @@ const defaultValues = { [SwapFormKey.FromAmount]: 0, [SwapFormKey.FromSearchTokensFilter]: '', [SwapFormKey.GasPrice]: 'normal', - [SwapFormKey.Slippage]: '0.5', + [SwapFormKey.Slippage]: '3', [SwapFormKey.ToAmount]: 0, [SwapFormKey.ToSearchTokensFilter]: '', };