Skip to content

Commit c98082d

Browse files
committed
Refactor address input callback system
1 parent 84899cd commit c98082d

File tree

1 file changed

+26
-24
lines changed

1 file changed

+26
-24
lines changed

components/hcb/apply/address-input.js

+26-24
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useEffect, useRef, useState } from 'react'
1+
import { useEffect, useRef, useState, useCallback } from 'react'
22
import { Box, Flex, Input, Text } from 'theme-ui'
33
import FlexCol from '../../flex-col'
44
import AutofillColourFix from './autofill-colour-fix'
@@ -31,13 +31,13 @@ const approvedCountries = [
3131

3232
export default function AutoComplete({ name, isPersonalAddressInput }) {
3333
const input = useRef()
34-
const base = useRef()
3534
const [predictions, setPredictions] = useState(null)
3635
const [countryCode, setCountryCode] = useState(null)
3736

3837
const optionClicked = async prediction => {
3938
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 } })
4141
setPredictions(null)
4242
}
4343
const clickOutside = e => {
@@ -46,26 +46,28 @@ export default function AutoComplete({ name, isPersonalAddressInput }) {
4646
}
4747
}
4848

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
5153

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)
5755

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
5961

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)
6563

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+
)
6971

7072
//TODO: Close suggestions view when focus is lost via tabbing.
7173
//TODO: Navigate suggestions with arrow keys.
@@ -75,15 +77,15 @@ export default function AutoComplete({ name, isPersonalAddressInput }) {
7577
if (!inputEl) return
7678

7779
document.addEventListener('click', clickOutside)
78-
inputEl.addEventListener('input', onInputWrapper)
79-
inputEl.addEventListener('focus', onInputWrapper)
80+
inputEl.addEventListener('input', onInput)
81+
inputEl.addEventListener('focus', onInput)
8082

8183
return () => {
8284
document.removeEventListener('click', clickOutside)
83-
inputEl.removeEventListener('input', onInputWrapper)
84-
inputEl.removeEventListener('focus', onInputWrapper)
85+
inputEl.removeEventListener('input', onInput)
86+
inputEl.removeEventListener('focus', onInput)
8587
}
86-
}, [])
88+
}, [onInput])
8789

8890
return (
8991
<Box sx={{ position: 'relative', width: '100%' }}>

0 commit comments

Comments
 (0)