@ministryofjustice/hmpps-connect-dps-shared-items 3.3.2 → 3.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/assets/dps/components/address-autosuggest/abortable-fetch.js +9 -1
- package/dist/assets/dps/components/address-autosuggest/autosuggest-address.js +3 -2
- package/dist/assets/dps/components/address-autosuggest/autosuggest-ui.js +15 -1
- package/dist/assets/dps/components/address-autosuggest/macro.njk +1 -0
- package/package.json +2 -2
|
@@ -10,18 +10,26 @@ export const abortError = 'AbortError'
|
|
|
10
10
|
export const abortTimeout = 'AbortTimeout'
|
|
11
11
|
|
|
12
12
|
class AbortableFetch {
|
|
13
|
-
constructor(url) {
|
|
13
|
+
constructor(url, options) {
|
|
14
14
|
this.url = url
|
|
15
15
|
this.controller = new window.AbortController()
|
|
16
16
|
this.status = FetchStatus.unsent
|
|
17
|
+
this.fetchDelay = options?.fetchDelay || 500
|
|
17
18
|
}
|
|
18
19
|
|
|
19
20
|
async send() {
|
|
20
21
|
this.status = FetchStatus.loading
|
|
21
22
|
try {
|
|
23
|
+
// Abortable delay before querying the API to limit the rate of requests when someone is typing:
|
|
24
|
+
await new Promise((resolve) => {
|
|
25
|
+
setTimeout(resolve, this.fetchDelay)
|
|
26
|
+
this.controller.signal.addEventListener('abort', () => { resolve() }, { once: true })
|
|
27
|
+
})
|
|
28
|
+
|
|
22
29
|
setTimeout(() => {
|
|
23
30
|
this.controller.abort(abortTimeout)
|
|
24
31
|
}, 5000)
|
|
32
|
+
|
|
25
33
|
const response = await window.fetch(this.url, { signal: this.controller.signal })
|
|
26
34
|
|
|
27
35
|
if (!(+response.status === 200)) {
|
|
@@ -13,6 +13,7 @@ export class AutosuggestAddress {
|
|
|
13
13
|
this.container = context.querySelector(`.${classInputContainer}`)
|
|
14
14
|
this.uprn = context.querySelector(`.${classUPRN}`)
|
|
15
15
|
this.findUrl = this.container.getAttribute('data-find-url')
|
|
16
|
+
this.fetchDelay = this.container.getAttribute('data-fetch-delay')
|
|
16
17
|
|
|
17
18
|
// State:
|
|
18
19
|
this.fetch = null
|
|
@@ -29,7 +30,7 @@ export class AutosuggestAddress {
|
|
|
29
30
|
}
|
|
30
31
|
|
|
31
32
|
async checkAPIStatus() {
|
|
32
|
-
this.fetch = abortableFetch(`${this.findUrl}/SW1H9AJ
|
|
33
|
+
this.fetch = abortableFetch(`${this.findUrl}/SW1H9AJ`, { fetchDelay: this.fetchDelay })
|
|
33
34
|
|
|
34
35
|
try {
|
|
35
36
|
const response = await this.fetch.send()
|
|
@@ -56,7 +57,7 @@ export class AutosuggestAddress {
|
|
|
56
57
|
}
|
|
57
58
|
|
|
58
59
|
async findAddress(query) {
|
|
59
|
-
this.fetch = abortableFetch(`${this.findUrl}/${query}
|
|
60
|
+
this.fetch = abortableFetch(`${this.findUrl}/${query}`, { fetchDelay: this.fetchDelay })
|
|
60
61
|
const data = await this.fetch.send()
|
|
61
62
|
const response = await data.json()
|
|
62
63
|
const status = response.status
|
|
@@ -80,6 +80,7 @@ export class AutosuggestUi {
|
|
|
80
80
|
this.input.addEventListener('keyup', this.handleKeyup.bind(this))
|
|
81
81
|
this.input.addEventListener('input', this.handleChange.bind(this))
|
|
82
82
|
this.input.addEventListener('blur', this.handleBlur.bind(this))
|
|
83
|
+
this.input.addEventListener('focus', this.handleFocus.bind(this))
|
|
83
84
|
}
|
|
84
85
|
|
|
85
86
|
handleKeydown(event) {
|
|
@@ -144,10 +145,23 @@ export class AutosuggestUi {
|
|
|
144
145
|
// Timeout required to allow user to click an option before clearing the listbox:
|
|
145
146
|
this.blurTimeout = setTimeout(() => {
|
|
146
147
|
this.blurring = false
|
|
147
|
-
|
|
148
|
+
|
|
149
|
+
this.context.classList.remove(`${this.stylingBaseClass}${classSuffixHasResults}`)
|
|
150
|
+
this.input.removeAttribute('aria-activedescendant')
|
|
151
|
+
this.input.setAttribute('aria-expanded', false)
|
|
152
|
+
this.setAriaStatus()
|
|
148
153
|
}, 300)
|
|
149
154
|
}
|
|
150
155
|
|
|
156
|
+
handleFocus() {
|
|
157
|
+
if (this.listbox.innerHTML) {
|
|
158
|
+
this.context.classList.add(`${this.stylingBaseClass}${classSuffixHasResults}`)
|
|
159
|
+
this.input.addAttribute('aria-activedescendant')
|
|
160
|
+
this.input.setAttribute('aria-expanded', true)
|
|
161
|
+
this.setAriaStatus()
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
|
|
151
165
|
handleScroll(option, scrollUp) {
|
|
152
166
|
clearTimeout(this.scrollTimeout)
|
|
153
167
|
this.scrolling = true
|
|
@@ -16,6 +16,7 @@
|
|
|
16
16
|
data-error-api="The address search is not working right now. Enter the address manually to continue."
|
|
17
17
|
data-styling-base-class="hmpps-address-autosuggest"
|
|
18
18
|
data-find-url="{{ options.findUrl or "/api/addresses/find" }}"
|
|
19
|
+
data-fetch-delay="{{ options.fetchDelay or 500 }}"
|
|
19
20
|
>
|
|
20
21
|
{{ govukInput({
|
|
21
22
|
id: 'address-autosuggest-input',
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ministryofjustice/hmpps-connect-dps-shared-items",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.5.0",
|
|
4
4
|
"description": "A package to share Connect DPS functionality across applications",
|
|
5
5
|
"main": "./dist/index.cjs",
|
|
6
6
|
"module": "./dist/index.esm.js",
|
|
@@ -64,7 +64,7 @@
|
|
|
64
64
|
"jest-junit": "^16.0.0",
|
|
65
65
|
"lint-staged": "^16.2.7",
|
|
66
66
|
"nock": "^14.0.10",
|
|
67
|
-
"rollup": "4.
|
|
67
|
+
"rollup": "4.56.0",
|
|
68
68
|
"rollup-plugin-dts": "^6.3.0",
|
|
69
69
|
"ts-jest": "^29.4.6",
|
|
70
70
|
"tslib": "^2.8.1",
|