Socket
Socket
Sign inDemoInstall

@ideal-postcodes/address-finder

Package Overview
Dependencies
8
Maintainers
1
Versions
54
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    @ideal-postcodes/address-finder

Address Finder JS library backed by the Ideal Postcodes UK address search API


Version published
Maintainers
1
Install size
1.42 MB
Created

Readme

Source

Ideal Postcodes Address Finder JavaScript Library

Find and autocomplete addresses with Address Finder

Cross Browser Testing Release codecov Dependency Status

npm version npm bundle size (scoped) npm bundle size (scoped) install size

@ideal-postcodes/address-finder is a JavaScript library that delivers address autocomplete (or adress typeahead) search functionality on a webpage.

This package should be consumed by a bundler or transpiler (e.g. webpack, parcel, rollup) for minification, module resolution and specific browser support.

For a pre-bundled version of address-finder (minified, transpiled, polyfilled, etc) that can immediately be dropped on a webpage, use address-finder-bundled.

Address Finder Demo

Example Setup

Webpack Status

Parcel Status

Rollup Status

React Demo

How it Works

  1. Add the library to your project. Your webpage should have pre-existing address input fields as well as an input field to host the finder
  2. Run initialisation code providing a reference to the Address Finder input and any other configuration
  3. Address Finder binds to the input field of your choice and renders a dropdown of address suggestion when the user starts typing

Documentation

Configuration & Usage

Install

Add address-finder to your project via npm

npm install @ideal-postcodes/address-finder
Instantiate

Instantiate Address Finder with AddressFinder.setup.

import { AddressFinder } from "@ideal-postcodes/address-finder";

const controller = AddressFinder.setup({
  inputField: "#line_1", // Target <input> to host Address Finder
  apiKey: "iddqd",       // API Key from your account
  outputFields: {        // Target address fields
    line_1: "#line_1",
    line_2: "#line_2",
    line_3: "#line_3",
    post_town: "#post_town",
    postcode: "#postcode",
  },
});

Configuration options

It's also possible to pass HTMLElements as inputField or outputFields.

AddressFinder.setup({
  inputField: document.getElementById("line_1"),
  outputFields: {
    line_1: document.getElementById("line_1"),
  },
  apiKey: "iddqd",
});
Detach

After instantiating Address Finder, it is possible to toggle availability with the .detach() and .attach() methods.

// Removes Address Finder from DOM and clears event listeners
controller.view.detach();

// Restores Address Finder to the DOM and start event listeners
controller.view.attach();

Quickstart

A complete list of quick integration examples can be found at ideal-postcodes.co.uk/address-finder-demo

Populating your Address Fields

Address Finder expects you to address inputs on the page ready to be populated.

When an address is select, the addressing data will be piped to those address inputs designated in output_fields. For instance line_1: "#line_1" will write Address Line One data to a HTML entity with ID line_1.

Assigning the 3 address line, post town and postcode fields, is all addressing information required to identify a UK premise. You may extract more data for an address by passing more properties into the output_fields configuration object.

The configuration attributes for outputFields matches the Address response object. E.g. street name can be populated can be populated using the thoroughfare attribute. A list of address attributes provided by the API can be found at @ideal-postcodes/api-typings.

More information on addressing data can be found on our data documentation.

For more sophistated behaviour when a user selects an address, you can hook into the onAddressRetrieved callback and implement customised behaviour there.

Styling

The Address Finder's default CSS stylesheet can be found in the css/ directory of this repository.

The Address Finder view CSS is structured in the following way:

  • The target input and Address Finder suggestion list is wrapped in a <div> with a default class of idpc_autocomplete
  • Address Suggestions are rendered in a <ul> list with a default class of idpc_ul
  • Any messages (including prompts to type, errors, etc) are rendered in a <li> with a default class of idpc_error

Default CSS classes can be overridden. For instance, containerClass, listClass and messageClass are all defined in defaults.

injectStyle

Set injectStyle: true to inject default Address Finder style into DOM. Defaults to false.

