diff --git a/ui/pages/bridge/prepare/bridge-transaction-settings-modal.tsx b/ui/pages/bridge/prepare/bridge-transaction-settings-modal.tsx index 8e03827103de..267b0d5df7e3 100644 --- a/ui/pages/bridge/prepare/bridge-transaction-settings-modal.tsx +++ b/ui/pages/bridge/prepare/bridge-transaction-settings-modal.tsx @@ -16,6 +16,9 @@ import { Text, TextField, TextFieldType, + BannerAlert, + BannerAlertSeverity, + Box, } from '../../../components/component-library'; import { useI18nContext } from '../../../hooks/useI18nContext'; import { @@ -26,6 +29,7 @@ import { JustifyContent, TextColor, TextVariant, + SEVERITIES, } from '../../../helpers/constants/design-system'; import { getSlippage } from '../../../ducks/bridge/selectors'; import { setSlippage } from '../../../ducks/bridge/actions'; @@ -50,13 +54,32 @@ export const BridgeTransactionSettingsModal = ({ const [localSlippage, setLocalSlippage] = useState( slippage, ); - const [customSlippage, setCustomSlippage] = useState( - slippage && HARDCODED_SLIPPAGE_OPTIONS.includes(slippage) - ? undefined - : slippage, + const [customSlippage, setCustomSlippage] = useState( + slippage && !HARDCODED_SLIPPAGE_OPTIONS.includes(slippage) + ? slippage.toString() + : undefined, ); const [showCustomButton, setShowCustomButton] = useState(true); + const getNotificationConfig = () => { + if (!customSlippage) { + return null; + } + + const slippageValue = Number(customSlippage.replace(',', '.')); + if (slippageValue < 0.5) { + return { + severity: SEVERITIES.WARNING, + text: t('swapSlippageLowDescription', [slippageValue]), + title: t('swapSlippageLowTitle'), + }; + } + + return null; + }; + + const notificationConfig = getNotificationConfig(); + return ( @@ -156,47 +179,49 @@ export const BridgeTransactionSettingsModal = ({ type={TextFieldType.Text} value={customSlippage} onChange={(e) => { - // Remove characters that are not numbers or decimal points if rendering a controlled or pasted value - const cleanedValue = e.target.value.replace(/[^0-9.]+/gu, ''); - setLocalSlippage(undefined); - setCustomSlippage( - cleanedValue.length > 0 ? Number(cleanedValue) : undefined, - ); + const { value } = e.target; + if (value === '' || /^\d*[.,]?\d*$/u.test(value)) { + setLocalSlippage(undefined); + setCustomSlippage(value); + } }} autoFocus={true} onBlur={() => { - console.log('====blur'); setShowCustomButton(true); }} onFocus={() => { - console.log('====focus'); setShowCustomButton(false); }} - onKeyPress={(e?: React.KeyboardEvent) => { - // Only allow numbers and at most one decimal point - if ( - e && - !/^[0-9]*\.{0,1}[0-9]*$/u.test( - `${customSlippage ?? ''}${e.key}`, - ) - ) { - e.preventDefault(); - } - }} endAccessory={%} /> )} + {notificationConfig && ( + + + {notificationConfig.text} + + + )} { - const newSlippage = localSlippage || customSlippage; - newSlippage && + const newSlippage = + localSlippage ?? Number(customSlippage?.replace(',', '.')); + if (newSlippage) { trackCrossChainSwapsEvent({ event: MetaMetricsEventName.InputChanged, properties: { @@ -204,8 +229,9 @@ export const BridgeTransactionSettingsModal = ({ value: newSlippage.toString(), }, }); - dispatch(setSlippage(newSlippage)); - onClose(); + dispatch(setSlippage(newSlippage)); + onClose(); + } }} > {t('submit')}