11import { chain , useId } from '@react-aria/utils' ;
2- import { Key , ReactNode , forwardRef , useEffect , useState } from 'react' ;
2+ import { Key , ReactNode , forwardRef , useCallback , useEffect } from 'react' ;
33
44import { HelperText } from '../HelperText' ;
55
66import { BaseTokenInput , BaseTokenInputProps } from './BaseTokenInput' ;
77import { TokenInputBalance } from './TokenInputBalance' ;
8- import { TokenSelect , TokenSelectProps } from './TokenSelect' ;
8+ import { TokenData , TokenSelect , TokenSelectProps } from './TokenSelect' ;
99
1010type Props = {
1111 balance ?: string ;
1212 humanBalance ?: string | number ;
1313 balanceLabel ?: ReactNode ;
14+ currency ?: any ;
15+ items ?: TokenData [ ] ;
1416 onClickBalance ?: ( balance : string ) => void ;
15- selectProps : Omit < TokenSelectProps , 'label' | 'helperTextId' > ;
17+ onChangeCurrency ?: ( currency ?: any ) => void ;
18+ selectProps ?: Omit < TokenSelectProps , 'label' | 'helperTextId' | 'items' > ;
1619} ;
1720
1821type InheritAttrs = Omit < BaseTokenInputProps , keyof Props | 'endAdornment' > ;
@@ -31,25 +34,33 @@ const SelectableTokenInput = forwardRef<HTMLInputElement, SelectableTokenInputPr
3134 isDisabled,
3235 balanceLabel,
3336 humanBalance,
37+ currency,
38+ items,
3439 onClickBalance,
40+ onChangeCurrency,
3541 size,
3642 ...props
3743 } ,
3844 ref
3945 ) : JSX . Element => {
4046 const selectHelperTextId = useId ( ) ;
4147
42- const [ ticker , setTicker ] = useState < string | undefined > ( selectProps ?. defaultValue ?. toString ( ) ) ;
43-
4448 useEffect ( ( ) => {
45- const value = selectProps ?. value ;
49+ if ( selectProps ?. value === undefined ) return ;
50+
51+ const item = ( items as TokenData [ ] ) . find ( ( item ) => item . currency . symbol === selectProps ?. value ) ;
4652
47- if ( value === undefined ) return ;
53+ onChangeCurrency ?.( item ?. currency ) ;
54+ } , [ selectProps ?. value , onChangeCurrency ] ) ;
4855
49- setTicker ( value . toString ( ) ) ;
50- } , [ selectProps ?. value ] ) ;
56+ const handleSelectionChange = useCallback (
57+ ( ticker : Key ) => {
58+ const tokenData = ( items as TokenData [ ] ) . find ( ( item ) => item . currency . symbol === ticker ) ;
5159
52- const handleTokenChange = ( ticker : Key ) => setTicker ( ticker as string ) ;
60+ onChangeCurrency ?.( tokenData ?. currency ) ;
61+ } ,
62+ [ selectProps ]
63+ ) ;
5364
5465 // Prioritise Number Input description and error message
5566 const hasNumberFieldMessages = ! ! ( errorMessage || description ) ;
@@ -58,7 +69,7 @@ const SelectableTokenInput = forwardRef<HTMLInputElement, SelectableTokenInputPr
5869
5970 const isInvalid = ! hasNumberFieldMessages && ! ! selectProps ?. errorMessage ;
6071
61- const { onSelectionChange, ...restSelectProps } = selectProps ;
72+ const { onSelectionChange, ...restSelectProps } = selectProps || { } ;
6273
6374 const endAdornment = (
6475 < TokenSelect
@@ -67,18 +78,19 @@ const SelectableTokenInput = forwardRef<HTMLInputElement, SelectableTokenInputPr
6778 description = { undefined }
6879 errorMessage = { undefined }
6980 isInvalid = { isInvalid }
81+ items = { items }
7082 size = { size }
71- value = { ticker }
72- onSelectionChange = { chain ( onSelectionChange , handleTokenChange ) }
83+ value = { currency ?. symbol }
84+ onSelectionChange = { chain ( onSelectionChange , handleSelectionChange ) }
7385 />
7486 ) ;
7587
7688 const balance = balanceProp !== undefined && (
7789 < TokenInputBalance
78- balance = { ticker ? balanceProp : '0' }
90+ balance = { currency ? balanceProp : '0' }
7991 balanceHuman = { humanBalance }
8092 inputId = { id }
81- isDisabled = { isDisabled || ! ticker }
93+ isDisabled = { isDisabled || ! currency }
8294 label = { balanceLabel }
8395 onClickBalance = { onClickBalance }
8496 />
@@ -88,6 +100,7 @@ const SelectableTokenInput = forwardRef<HTMLInputElement, SelectableTokenInputPr
88100 < BaseTokenInput
89101 ref = { ref }
90102 balance = { balance }
103+ currency = { currency }
91104 description = { description }
92105 endAdornment = { endAdornment }
93106 errorMessage = { errorMessage }
0 commit comments