Set to a URL (e.g. injectStyle: "https://cdn.jsdelivr.net/npm/@ideal-postcodes/address-finder@1.2.1/css/address-finder.min.css") to retrieve a CSS stylesheet.

Messages

Default messages (e.g. "Start typing to find address", "No matches found") can be overridden. For instance, msgFallback, msgInitial and msgNoMatch.

Setup Options

AddressFinder.setup() accepts a single configuration object and returns the Address Finder controller instance.

Below is the list of parameters you can use to modify Address Finder.

A complete list of configuration options can be found in the library documentation

Required Config

inputField

CSS selector or HTML Element which specifies the <input> field which the Address Finder interface should bind

apiKey

API Key from your Ideal Postcodes account. Typically begins ak_

outputFields

Specify where to send address data given a selected address. A object which maps an address attribute to the CSS selector of an input field or HTMLElement like HTMLInputElement or HTMLTextAreaElement.

{
  line_1: "#line_1",
  line_2: "#line_2",
  line_3: "input[name='line_3']",
  post_town: document.getElementById("post_town"),
  postcode: document.getElementById("postcode")
}

The configuration attributes for outputFields matches the Address response object. E.g. street name can be populated can be populated using the thoroughfare attribute. A list of address attributes provided by the API can be found at @ideal-postcodes/api-typings.

More complex, dynamic assignment can be performed using the onAddressRetrieved callback.

Output fields assigned with a query selector are evaluated lazily (i.e. when an address attribute needs to be piped to a field).

Configure Behaviour

checkKey

If enabled, the plugin will check if the key is in a usable state before initialising itself.

The check can fail if:

  • Your key has no remaining lookups
  • Your key has run into its lookup limit for the day
  • Your key is requested from a URL which is not on your whitelist
  • The user seeking to use the key has exceeded their limit for the day

If the check fails, the plugin will not initialise. You can use the onFailedCheck callback to customise your response to a failed check.

titleizePostTown

An optional field to convert the case of the Post Town from upper case into title case. E.g. "LONDON" becomes "London". Default is true

removeOrganisation

If set to true, organisation name will be removed from the address.

Note that addresses which are exclusively an organisation name will not result in the organisation name being removed as this will result in no premise identifier.

Defaults to false.

Lifecycle Callbacks

Address Finder also provides callbacks which let you hook into specific events in the controller lifecyle.

onLoaded

Invoked when Address Finder has been successfully attached to the input element.

onFailedCheck

A function invoked if checkKey is enabled and the check fails.

onSuggestionsRetrieved

Invoked immediately after address suggestions are retrieved from the API. The first argument is an array of address suggestions.

onAddressSelected

Invoked immediately after the user has selected a suggestion (either by click or keypress). The first argument is an object which represents the suggestion selected.

onAddressRetrieved

Invoked when the Address Finder client has retrieved a full address from the API following a user accepting a suggestion. The first argument is an object representing the address that has been retrieved.

onSearchError

Invoked when an error has occurred following an attempt to retrieve a full address. In this scenario the user will also receive a message to manually input their address.

The first argument is an error instance (i.e. inherits from Error) representing the error which has occurred.

Examples of errors includes "lookup balance exhausted" and "lookup limit reached" errors.

onSuggestError

Invoked when an error has occurred following an attempt to retrieve suggestions for a key press. In this scenario the user will also receive a message to manually input their address.

The first argument is an error instance (i.e. inherits from Error) representing the error which has occurred.

Examples of errors includes "lookup balance exhausted" and "lookup limit reached" errors.

onOpen

Invoked when Address Finder suggestion box is opened (i.e. presented to the user).

onBlur

Invoked when the user unfocuses from the address input field.

onClose

Invoked when Address Finder suggestion box is closed (i.e. hidden from user).

onFocus

Invoked when user selects or focuses address input field.

onInput

Invoked when user selects or focuses address input field.

onSelect

Invoked when an address suggestion in suggestion box is selected.

Licence

GNU Affero General Public License v3.0

Keywords

FAQs

Last updated on 20 Jan 2021

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc