@ideal-postcodes/address-finder
Advanced tools
Comparing version 4.4.1 to 4.5.0
@@ -468,2 +468,6 @@ /** | ||
/** | ||
* Reference to input DOM element | ||
*/ | ||
countryInput: HTMLInputElement | HTMLSelectElement | null; | ||
/** | ||
* Reference to Address Finder message DOM element | ||
@@ -525,2 +529,6 @@ */ | ||
/** | ||
* Country form input change listener | ||
*/ | ||
countryListener: Listener<"change">; | ||
/** | ||
* Address Finder state machine | ||
@@ -742,2 +750,3 @@ */ | ||
export declare const _onKeyDown: (c: Controller) => Listener<"keydown">; | ||
export declare const _onCountryChange: (c: Controller) => Listener<"change">; | ||
/** | ||
@@ -744,0 +753,0 @@ * Retrieve Element |
@@ -9,3 +9,3 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.setPositionFixed = exports.findOrCreate = exports._onKeyDown = exports.Controller = exports.defaults = exports.NOOP = void 0; | ||
exports.setPositionFixed = exports.findOrCreate = exports._onCountryChange = exports._onKeyDown = exports.Controller = exports.defaults = exports.NOOP = void 0; | ||
/* eslint-disable no-invalid-this */ | ||
@@ -244,2 +244,4 @@ const announcer_1 = require("./announcer"); | ||
this.input.id = this.ids(); | ||
const countryInput = this.scope.querySelector(this.options.outputFields.country); | ||
this.countryInput = countryInput; | ||
// Apply additional accessibility improvments | ||
@@ -255,2 +257,3 @@ this.ariaAnchor().setAttribute("role", "combobox"); | ||
this.keydownListener = (0, exports._onKeyDown)(this); | ||
this.countryListener = (0, exports._onCountryChange)(this); | ||
const { container, announce } = (0, announcer_1.announcer)({ | ||
@@ -515,2 +518,5 @@ idA: this.ids(), | ||
this.input.addEventListener("keydown", this.keydownListener); | ||
this.input.addEventListener("keydown", this.keydownListener); | ||
if (this.countryInput) | ||
this.countryInput.addEventListener("change", this.countryListener); | ||
const parent = this.input.parentNode; | ||
@@ -546,2 +552,4 @@ if (parent) { | ||
this.input.removeEventListener("keydown", this.keydownListener); | ||
if (this.countryInput) | ||
this.countryInput.removeEventListener("change", this.countryListener); | ||
this.container.removeChild(this.mainComponent); | ||
@@ -861,2 +869,13 @@ this.container.removeChild(this.alerts); | ||
exports._onKeyDown = _onKeyDown; | ||
// Event handler: Fires when country selection is changed | ||
const _onCountryChange = (c) => function (event) { | ||
if (event.target === null) | ||
return; | ||
const target = event.target; | ||
if (!target) | ||
return; | ||
let contextDetails = findMatchingContext(target.value, c.options.contexts); | ||
c.fsm.send({ type: "COUNTRY_CHANGE_EVENT", contextDetails }); | ||
}; | ||
exports._onCountryChange = _onCountryChange; | ||
/** | ||
@@ -894,1 +913,13 @@ * Retrieve Element | ||
exports.setPositionFixed = setPositionFixed; | ||
const findMatchingContext = (name, contexts) => { | ||
const n = name.toUpperCase(); | ||
for (const context of Object.values(contexts)) { | ||
if (context.iso_3 === n) | ||
return context; | ||
if (context.iso_2 === n) | ||
return context; | ||
if (context.description.toUpperCase() === n) | ||
return context; | ||
} | ||
return undefined; | ||
}; |
@@ -12,2 +12,5 @@ import { StateMachine } from "@xstate/fsm"; | ||
} | { | ||
type: "COUNTRY_CHANGE_EVENT"; | ||
contextDetails: ContextDetails | undefined; | ||
} | { | ||
type: "SUGGEST"; | ||
@@ -14,0 +17,0 @@ suggestions: AddressSuggestion[]; |
@@ -14,2 +14,5 @@ "use strict"; | ||
on: { | ||
COUNTRY_CHANGE_EVENT: { | ||
actions: ["updateContextWithCountry"], | ||
}, | ||
AWAKE: [ | ||
@@ -85,2 +88,12 @@ { | ||
actions: { | ||
// Updates the Address Finder context | ||
updateContextWithCountry: (_, e) => { | ||
if (e.type !== "COUNTRY_CHANGE_EVENT") | ||
return; | ||
if (!e.contextDetails) | ||
return; | ||
c.applyContext(e.contextDetails); | ||
c.suggestions = []; | ||
c.cache.clear(); | ||
}, | ||
addHint: () => { | ||
@@ -87,0 +100,0 @@ c.setPlaceholder(c.options.msgPlaceholder); |
@@ -468,2 +468,6 @@ /** | ||
/** | ||
* Reference to input DOM element | ||
*/ | ||
countryInput: HTMLInputElement | HTMLSelectElement | null; | ||
/** | ||
* Reference to Address Finder message DOM element | ||
@@ -525,2 +529,6 @@ */ | ||
/** | ||
* Country form input change listener | ||
*/ | ||
countryListener: Listener<"change">; | ||
/** | ||
* Address Finder state machine | ||
@@ -742,2 +750,3 @@ */ | ||
export declare const _onKeyDown: (c: Controller) => Listener<"keydown">; | ||
export declare const _onCountryChange: (c: Controller) => Listener<"change">; | ||
/** | ||
@@ -744,0 +753,0 @@ * Retrieve Element |
@@ -236,2 +236,4 @@ /** | ||
this.input.id = this.ids(); | ||
const countryInput = this.scope.querySelector(this.options.outputFields.country); | ||
this.countryInput = countryInput; | ||
// Apply additional accessibility improvments | ||
@@ -247,2 +249,3 @@ this.ariaAnchor().setAttribute("role", "combobox"); | ||
this.keydownListener = _onKeyDown(this); | ||
this.countryListener = _onCountryChange(this); | ||
const { container, announce } = announcer({ | ||
@@ -507,2 +510,5 @@ idA: this.ids(), | ||
this.input.addEventListener("keydown", this.keydownListener); | ||
this.input.addEventListener("keydown", this.keydownListener); | ||
if (this.countryInput) | ||
this.countryInput.addEventListener("change", this.countryListener); | ||
const parent = this.input.parentNode; | ||
@@ -538,2 +544,4 @@ if (parent) { | ||
this.input.removeEventListener("keydown", this.keydownListener); | ||
if (this.countryInput) | ||
this.countryInput.removeEventListener("change", this.countryListener); | ||
this.container.removeChild(this.mainComponent); | ||
@@ -851,2 +859,12 @@ this.container.removeChild(this.alerts); | ||
}; | ||
// Event handler: Fires when country selection is changed | ||
export const _onCountryChange = (c) => function (event) { | ||
if (event.target === null) | ||
return; | ||
const target = event.target; | ||
if (!target) | ||
return; | ||
let contextDetails = findMatchingContext(target.value, c.options.contexts); | ||
c.fsm.send({ type: "COUNTRY_CHANGE_EVENT", contextDetails }); | ||
}; | ||
/** | ||
@@ -882,1 +900,13 @@ * Retrieve Element | ||
}; | ||
const findMatchingContext = (name, contexts) => { | ||
const n = name.toUpperCase(); | ||
for (const context of Object.values(contexts)) { | ||
if (context.iso_3 === n) | ||
return context; | ||
if (context.iso_2 === n) | ||
return context; | ||
if (context.description.toUpperCase() === n) | ||
return context; | ||
} | ||
return undefined; | ||
}; |
@@ -12,2 +12,5 @@ import { StateMachine } from "@xstate/fsm"; | ||
} | { | ||
type: "COUNTRY_CHANGE_EVENT"; | ||
contextDetails: ContextDetails | undefined; | ||
} | { | ||
type: "SUGGEST"; | ||
@@ -14,0 +17,0 @@ suggestions: AddressSuggestion[]; |
@@ -11,2 +11,5 @@ import { createMachine, interpret } from "@xstate/fsm"; | ||
on: { | ||
COUNTRY_CHANGE_EVENT: { | ||
actions: ["updateContextWithCountry"], | ||
}, | ||
AWAKE: [ | ||
@@ -82,2 +85,12 @@ { | ||
actions: { | ||
// Updates the Address Finder context | ||
updateContextWithCountry: (_, e) => { | ||
if (e.type !== "COUNTRY_CHANGE_EVENT") | ||
return; | ||
if (!e.contextDetails) | ||
return; | ||
c.applyContext(e.contextDetails); | ||
c.suggestions = []; | ||
c.cache.clear(); | ||
}, | ||
addHint: () => { | ||
@@ -84,0 +97,0 @@ c.setPlaceholder(c.options.msgPlaceholder); |
{ | ||
"name": "@ideal-postcodes/address-finder", | ||
"version": "4.4.1", | ||
"version": "4.5.0", | ||
"description": "Address Finder JS library backed by the Ideal Postcodes UK address search API", | ||
@@ -45,2 +45,3 @@ "main": "dist/index.js", | ||
"scripts": { | ||
"dev": "npx parcel examples/index.html", | ||
"test": "cypress run --e2e -C ./cypress.config.unit.ts --spec=cypress/unit/**/**.cy.ts", | ||
@@ -60,3 +61,4 @@ "test:open": "cypress open --e2e -C ./cypress.config.unit.ts", | ||
"prepublishOnly": "npm run build", | ||
"cypress:open": "cypress open" | ||
"cypress:open": "cypress open", | ||
"rollup": "rollup -c" | ||
}, | ||
@@ -109,3 +111,3 @@ "browserslist": [ | ||
"@ideal-postcodes/core-axios": "4.1.2", | ||
"@ideal-postcodes/jsutil": "6.2.0", | ||
"@ideal-postcodes/jsutil": "6.4.0", | ||
"@xstate/fsm": "~2.1.0", | ||
@@ -120,2 +122,3 @@ "lodash": "~4.17.21" | ||
"@babel/preset-typescript": "~7.22.5", | ||
"@babel/plugin-transform-runtime": "~7.23.9", | ||
"@cablanchard/semantic-release": "~1.3.4", | ||
@@ -129,18 +132,15 @@ "@cablanchard/tsconfig": "~2.0.0", | ||
"@rollup/plugin-node-resolve": "~13.0.0", | ||
"@types/chai": "~4.2.14", | ||
"@types/dotenv": "~8.2.0", | ||
"@types/karma": "~6.3.0", | ||
"@types/lodash": "~4.14.195", | ||
"@types/mocha": "~9.0.0", | ||
"@types/node": "~20.4.2", | ||
"@types/sinon": "~10.0.15", | ||
"rollup-plugin-ts": "~3.2.0", | ||
"chai": "~4.3.0", | ||
"@types/node": "~20.11.19", | ||
"@types/sinon": "~17.0.3", | ||
"@types/chai": "~4.3.11", | ||
"chai": "~5.0.3", | ||
"rollup-plugin-ts": "~3.4.5", | ||
"codecov": "~3.8.1", | ||
"core-js": "~3.15.2", | ||
"core-js-pure": "~3.17.2", | ||
"cypress": "~12.17.1", | ||
"cypress": "~13.6.4", | ||
"dotenv": "~8.2.0", | ||
"minify": "~7.0.0", | ||
"mocha": "~9.1.1", | ||
"prettier": "~2.4.1", | ||
@@ -150,3 +150,3 @@ "promise-polyfill": "~8.2.0", | ||
"semantic-release": "~19.0.3", | ||
"sinon": "~15.2.0", | ||
"sinon": "~17.0.1", | ||
"ts-node": "~10.2.0", | ||
@@ -153,0 +153,0 @@ "typedoc": "~0.24.8", |
251037
35
7089
+ Added@ideal-postcodes/jsutil@6.4.0(transitive)
- Removed@ideal-postcodes/jsutil@6.2.0(transitive)