@ -6,12 +6,29 @@ import ButtonGroup from '../../../components/ui/button-group';
import Button from '../../../components/ui/button' ;
import InfoTooltip from '../../../components/ui/info-tooltip' ;
export default function SlippageButtons ( { onSelect , maxAllowedSlippage } ) {
export default function SlippageButtons ( {
onSelect ,
maxAllowedSlippage ,
currentSlippage ,
} ) {
const t = useContext ( I18nContext ) ;
const [ open , setOpen ] = useState ( false ) ;
const [ customValue , setCustomValue ] = useState ( '' ) ;
const [ customValue , setCustomValue ] = useState ( ( ) => {
if ( currentSlippage && currentSlippage !== 2 && currentSlippage !== 3 ) {
return currentSlippage ;
}
return '' ;
} ) ;
const [ enteringCustomValue , setEnteringCustomValue ] = useState ( false ) ;
const [ activeButtonIndex , setActiveButtonIndex ] = useState ( 1 ) ;
const [ activeButtonIndex , setActiveButtonIndex ] = useState ( ( ) => {
if ( currentSlippage === 3 ) {
return 1 ;
} else if ( currentSlippage === 2 ) {
return 0 ;
} else if ( currentSlippage ) {
return 2 ;
}
return 1 ; // Choose activeButtonIndex = 1 for 3% slippage by default.
} ) ;
const [ inputRef , setInputRef ] = useState ( null ) ;
let errorText = '' ;
@ -44,110 +61,98 @@ export default function SlippageButtons({ onSelect, maxAllowedSlippage }) {
return (
< div className = "slippage-buttons" >
< button
onClick = { ( ) => setOpen ( ! open ) }
className = { classnames ( 'slippage-buttons__header' , {
'slippage-buttons__header--open' : open ,
} ) }
>
< div className = "slippage-buttons__header" >
< div className = "slippage-buttons__header-text" >
{ t ( 'swapsAdvancedOptions' ) }
< / d i v >
{ open ? (
< i className = "fa fa-angle-up" / >
) : (
< i className = "fa fa-angle-down" / >
) }
< / b u t t o n >
< / d i v >
< div className = "slippage-buttons__content" >
{ open && (
< div className = "slippage-buttons__dropdown-content" >
< div className = "slippage-buttons__buttons-prefix" >
< div className = "slippage-buttons__prefix-text" >
{ t ( 'swapsMaxSlippage' ) }
< / d i v >
< InfoTooltip
position = "top"
contentText = { t ( 'swapAdvancedSlippageInfo' ) }
/ >
< div className = "slippage-buttons__dropdown-content" >
< div className = "slippage-buttons__buttons-prefix" >
< div className = "slippage-buttons__prefix-text" >
{ t ( 'swapsMaxSlippage' ) }
< / d i v >
< ButtonGroup
defaultActiveButtonIndex = {
activeButtonIndex === 2 && ! customValue ? 1 : activeButtonIndex
}
variant = "radiogroup"
newActiveButtonIndex = { activeButtonIndex }
< InfoTooltip
position = "top"
contentText = { t ( 'swapAdvancedSlippageInfo' ) }
/ >
< / d i v >
< ButtonGroup
defaultActiveButtonIndex = {
activeButtonIndex === 2 && ! customValue ? 1 : activeButtonIndex
}
variant = "radiogroup"
newActiveButtonIndex = { activeButtonIndex }
className = { classnames (
'button-group' ,
'slippage-buttons__button-group' ,
) }
>
< Button
onClick = { ( ) => {
setCustomValue ( '' ) ;
setEnteringCustomValue ( false ) ;
setActiveButtonIndex ( 0 ) ;
onSelect ( 2 ) ;
} }
>
2 %
< / B u t t o n >
< Button
onClick = { ( ) => {
setCustomValue ( '' ) ;
setEnteringCustomValue ( false ) ;
setActiveButtonIndex ( 1 ) ;
onSelect ( 3 ) ;
} }
>
3 %
< / B u t t o n >
< Button
className = { classnames (
'button-group' ,
'slippage-buttons__button-group' ,
'slippage-buttons__button-group-custom-button' ,
{
'radio-button--danger' : errorText ,
} ,
) }
onClick = { ( ) => {
setActiveButtonIndex ( 2 ) ;
setEnteringCustomValue ( true ) ;
} }
>
< Button
onClick = { ( ) => {
setCustomValue ( '' ) ;
setEnteringCustomValue ( false ) ;
setActiveButtonIndex ( 0 ) ;
onSelect ( 1 ) ;
} }
>
1 %
< / B u t t o n >
< Button
onClick = { ( ) => {
setCustomValue ( '' ) ;
setEnteringCustomValue ( false ) ;
setActiveButtonIndex ( 1 ) ;
onSelect ( 2 ) ;
} }
>
2 %
< / B u t t o n >
< Button
className = { classnames (
'slippage-buttons__button-group-custom-button' ,
{
'radio-button--danger' : errorText ,
} ,
) }
onClick = { ( ) => {
setActiveButtonIndex ( 2 ) ;
setEnteringCustomValue ( true ) ;
} }
>
{ enteringCustomValue ? (
< div
className = { classnames ( 'slippage-buttons__custom-input' , {
'slippage-buttons__custom-input--danger' : errorText ,
} ) }
>
< input
onChange = { ( event ) => {
setCustomValue ( event . target . value ) ;
onSelect ( Number ( event . target . value ) ) ;
} }
type = "number"
step = "0.1"
ref = { setInputRef }
onBlur = { ( ) => {
setEnteringCustomValue ( false ) ;
if ( customValue === '0' ) {
setCustomValue ( '' ) ;
setActiveButtonIndex ( 1 ) ;
}
} }
value = { customValue || '' }
/ >
< / d i v >
) : (
customValueText
) }
{ ( customValue || enteringCustomValue ) && (
< div className = "slippage-buttons__percentage-suffix" > % < / d i v >
) }
< / B u t t o n >
< / B u t t o n G r o u p >
< / d i v >
) }
{ enteringCustomValue ? (
< div
className = { classnames ( 'slippage-buttons__custom-input' , {
'slippage-buttons__custom-input--danger' : errorText ,
} ) }
>
< input
onChange = { ( event ) => {
setCustomValue ( event . target . value ) ;
onSelect ( Number ( event . target . value ) ) ;
} }
type = "number"
step = "0.1"
ref = { setInputRef }
onBlur = { ( ) => {
setEnteringCustomValue ( false ) ;
if ( customValue === '0' ) {
setCustomValue ( '' ) ;
setActiveButtonIndex ( 1 ) ;
}
} }
value = { customValue || '' }
/ >
< / d i v >
) : (
customValueText
) }
{ ( customValue || enteringCustomValue ) && (
< div className = "slippage-buttons__percentage-suffix" > % < / d i v >
) }
< / B u t t o n >
< / B u t t o n G r o u p >
< / d i v >
{ errorText && (
< div className = "slippage-buttons__error-text" > { errorText } < / d i v >
) }
@ -159,4 +164,5 @@ export default function SlippageButtons({ onSelect, maxAllowedSlippage }) {
SlippageButtons . propTypes = {
onSelect : PropTypes . func . isRequired ,
maxAllowedSlippage : PropTypes . number . isRequired ,
currentSlippage : PropTypes . number ,
} ;