From 030c5ce91943311d1cad677281f5189088cd9b04 Mon Sep 17 00:00:00 2001 From: Matt Simon Date: Thu, 29 Jul 2021 11:07:03 +0100 Subject: [PATCH] Autosuggest/Address finder - add functionality and refactor (#1605) * Autosuggest - add more input params + fix uprn param * Fix class params * typo * Fix evaluation * Fix tests * Remove only from test * Changed name of onsAddressFinder to onsAddressInput, updated docs, examples and component functionality to allow manual only mode * Update macro options --- .../address-finder/_macro-options.md | 58 -------- .../address-finder/_test-template.njk | 3 - .../address-finder/abortable-fetch.js | 55 -------- .../address-input/_macro-options.md | 67 ++++++++++ .../_macro.njk | 124 ++++++++++-------- .../address-input/_test-template.njk | 3 + .../autosuggest.address.dom.js | 0 .../autosuggest.address.error.js | 0 .../autosuggest.address.js | 0 .../autosuggest.address.setter.js | 0 .../address-input-editable}/index.njk | 4 +- .../examples/address-input-manual/index.njk | 23 ++++ .../examples/address-input}/index.njk | 4 +- .../index.njk | 30 +++-- src/components/autosuggest/_macro-options.md | 40 +++--- src/components/autosuggest/_macro.njk | 34 +++-- src/components/input/_macro.njk | 3 +- src/components/mutually-exclusive/_macro.njk | 3 + src/js/index.js | 2 +- .../examples/address-input-manual/index.njk | 49 +++++++ .../index.njk | 6 +- .../register-address/index.njk | 4 +- .../index.njk | 4 +- src/patterns/addresses/index.njk | 24 +++- .../autosuggest/autosuggest.address.spec.js | 12 +- 25 files changed, 320 insertions(+), 232 deletions(-) delete mode 100644 src/components/address-finder/_macro-options.md delete mode 100644 src/components/address-finder/_test-template.njk delete mode 100644 src/components/address-finder/abortable-fetch.js create mode 100644 src/components/address-input/_macro-options.md rename src/components/{address-finder => address-input}/_macro.njk (50%) create mode 100644 src/components/address-input/_test-template.njk rename src/components/{address-finder => address-input}/autosuggest.address.dom.js (100%) rename src/components/{address-finder => address-input}/autosuggest.address.error.js (100%) rename src/components/{address-finder => address-input}/autosuggest.address.js (100%) rename src/components/{address-finder => address-input}/autosuggest.address.setter.js (100%) rename src/components/{address-finder/examples/address-finder-editable => address-input/examples/address-input-editable}/index.njk (95%) create mode 100644 src/components/address-input/examples/address-input-manual/index.njk rename src/components/{address-finder/examples/address-finder => address-input/examples/address-input}/index.njk (95%) rename src/components/{address-finder => address-input}/index.njk (73%) create mode 100644 src/patterns/addresses/examples/address-input-manual/index.njk rename src/patterns/addresses/examples/{address-finder-non-editable => address-input-non-editable}/index.njk (95%) rename src/patterns/addresses/examples/{address-finder-non-editable => address-input-non-editable}/register-address/index.njk (84%) rename src/patterns/addresses/examples/{address-finder => address-input}/index.njk (96%) diff --git a/src/components/address-finder/_macro-options.md b/src/components/address-finder/_macro-options.md deleted file mode 100644 index 5df8d5d8b5..0000000000 --- a/src/components/address-finder/_macro-options.md +++ /dev/null @@ -1,58 +0,0 @@ -| Name | Type | Required | Description | -| ----------------------- | ---------------------- | -------- | -------------------------------------------------------------------------------------------------------------------------------------------------- | -| id | string | true | ID for the fieldset. This is used to automatically generate IDs and names for each field if one isn't specified | -| classes | string | false | Classes to add to the fieldset | -| legend | string | true | Legend text for the address input | -| legendClasses | string | false | Classes to apply to the legend | -| dontWrap | boolean | false | Set to true to prevent the address fields from being wrapped in a fieldset | -| isEditable | boolean | true | Used with the address macro to invoke population of manual fields upon selection of suggestion | -| mandatory | boolean | false | Set the autosuggest input to be mandatory and use client side validation for empty form submission | -| APIDomain | string | false | Set an api domain when using an external API to suggest results | -| APIDomainBearerToken | string | false | Set a bearer token for api authorization on the AIMS address api. | -| instructions | string | true | Instructions on how to use the autosuggest that will be read out by screenreaders | -| ariaYouHaveSelected | string | true | Aria message to tell the user that they have selected an answer | -| ariaMinChars | string | true | Aria message to tell the user how many charecters they need to enter before autosuggest will start | -| ariaResultsLabel | string | true | Aria message to tell the user that suggestions are available | -| ariaOneResult | string | true | Aria message to tell the user there is only one suggestion left | -| ariaNResults | string | true | Aria message to tell the user how many suggestions are left | -| ariaLimitedResults | string | true | Aria message to tell the user if the results have been limited and what they are limited to | -| ariaGroupedResults | string | true | Aria message to tell the user about a grouped result e.g There are {n} for {x} | -| groupCount | string | true | Aria message to tell the user the number of addresses in a group e.g. {n} addresses | -| moreResults | string | true | Aria message to tell the user to continue to type to refine suggestions | -| noResults | string | true | message to tell the user there are no results | -| tooManyResults | string | true | message to tell the user there are too many results to display and the user should refine the search | -| typeMore | string | true | message to encourage the user to enter more characters to get suggestions | -| resultsTitle | string | true | Title of results to be displayed on screen at the top of the results | -| errorTitle | string | false | Error message title displayed in the error panel | -| errorMessageEnter | string | false | Error message description displayed in the error panel when the input is empty | -| errorMessageSelect | string | false | Error message description displayed in the error panel when a suggestion has not been selected | -| errorMessageAPI | string | false | Error message displayed when the API has failed during a search | -| errorMessageAPILinkText | string | false | Link text used to toggle to a manual mode when using the address macro | -| manualLink | string | false | url for the link displayed below the input | -| manualLinkText | string | false | Link text shown for the manual link. If using the editable address, including this parameter will toggle the mode to manual entry | -| options | `Object` | false | Option to provide key value pairs that will be added as data attributes to the component that will be added as parameters to the address index api | -| organisation | `Object` | false | Configuration for the organisation field. | -| line1 | `Object` | false | Configuration for the line1 field | -| line2 | `Object` | false | Configuration for the line2 field | -| town | `Object` | false | Configuration for the town field | -| county | `Object` | false | Configuration for the county field | -| postcode | `Object` | false | Configuration for the postcode field | -| uprn | `Object` | false | Configuration for the uprn field | - -## Options - -| Name | Type | Required | Description | -| ---------- | ------ | -------- | ------------------------------------------------ | -| regionCode | string | false | Sets the provided region code e.g. en-gb | -| adressType | string | false | Sets the provided address type e.g. resedential | -| oneYearAgo | string | false | If "true" will set a query parameter of epoch=75 | - -## AddressField - -| Name | Type | Required | Description | -| ---------- | ------------------------------------ | -------- | ------------------------------------ | -| id | string | false | ID for the field | -| name | string | false | Sets the name attribute on the field | -| label.text | string | true | The label for the input | -| value | string | false | The value for the input | -| error | `Error` [_(ref)_](/components/error) | false | Configuration for validation errors | diff --git a/src/components/address-finder/_test-template.njk b/src/components/address-finder/_test-template.njk deleted file mode 100644 index 3dfce6c830..0000000000 --- a/src/components/address-finder/_test-template.njk +++ /dev/null @@ -1,3 +0,0 @@ -{% from "components/address-finder/_macro.njk" import onsAddressFinder %} - -{{ onsAddressFinder(params) }} diff --git a/src/components/address-finder/abortable-fetch.js b/src/components/address-finder/abortable-fetch.js deleted file mode 100644 index 0c96ae549f..0000000000 --- a/src/components/address-finder/abortable-fetch.js +++ /dev/null @@ -1,55 +0,0 @@ -class AbortableFetch { - constructor(url, options) { - this.url = url; - this.options = options; - this.controller = new window.AbortController(); - this.status = 'UNSENT'; - } - - send() { - this.status = 'LOADING'; - - return new Promise((resolve, reject) => { - abortableFetch(this.url, { signal: this.controller.signal, ...this.options }) - .then(response => { - if (response.status >= 200 && response.status < 300) { - this.status = 'DONE'; - resolve(response); - } else if (response.status >= 400) { - this.status = 'DONE'; - resolve(response); - } else { - this.status = 'DONE'; - reject(response); - } - }) - .catch(error => { - this.status = 'undefined'; - reject(error); - }); - }); - } - - abort() { - this.controller.abort(); - } -} - -function abortableFetch(url, options) { - return window - .fetch(url, options) - .then(response => { - if (response) { - return response; - } else { - const error = new Error(response.status); - error.response = response; - throw error; - } - }) - .catch(error => { - throw error; - }); -} - -export default (url, options) => new AbortableFetch(url, options); diff --git a/src/components/address-input/_macro-options.md b/src/components/address-input/_macro-options.md new file mode 100644 index 0000000000..1359e94cf6 --- /dev/null +++ b/src/components/address-input/_macro-options.md @@ -0,0 +1,67 @@ +| Name | Type | Required | Description | +| ----------------------- | ------------------------------------------------------------- | -------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------- | +| id | string | true | ID for the fieldset. This is used to automatically generate IDs and names for each field if one isn't specified | +| classes | string | false | Classes to add to the fieldset | +| label | `Label` [_(ref)_](/components/label) | false | Settings for the input label. `for` will automatically be set to match the input id | +| legend | string | true | Legend text for the address input | +| legendClasses | string | false | Classes to apply to the legend | +| dontWrap | boolean | false | Set to true to prevent the address fields from being wrapped in a fieldset | +| isEditable | boolean | true | Used with the address macro to invoke population of manual fields upon selection of suggestion | +| manualEntry | boolean | false | Only display manual fields and do not invoke autosuggest/api functionality | +| mandatory | boolean | false | Set the autosuggest input to be mandatory and use client side validation for empty form submission | +| APIDomain | string | false | Set an api domain when using an external API to suggest results | +| APIDomainBearerToken | string | false | Set a bearer token for api authorization on the AIMS address api. | +| instructions | string | true | Instructions on how to use the autosuggest that will be read out by screenreaders | +| ariaYouHaveSelected | string | true | Aria message to tell the user that they have selected an answer | +| ariaMinChars | string | true | Aria message to tell the user how many charecters they need to enter before autosuggest will start | +| ariaResultsLabel | string | true | Aria message to tell the user that suggestions are available | +| ariaOneResult | string | true | Aria message to tell the user there is only one suggestion left | +| ariaNResults | string | true | Aria message to tell the user how many suggestions are left | +| ariaLimitedResults | string | true | Aria message to tell the user if the results have been limited and what they are limited to | +| ariaGroupedResults | string | true | Aria message to tell the user about a grouped result e.g There are {n} for {x} | +| groupCount | string | true | Aria message to tell the user the number of addresses in a group e.g. {n} addresses | +| moreResults | string | true | Aria message to tell the user to continue to type to refine suggestions | +| noResults | string | true | message to tell the user there are no results | +| tooManyResults | string | true | message to tell the user there are too many results to display and the user should refine the search | +| typeMore | string | true | message to encourage the user to enter more characters to get suggestions | +| resultsTitle | string | true | Title of results to be displayed on screen at the top of the results | +| errorTitle | string | false | Error message title displayed in the error panel | +| errorMessageEnter | string | false | Error message description displayed in the error panel when the input is empty | +| errorMessageSelect | string | false | Error message description displayed in the error panel when a suggestion has not been selected | +| errorMessageAPI | string | false | Error message displayed when the API has failed during a search | +| errorMessageAPILinkText | string | false | Link text used to toggle to a manual mode when using the address macro | +| manualLink | string | false | url for the link displayed below the input | +| manualLinkText | string | false | Link text shown for the manual link. If using the editable address, including this parameter will toggle the mode to manual entry | +| options | `Object` | false | Option to provide key value pairs that will be added as data attributes to the component that will be added as parameters to the address index api | +| organisation | `Object` | false | Configuration for the organisation field. | +| line1 | `Object` | false | Configuration for the line1 field | +| line2 | `Object` | false | Configuration for the line2 field | +| town | `Object` | false | Configuration for the town field | +| county | `Object` | false | Configuration for the county field | +| postcode | `Object` | false | Configuration for the postcode field | +| uprn | `Object` | false | Configuration for the uprn field | +| mutuallyExclusive | `MutuallyExclusive` [_(ref)_](/components/mutually-exclusive) | false | Configuration object if this is a mutually exclusive input | +| legend | string | Only if mutuallyExclusive is set | Text content for the legend | +| legendClasses | string | false | Classes for the legend | +| error | `Error` [_(ref)_](/components/error) | false | Configuration for validation errors | +| name | string | false | The name of the input | +| value | string | number | false | The value to set the input to | +| attributes | object | false | HTML attributes (for example data attributes) to add to the input | + +## Options + +| Name | Type | Required | Description | +| ---------- | ------ | -------- | ------------------------------------------------ | +| regionCode | string | false | Sets the provided region code e.g. en-gb | +| adressType | string | false | Sets the provided address type e.g. resedential | +| oneYearAgo | string | false | If "true" will set a query parameter of epoch=75 | + +## AddressField + +| Name | Type | Required | Description | +| ---------- | ------------------------------------ | -------- | ------------------------------------ | +| id | string | false | ID for the field | +| name | string | false | Sets the name attribute on the field | +| label.text | string | true | The label for the input | +| value | string | false | The value for the input | +| error | `Error` [_(ref)_](/components/error) | false | Configuration for validation errors | diff --git a/src/components/address-finder/_macro.njk b/src/components/address-input/_macro.njk similarity index 50% rename from src/components/address-finder/_macro.njk rename to src/components/address-input/_macro.njk index 084af4739a..caeee09cc4 100644 --- a/src/components/address-finder/_macro.njk +++ b/src/components/address-input/_macro.njk @@ -2,11 +2,11 @@ {% from "components/autosuggest/_macro.njk" import onsAutosuggest %} {% from "components/input/_macro.njk" import onsInput %} -{% macro onsAddressFinder(params) %} +{% macro onsAddressInput(params) %} {% set fields %} -