npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@afd-software/pce-react

v2.0.0

Published

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 wh

Downloads

245

Readme

AFD PCE React Plugin

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.

Install

npm install --save @afd-software/pce-react

Quick Start

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

Address Validation

Controlled Fields

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 [email protected] 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

Uncontrolled Fields

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 [email protected] 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>
    </>
  )

}