1+ import { valueToBigNumber } from '@aave/math-utils' ;
12import { isAddress } from '@ethersproject/address' ;
23import { formatUnits } from '@ethersproject/units' ;
34import { ExclamationIcon } from '@heroicons/react/outline' ;
@@ -12,7 +13,10 @@ import {
1213 InputBase ,
1314 ListItemText ,
1415 MenuItem ,
16+ Popover ,
1517 SvgIcon ,
18+ ToggleButton ,
19+ ToggleButtonGroup ,
1620 Typography ,
1721 useTheme ,
1822} from '@mui/material' ;
@@ -530,10 +534,20 @@ export const SwitchAssetInput = ({
530534 sx = { { ml : 1 } }
531535 />
532536 </ Typography >
537+ { ! disableInput && (
538+ < PercentSelector
539+ disabled = { disabled || Number ( selectedAsset . balance ) === 0 }
540+ onSelectPercent = { ( fraction ) => {
541+ const maxBase = forcedMaxValue || selectedAsset . balance || '0' ;
542+ const next = valueToBigNumber ( maxBase ) . multipliedBy ( fraction ) . toString ( ) ;
543+ onChange && onChange ( next ) ;
544+ } }
545+ />
546+ ) }
533547 { ! disableInput && (
534548 < Button
535549 size = "small"
536- sx = { { minWidth : 0 , ml : '7px ' , p : 0 } }
550+ sx = { { minWidth : 0 , ml : '1px ' , pt : 0 , pb : 0 , mr : '-5px' } }
537551 onClick = { ( ) => {
538552 onChange && onChange ( forcedMaxValue || '-1' ) ;
539553 } }
@@ -555,3 +569,85 @@ export const SwitchAssetInput = ({
555569 </ Box >
556570 ) ;
557571} ;
572+
573+ const PercentSelector = ( {
574+ disabled,
575+ onSelectPercent,
576+ } : {
577+ disabled ?: boolean ;
578+ onSelectPercent : ( fraction : number ) => void ;
579+ } ) => {
580+ const [ anchorEl , setAnchorEl ] = useState < null | HTMLElement > ( null ) ;
581+ const open = Boolean ( anchorEl ) ;
582+
583+ const handleOpen = ( event : React . MouseEvent < HTMLButtonElement > ) => {
584+ if ( disabled ) return ;
585+ setAnchorEl ( event . currentTarget ) ;
586+ } ;
587+ const handleClose = ( ) => setAnchorEl ( null ) ;
588+
589+ const handlePick = ( fraction : number ) => {
590+ onSelectPercent ( fraction ) ;
591+ handleClose ( ) ;
592+ } ;
593+
594+ return (
595+ < >
596+ < Button
597+ size = "small"
598+ sx = { { minWidth : 0 , ml : '6px' , py : 0 , fontSize : '12px' } }
599+ onClick = { handleOpen }
600+ disabled = { disabled }
601+ >
602+ < Trans > %</ Trans >
603+ </ Button >
604+ < Popover
605+ open = { open }
606+ anchorEl = { anchorEl }
607+ onClose = { handleClose }
608+ anchorOrigin = { { vertical : 'top' , horizontal : 'left' } }
609+ transformOrigin = { { vertical : 'bottom' , horizontal : 'left' } }
610+ PaperProps = { {
611+ sx : {
612+ p : 1 ,
613+ backgroundColor : 'background.surface' ,
614+ border : '1px solid' ,
615+ borderColor : 'divider' ,
616+ } ,
617+ } }
618+ >
619+ < Box sx = { { display : 'flex' , alignItems : 'center' , p : 0.5 } } >
620+ < ToggleButtonGroup
621+ exclusive
622+ sx = { {
623+ backgroundColor : 'background.surface' ,
624+ borderRadius : '6px' ,
625+ borderColor : 'background.surface' ,
626+ } }
627+ onChange = { ( _ , v ) => v && handlePick ( v ) }
628+ >
629+ { [ 0.25 , 0.5 , 0.75 ] . map ( ( fraction ) => (
630+ < ToggleButton
631+ key = { fraction }
632+ value = { fraction }
633+ sx = { {
634+ borderRadius : 1 ,
635+ py : 0.5 ,
636+ px : 1 ,
637+ borderWidth : 2 ,
638+ '&.Mui-selected' : {
639+ backgroundColor : 'background.paper' ,
640+ } ,
641+ } }
642+ >
643+ < Typography variant = "subheader2" color = "primary.main" >
644+ { Math . round ( fraction * 100 ) } %
645+ </ Typography >
646+ </ ToggleButton >
647+ ) ) }
648+ </ ToggleButtonGroup >
649+ </ Box >
650+ </ Popover >
651+ </ >
652+ ) ;
653+ } ;
0 commit comments