The AFD Software React plugin allows for easy integration with the AFD Software Postcode Evolution Validation API. The plugin provides a collection of components and hooks that can be added to any React project and with a small amount of configuration which can enable address validation on input on any form, as well as email address and phone number validation.
The components are designed to be as flexible as possible, allowing for easy integration with any form library or custom form implementation, with both controlled and uncontrolled forms supported.
npm install --save @afd-software/pce-react
Below are simple examples that demonstrate how to populate a Postcode
field. The full list of available PCE fields can be found in the documentation for the AFD JSON API.
For full examples of how to use the plugin, please see the Storybook
import React, { useEffect, useState } from 'react'
import { AFDTypeahead, useAFDAddressValidation, AddressField } from '@afd-software/pce-react'
import type { AFDTypeaheadOptions } from '@afd-software/pce-react'
const AddressForm: React.FC = () => {
const afdOptions: AFDTypeaheadOptions = {
// if you don't yet have credentials please contact support@afd.co.uk or call 01624 811712
id: 'XXXX',
token: 'XXXX',
placeholder: 'Search for address'
}
// state object for postcode
const [street, setStreet] = useState('')
const [town, setTown] = useState('')
const [postcode, setPostcode] = useState('')
// get the relevant functions for the useAFDAddressValidation hook
const { getAFDTypeaheadProps, selectedAddress, fieldVisible } =
useAFDAddressValidation(afdOptions, 'GB')
// monitor for changes to the selected address
useEffect(() => {
if (!selectedAddress.Street) return
setStreet(selectedAddress.Street)
}, [selectedAddress])
useEffect(() => {
if (!selectedAddress.Town) return
setTown(selectedAddress.Town)
}, [selectedAddress])
useEffect(() => {
if (!selectedAddress.Postcode) return
setPostcode(selectedAddress.Postcode)
}, [selectedAddress])
return (
<>
<AFDTypeahead {...getAFDTypeaheadProps()} />
<AddressField visible={fieldVisible('Street')}>
<input
type="text"
value={street}
onChange={(e) => setPostcode(e.target.value)}
/>
</AddressField>
<AddressField visible={fieldVisible('Town')}>
<input
type="text"
value={town}
onChange={(e) => setTown(e.target.value)}
/>
</AddressField>
<AddressField visible={fieldVisible('Postcode')}>
<input
type="text"
value={postcode}
onChange={(e) => setPostcode(e.target.value)}
/>
</AddressField>
</>
)
}
export default AddressForm
import React, { Component } from 'react'
import { AFDTypeahead, useAFDAddressValidation } from '@afd-software/pce-react'
import { AFDTypeaheadOptions } from './AFDOptions'
const AddressForm: React.FC = () => {
const afdOptions: AFDTypeaheadOptions = {
// if you don't yet have credentials please contact support@afd.co.uk or call 01624 811712
id: 'XXXX',
token: 'XXXX',
placeholder: 'Search for address'
}
// ref for the postcode field
const postcodeRef = useRef(null)
useEffect(() => {
registerUncontrolledField('Postcode')
}, []);
// get the relevant functions for the useAddressValidation hook
const { getAFDTypeaheadProps, registerUncontrolledField, fieldVisible } =
useAddressValidation(afdOptions, 'GB')
return (
<>
<AFDTypeahead {...getAFDTypeaheadProps()} />
<AddressField visible={fieldVisible('Postcode')}>
<input
type="text"
ref={postcodeRef}
/>
</AddressField>
</>
)
}