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' ;
@@ -64,6 +68,7 @@ export interface AssetInputProps {
6468 usdValue : string ;
6569 chainId : number ;
6670 onChange ?: ( value : string ) => void ;
71+ onClear ?: ( ) => void ;
6772 disabled ?: boolean ;
6873 disableInput ?: boolean ;
6974 onSelect ?: ( asset : SwappableToken ) => void ;
@@ -84,6 +89,7 @@ export const SwitchAssetInput = ({
8489 value,
8590 usdValue,
8691 onChange,
92+ onClear,
8793 disabled,
8894 disableInput,
8995 onSelect,
@@ -281,7 +287,11 @@ export const SwitchAssetInput = ({
281287 } ,
282288 } }
283289 onClick = { ( ) => {
284- onChange && onChange ( '' ) ;
290+ if ( onClear ) {
291+ onClear ( ) ;
292+ } else {
293+ onChange && onChange ( '' ) ;
294+ }
285295 } }
286296 disabled = { disabled }
287297 >
@@ -530,10 +540,20 @@ export const SwitchAssetInput = ({
530540 sx = { { ml : 1 } }
531541 />
532542 </ Typography >
543+ { ! disableInput && (
544+ < PercentSelector
545+ disabled = { disabled || Number ( selectedAsset . balance ) === 0 }
546+ onSelectPercent = { ( fraction ) => {
547+ const maxBase = forcedMaxValue || selectedAsset . balance || '0' ;
548+ const next = valueToBigNumber ( maxBase ) . multipliedBy ( fraction ) . toString ( ) ;
549+ onChange && onChange ( next ) ;
550+ } }
551+ />
552+ ) }
533553 { ! disableInput && (
534554 < Button
535555 size = "small"
536- sx = { { minWidth : 0 , ml : '7px ' , p : 0 } }
556+ sx = { { minWidth : 0 , ml : '1px ' , pt : 0 , pb : 0 , mr : '-5px' } }
537557 onClick = { ( ) => {
538558 onChange && onChange ( forcedMaxValue || '-1' ) ;
539559 } }
@@ -555,3 +575,85 @@ export const SwitchAssetInput = ({
555575 </ Box >
556576 ) ;
557577} ;
578+
579+ const PercentSelector = ( {
580+ disabled,
581+ onSelectPercent,
582+ } : {
583+ disabled ?: boolean ;
584+ onSelectPercent : ( fraction : number ) => void ;
585+ } ) => {
586+ const [ anchorEl , setAnchorEl ] = useState < null | HTMLElement > ( null ) ;
587+ const open = Boolean ( anchorEl ) ;
588+
589+ const handleOpen = ( event : React . MouseEvent < HTMLButtonElement > ) => {
590+ if ( disabled ) return ;
591+ setAnchorEl ( event . currentTarget ) ;
592+ } ;
593+ const handleClose = ( ) => setAnchorEl ( null ) ;
594+
595+ const handlePick = ( fraction : number ) => {
596+ onSelectPercent ( fraction ) ;
597+ handleClose ( ) ;
598+ } ;
599+
600+ return (
601+ < >
602+ < Button
603+ size = "small"
604+ sx = { { minWidth : 0 , ml : '6px' , py : 0 , fontSize : '12px' } }
605+ onClick = { handleOpen }
606+ disabled = { disabled }
607+ >
608+ < Trans > %</ Trans >
609+ </ Button >
610+ < Popover
611+ open = { open }
612+ anchorEl = { anchorEl }
613+ onClose = { handleClose }
614+ anchorOrigin = { { vertical : 'top' , horizontal : 'left' } }
615+ transformOrigin = { { vertical : 'bottom' , horizontal : 'left' } }
616+ PaperProps = { {
617+ sx : {
618+ p : 1 ,
619+ backgroundColor : 'background.surface' ,
620+ border : '1px solid' ,
621+ borderColor : 'divider' ,
622+ } ,
623+ } }
624+ >
625+ < Box sx = { { display : 'flex' , alignItems : 'center' , p : 0.5 } } >
626+ < ToggleButtonGroup
627+ exclusive
628+ sx = { {
629+ backgroundColor : 'background.surface' ,
630+ borderRadius : '6px' ,
631+ borderColor : 'background.surface' ,
632+ } }
633+ onChange = { ( _ , v ) => v && handlePick ( v ) }
634+ >
635+ { [ 0.25 , 0.5 , 0.75 ] . map ( ( fraction ) => (
636+ < ToggleButton
637+ key = { fraction }
638+ value = { fraction }
639+ sx = { {
640+ borderRadius : 1 ,
641+ py : 0.5 ,
642+ px : 1 ,
643+ borderWidth : 2 ,
644+ '&.Mui-selected' : {
645+ backgroundColor : 'background.paper' ,
646+ } ,
647+ } }
648+ >
649+ < Typography variant = "subheader2" color = "primary.main" >
650+ { Math . round ( fraction * 100 ) } %
651+ </ Typography >
652+ </ ToggleButton >
653+ ) ) }
654+ </ ToggleButtonGroup >
655+ </ Box >
656+ </ Popover >
657+ </ >
658+ ) ;
659+ } ;
0 commit comments