1
- import { useEffect , useRef , useState } from 'react'
1
+ import { useEffect , useRef , useState , useCallback } from 'react'
2
2
import { Box , Flex , Input , Text } from 'theme-ui'
3
3
import FlexCol from '../../flex-col'
4
4
import AutofillColourFix from './autofill-colour-fix'
@@ -31,13 +31,13 @@ const approvedCountries = [
31
31
32
32
export default function AutoComplete ( { name, isPersonalAddressInput } ) {
33
33
const input = useRef ( )
34
- const base = useRef ( )
35
34
const [ predictions , setPredictions ] = useState ( null )
36
35
const [ countryCode , setCountryCode ] = useState ( null )
37
36
38
37
const optionClicked = async prediction => {
39
38
input . current . value = prediction . name
40
- await onInput ( prediction . name )
39
+ // Needs to match the shape of the event object because onInput takes an event object.
40
+ await onInput ( { target : { value : prediction . name } } )
41
41
setPredictions ( null )
42
42
}
43
43
const clickOutside = e => {
@@ -46,26 +46,28 @@ export default function AutoComplete({ name, isPersonalAddressInput }) {
46
46
}
47
47
}
48
48
49
- const onInput = async value => {
50
- setPredictions ( value ? ( await search ( value ) ) . results : null )
49
+ const onInput = useCallback (
50
+ async e => {
51
+ if ( ! e . target . value ) return
52
+ const value = e . target . value
51
53
52
- if ( isPersonalAddressInput ) return
53
- geocode ( value )
54
- . then ( res => {
55
- const country = res ?. results [ 0 ] ?. country
56
- const countryCode = res ?. results [ 0 ] ?. countryCode
54
+ setPredictions ( value ? ( await search ( value ) ) . results : null )
57
55
58
- setCountryCode ( countryCode )
56
+ if ( isPersonalAddressInput ) return
57
+ geocode ( value )
58
+ . then ( res => {
59
+ const country = res ?. results [ 0 ] ?. country
60
+ const countryCode = res ?. results [ 0 ] ?. countryCode
59
61
60
- sessionStorage . setItem ( 'bank-signup-eventCountry' , country )
61
- sessionStorage . setItem ( 'bank-signup-eventCountryCode' , countryCode )
62
- } )
63
- . catch ( err => console . error ( err ) )
64
- }
62
+ setCountryCode ( countryCode )
65
63
66
- const onInputWrapper = async e => {
67
- if ( e . target . value ) await onInput ( e . target . value )
68
- }
64
+ sessionStorage . setItem ( 'bank-signup-eventCountry' , country )
65
+ sessionStorage . setItem ( 'bank-signup-eventCountryCode' , countryCode )
66
+ } )
67
+ . catch ( err => console . error ( err ) )
68
+ } ,
69
+ [ isPersonalAddressInput ]
70
+ )
69
71
70
72
//TODO: Close suggestions view when focus is lost via tabbing.
71
73
//TODO: Navigate suggestions with arrow keys.
@@ -75,15 +77,15 @@ export default function AutoComplete({ name, isPersonalAddressInput }) {
75
77
if ( ! inputEl ) return
76
78
77
79
document . addEventListener ( 'click' , clickOutside )
78
- inputEl . addEventListener ( 'input' , onInputWrapper )
79
- inputEl . addEventListener ( 'focus' , onInputWrapper )
80
+ inputEl . addEventListener ( 'input' , onInput )
81
+ inputEl . addEventListener ( 'focus' , onInput )
80
82
81
83
return ( ) => {
82
84
document . removeEventListener ( 'click' , clickOutside )
83
- inputEl . removeEventListener ( 'input' , onInputWrapper )
84
- inputEl . removeEventListener ( 'focus' , onInputWrapper )
85
+ inputEl . removeEventListener ( 'input' , onInput )
86
+ inputEl . removeEventListener ( 'focus' , onInput )
85
87
}
86
- } , [ ] )
88
+ } , [ onInput ] )
87
89
88
90
return (
89
91
< Box sx = { { position : 'relative' , width : '100%' } } >
0 commit comments