Skip to content

Commit 1e924c9

Browse files
authored
Merge pull request #19 from lob/verification-result
Address forms clean up input values after verification
2 parents 9227090 + 4ec10a6 commit 1e924c9

File tree

8 files changed

+142
-65
lines changed

8 files changed

+142
-65
lines changed

example/src/AddressFormDemo.js

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// External Dependencies
22
import React, { useState } from 'react';
3-
import { AddressForm, verify } from '@lob/react-address-autocomplete'
3+
import { AddressForm } from '@lob/react-address-autocomplete'
44

55
// Internal Dependencies
66
import VerificationResult from './VerificationResult'
@@ -25,10 +25,9 @@ const AddressFormDemo = ({ apiKey }) => {
2525
resetApiResult()
2626
}
2727

28-
const handleSubmit = () =>
28+
const handleSubmit = verificationResult =>
2929
resetApiResult()
30-
.then(() => verify(apiKey, selectedResult.value))
31-
.then(res => setVerificationResult(res))
30+
.then(() => setVerificationResult(verificationResult))
3231
.catch(err => setErrorMessage(err.message))
3332

3433
return (
@@ -38,13 +37,12 @@ const AddressFormDemo = ({ apiKey }) => {
3837
apiKey={apiKey}
3938
onFieldChange={handleChange}
4039
onSelection={handleSelect}
40+
onSubmit={handleSubmit}
41+
submitLabel="Verify"
42+
styles={{
43+
'lob-submit': { width: '100%' }
44+
}}
4145
/>
42-
<button
43-
onClick={handleSubmit}
44-
style={{ width: '100%' }}
45-
>
46-
Verify
47-
</button>
4846
<VerificationResult apiResponse={verificationResult} error={errorMessage} />
4947
</div>
5048
);

example/src/App.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import AddressFormDemo from './AddressFormDemo'
66
// import DomesticDemo from './DomesticDemo'
77
// import InternationalDemo from './InternationalDemo'
88

9-
const API_KEY = 'live_pub_16d30adbb46a1c360ebf7d1e6b48361'
9+
const API_KEY = 'YOUR_API_KEY_HERE'
1010

1111
const App = () => {
1212

@@ -18,8 +18,8 @@ const App = () => {
1818
alt='Lob'
1919
/>
2020
<h1>React Address Autocomplete Demo</h1>
21-
{/* <DomesticDemo apiKey={API_KEY} />
22-
<InternationalDemo apiKey={API_KEY} /> */}
21+
{/* <DomesticDemo apiKey={API_KEY} /> */}
22+
{/* <InternationalDemo apiKey={API_KEY} /> */}
2323
<AddressFormDemo apiKey={API_KEY} />
2424
</div>
2525
);

package-lock.json

Lines changed: 2 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@lob/react-address-autocomplete",
3-
"version": "2.1.1",
3+
"version": "2.1.2",
44
"description": "A collection of components and utility functions for verifying and suggesting addresses using Lob",
55
"author": "Lob",
66
"license": "MIT",
@@ -42,7 +42,6 @@
4242
"cross-env": "^7.0.2",
4343
"eslint": "^8.14.0",
4444
"eslint-config-prettier": "^8.3.0",
45-
"eslint-config-react-app": "^7.0.1",
4645
"eslint-config-standard-react": "^11.0.1",
4746
"eslint-plugin-prettier": "^4.0.0",
4847
"eslint-plugin-promise": "^5.1.0",

src/AddressForm/AddressForm.js

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import React, { useState } from 'react'
55

66
// Internal Dependencies
7+
import { verify } from '../verify'
78
import Autocomplete from '../Autocomplete'
89
import useMergedStyles from './useMergedStyles'
910

@@ -22,12 +23,15 @@ const customStyles = {
2223
/**
2324
* Similar to Autocomplete except each address component is given its own input. Autocomplete
2425
* occurs on the primary line but the results are inserted into each component.
25-
* @param {string} apiKey - Public API key to your Lob account.
26-
* @param {onSelection?} onSelection -
27-
* Callback function when the select component changes.
28-
* @param {onInputChange?} onInputChange -
29-
* Callback function when any input value changes. Includes both the event object and address form.
30-
* Use event.target.id to determine which component is being updated.
26+
* @param {String} apiKey - Public API key to your Lob account.
27+
* @param {Array?} children - These elements get rendered between the address form inputs and
28+
* submit button
29+
* @param {Boolean} hideSubmitButton - Hides the submit button and its behavior
30+
* @param {Function} onInputChange - Callback when any input value changes. Includes both the event
31+
* object and address form. Use event.target.id to determine which component is being updated.
32+
* @param {Function} onSelection - Callback when the select component changes.
33+
* @param {Function} onSubmit - Callback after the submit button is clicked and the form inputs
34+
* are updated. Passes the verification result from the API.
3135
* @param {Object} styles - Override the default styles by providing an object similar to the
3236
* styling framework used by react-select. Each key corresponds to a component and maps to a
3337
* function that returns the new styles.lob_ Here is an example:
@@ -43,14 +47,20 @@ const customStyles = {
4347
* - lob_input
4448
* - lob_label
4549
* - lob_row
50+
* - lob_submit
4651
*
4752
* For more details visit https://react-select.com/styles
53+
* @param {String} submitButtonLabel
4854
*/
4955
const AddressForm = ({
5056
apiKey,
57+
children,
58+
hideSubmitButton = false,
5159
onFieldChange = () => {},
5260
onSelection = () => {},
61+
onSubmit = () => {},
5362
styles = {},
63+
submitButtonLabel = 'Submit',
5464
...additionalProps
5565
}) => {
5666
const [form, setForm] = useState(defaultForm)
@@ -98,6 +108,23 @@ const AddressForm = ({
98108
})
99109
}
100110

111+
const handleSubmit = () =>
112+
verify(apiKey, form).then((verificationResult) => {
113+
const {
114+
primary_line,
115+
secondary_line,
116+
components: { city, state, zip_code, zip_code_plus_4 }
117+
} = verificationResult
118+
setForm({
119+
primary_line,
120+
secondary_line,
121+
city,
122+
state,
123+
zip_code: `${zip_code}-${zip_code_plus_4}`
124+
})
125+
onSubmit(verificationResult)
126+
})
127+
101128
const mergedStyles = useMergedStyles(styles, false /* isInternational */)
102129

103130
return (
@@ -163,6 +190,12 @@ const AddressForm = ({
163190
value={zip_code}
164191
/>
165192
</div>
193+
{children}
194+
{!hideSubmitButton && (
195+
<button onClick={handleSubmit} style={mergedStyles.lob_submit}>
196+
{submitButtonLabel}
197+
</button>
198+
)}
166199
</div>
167200
)
168201
}

src/AddressForm/AddressFormInternational.js

Lines changed: 58 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import React, { useState } from 'react'
55

66
// Internal Dependencies
7+
import { verifyInternational } from '../verify'
78
import CountrySelect from '../CountrySelect'
89
import useMergedStyles from './useMergedStyles'
910

@@ -12,15 +13,20 @@ const defaultForm = {
1213
secondary_line: '',
1314
city: '',
1415
state: '',
15-
zip_code: ''
16+
postal_code: ''
1617
}
1718

1819
/**
1920
* Renders a group of inputs for each address component including a select input for country code.
2021
* Does not perform any address autocompletion.
21-
* @param {onInputChange?} onInputChange -
22-
* Callback function when any input value changes. Use e.target.id to determine which component
23-
* is being updated.
22+
* @param {String?} apiKey - Public API key to your Lob account.
23+
* @param {Array?} children - These elements get rendered between the address form inputs and
24+
* submit button
25+
* @param {Boolean} hideSubmitButton - Hides the submit button and its behavior
26+
* @param {Function?} onFieldChange - Callback when any input value changes. Use e.target.id
27+
* to determine which component is being updated.
28+
* @param {Function?} onSubmit - Callback after the submit button is clicked and the form inputs
29+
* are updated. Passes the verification result from the API.
2430
* @param {Object} styles - Override the default styles by providing an object similar to the
2531
* styling framework used by react-select. Each key corresponds to a component and maps to a
2632
* function that returns the new styles.lob_ Here is an example:
@@ -38,19 +44,56 @@ const defaultForm = {
3844
* - lob_row
3945
*
4046
* For more details visit https://react-select.com/styles
47+
* @param {String} submitButtonLabel
4148
*/
4249
const AddressFormInternational = ({
50+
apiKey = null,
51+
children,
52+
hideSubmitButton = false,
4353
onFieldChange = () => {},
44-
styles = {}
54+
onSubmit = () => {},
55+
styles = {},
56+
submitButtonLabel = 'Submit'
4557
}) => {
4658
const [form, setForm] = useState(defaultForm)
47-
const { primary_line, secondary_line, city, state, zip_code, country } = form
59+
const { primary_line, secondary_line, city, state, postal_code, country } =
60+
form
4861

4962
const handleChange = (e) => {
5063
setForm({ ...form, [e.target.id]: e.target.value })
5164
onFieldChange(e)
5265
}
5366

67+
const handleSubmit = () => {
68+
// apiKey wasn't used is previous versions of AddressFormInternational so we made the prop
69+
// optional to not introduce a breaking change.
70+
if (!apiKey) {
71+
console.error(
72+
'[@lob/react-address-autocomplete] ' +
73+
'AddressFormInternational requires props apiKey for verifications'
74+
)
75+
return
76+
}
77+
78+
verifyInternational(apiKey, form, form.country).then(
79+
(verificationResult) => {
80+
const {
81+
primary_line,
82+
secondary_line,
83+
components: { city, state, postal_code }
84+
} = verificationResult
85+
setForm({
86+
primary_line,
87+
secondary_line,
88+
city,
89+
state,
90+
postal_code
91+
})
92+
onSubmit(verificationResult)
93+
}
94+
)
95+
}
96+
5497
const mergedStyles = useMergedStyles(styles, true /* isInternational */)
5598

5699
return (
@@ -100,14 +143,14 @@ const AddressFormInternational = ({
100143
/>
101144
</div>
102145
<div style={mergedStyles.lob_row}>
103-
<label style={mergedStyles.lob_label} htmlFor='zip_code'>
146+
<label style={mergedStyles.lob_label} htmlFor='postal_code'>
104147
Zip / Postal Code
105148
</label>
106149
<input
107150
style={{ ...mergedStyles.lob_input }}
108-
id='zip_code'
151+
id='postal_code'
109152
onChange={handleChange}
110-
value={zip_code}
153+
value={postal_code}
111154
/>
112155
</div>
113156
<div style={mergedStyles.lob_row}>
@@ -121,6 +164,12 @@ const AddressFormInternational = ({
121164
value={country}
122165
/>
123166
</div>
167+
{children}
168+
{!hideSubmitButton && (
169+
<button onClick={handleSubmit} style={mergedStyles.lob_submit}>
170+
{submitButtonLabel}
171+
</button>
172+
)}
124173
</div>
125174
)
126175
}

src/api.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ export const postVerifyInternationalAddress = (
4848
address,
4949
countryCode
5050
) => {
51+
const payload = typeof address === 'string' ? { address } : address
5152
const url = new URL('https://api.lob.com/v1/intl_verifications')
5253
url.searchParams.append('av_integration_origin', window.location.href)
5354
url.searchParams.append('integration', 'react-address-autocomplete')
@@ -57,7 +58,7 @@ export const postVerifyInternationalAddress = (
5758
Authorization: `Basic ${base64.encode(apiKey + ':')}`,
5859
'Content-Type': 'application/json'
5960
},
60-
body: JSON.stringify({ address, country: countryCode })
61+
body: JSON.stringify({ ...payload, country: countryCode })
6162
}
6263

6364
return fetch(url, init)

0 commit comments

Comments
 (0)