UNPKG

@ideal-postcodes/address-finder

Version:

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

204 lines (203 loc) 6.08 kB
import { CloseReason } from "./state"; import { AddressSuggestion, AnyAddress } from "@ideal-postcodes/jsutil"; import { ContextDetails } from "./contexts"; import { Controller } from "./controller"; export interface Callbacks { /** * Invoked when Address Finder has been successfully attached to the input element. */ onLoaded?: OnLoaded; /** * Invoked when `checkKey` is enabled and the key is discovered to be in an * unusable state (e.g. daily limit reached, no balance, etc). */ onFailedCheck?: OnFailedCheck; /** * Invoked when mouse click on Address suggestion occuers */ onMouseDown?: OnMouseDown; /** * Invoked when keypress on input field occurs */ onKeyDown?: OnKeyDown; /** * Invoked immediately after address suggestions are retrieved from the API. * The first argument is an array of address suggestions. */ onSuggestionsRetrieved?: OnSuggestionsRetrieved; /** * 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. */ onAddressRetrieved?: OnAddressRetrieved; /** * Invoked when view is attached to the DOM */ onMounted?: OnMounted; /** * Invoked when view is detached from the DOM */ onRemove?: OnRemove; /** * 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. */ onAddressSelected?: OnAddressSelected; /** * Invoked when a user selects a country. */ onCountrySelected?: OnCountrySelected; /** * Invoked when selected address is populated into address fields of user * address form */ onAddressPopulated?: OnAddressPopulated; /** * Invoked when an error has occurred following an attempt to retrieve a full * address. i.e. the API request made after the user selects a suggestion. * * The first argument is an error instance (i.e. inherits from `Error`) * representing the error which has occurred. * * In this scenario the user will also receive a message to manually input an * address if address retrieval fails. */ onSearchError?: OnSearchError; /** * Invoked when an address suggestion retrieval request has failed. * * In this scenario the user will be alerted that no address suggestions * could be found and to manually input an address. */ onSuggestionError?: OnSuggestionError; /** * Invoked when Address Finder suggestion box is opened (i.e. presented to the user). */ onOpen?: OnOpen; /** * Invoked when `blur` event is dispatched by Address Finder input field */ onBlur?: OnBlur; /** * Invoked when the Address Finder view closes (i.e. disappears) */ onClose?: OnClose; /** * Invoked when `focus` event is dispatched by Address Finder input field */ onFocus?: OnFocus; /** * Invoked when `input` event is dispatched by Address Finder input field */ onInput?: OnInput; /** * Invoked when a suggestion has been selected */ onSelect?: OnSelect; /** * Invoked when hidden fields are unhidden (i.e. user selects an address or opts for manual input) */ onUnhide?: OnUnhide; /** * Invoked each time context changed (i.e. user change country to search address) */ onContextChange?: OnContextChange; } /** * Invoked when Address Finder suggestion box is opened (i.e. presented to the user) */ export interface OnOpen { (this: Controller): void; } /** * Invoked when the user unfocuses from the address input field */ export interface OnBlur { (this: Controller): void; } /** * Invoked when Address Finder suggestion box is closed (i.e. hidden from user) */ export interface OnClose { (this: Controller, reason?: CloseReason): void; } /** * Invoked when user selects or focuses address input field */ export interface OnFocus { (this: Controller): void; } /** * Invoked when input is detected on address input field */ export interface OnInput { (this: Controller, event: Event): void; } /** * Invoked when an address suggestion in suggestion box is selected */ export interface OnSelect { (this: Controller, suggestion: AddressSuggestion): void; } /** * Invoked when mousedown event is triggered on suggestion list */ export interface OnMouseDown { (this: Controller, event: MouseEvent): void; } /** * Invoked when keypress is triggerd on input */ export interface OnKeyDown { (this: Controller, event: KeyboardEvent): void; } /** * Invoked when an address suggestion in suggestion box is selected */ export interface OnUnhide { (this: Controller): void; } export interface OnLoaded { (this: Controller): void; } export interface OnFailedCheck { (this: Controller, error: Error): void; } export interface OnSuggestionsRetrieved { (this: Controller, suggestion: AddressSuggestion[]): void; } export interface OnAddressRetrieved { (this: Controller, address: AnyAddress): void; } export interface OnCountrySelected { (this: Controller, country: ContextDetails): void; } export interface OnSearchError { (this: Controller, error: Error): void; } export interface OnSuggestionError { (this: Controller, error: Error): void; } export interface OnMounted { (this: Controller): void; } export interface OnRemove { (this: Controller): void; } export interface OnAddressSelected { (this: Controller, suggestion: AddressSuggestion): void; } export interface OnAddressPopulated { (this: Controller, address: AnyAddress): void; } export interface OnContextChange { (this: Controller, context: string): void; } /** * @hidden */ export interface Listener<K extends keyof HTMLElementEventMap> { (ev: HTMLElementEventMap[K]): void; }