@govuk-react/primitives 0.0.0-stage → 0.2.1
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/README.md +271 -2
- package/dist/accordion/element.d.ts +14 -0
- package/dist/accordion/element.js +302 -0
- package/dist/accordion/element.js.map +1 -0
- package/dist/accordion/index.d.ts +74 -0
- package/dist/accordion/index.js +138 -0
- package/dist/accordion/index.js.map +1 -0
- package/dist/auto-focus/element.d.ts +1 -0
- package/dist/auto-focus/element.js +75 -0
- package/dist/auto-focus/element.js.map +1 -0
- package/dist/auto-focus/index.d.ts +297 -0
- package/dist/auto-focus/index.js +29 -0
- package/dist/auto-focus/index.js.map +1 -0
- package/dist/autocomplete/element.d.ts +27 -0
- package/dist/autocomplete/element.js +597 -0
- package/dist/autocomplete/element.js.map +1 -0
- package/dist/autocomplete/index.d.ts +350 -0
- package/dist/autocomplete/index.js +108 -0
- package/dist/autocomplete/index.js.map +1 -0
- package/dist/button/element.d.ts +1 -0
- package/dist/button/element.js +59 -0
- package/dist/button/element.js.map +1 -0
- package/dist/button/index.d.ts +10 -0
- package/dist/button/index.js +9 -0
- package/dist/button/index.js.map +1 -0
- package/dist/character-count/element.d.ts +7 -0
- package/dist/character-count/element.js +287 -0
- package/dist/character-count/element.js.map +1 -0
- package/dist/character-count/index.d.ts +57 -0
- package/dist/character-count/index.js +79 -0
- package/dist/character-count/index.js.map +1 -0
- package/dist/character-count/messages.d.ts +20 -0
- package/dist/character-count/messages.js +54 -0
- package/dist/character-count/messages.js.map +1 -0
- package/dist/choices/element.d.ts +5 -0
- package/dist/choices/element.js +202 -0
- package/dist/choices/element.js.map +1 -0
- package/dist/choices/index.d.ts +309 -0
- package/dist/choices/index.js +37 -0
- package/dist/choices/index.js.map +1 -0
- package/dist/disclosure/element.d.ts +14 -0
- package/dist/disclosure/element.js +123 -0
- package/dist/disclosure/element.js.map +1 -0
- package/dist/disclosure/index.d.ts +307 -0
- package/dist/disclosure/index.js +66 -0
- package/dist/disclosure/index.js.map +1 -0
- package/dist/exit-this-page/element.d.ts +19 -0
- package/dist/exit-this-page/element.js +201 -0
- package/dist/exit-this-page/element.js.map +1 -0
- package/dist/exit-this-page/index.d.ts +330 -0
- package/dist/exit-this-page/index.js +54 -0
- package/dist/exit-this-page/index.js.map +1 -0
- package/dist/file-upload/element.d.ts +23 -0
- package/dist/file-upload/element.js +270 -0
- package/dist/file-upload/element.js.map +1 -0
- package/dist/file-upload/index.d.ts +46 -0
- package/dist/file-upload/index.js +65 -0
- package/dist/file-upload/index.js.map +1 -0
- package/dist/focus-links/element.d.ts +1 -0
- package/dist/focus-links/element.js +90 -0
- package/dist/focus-links/element.js.map +1 -0
- package/dist/focus-links/index.d.ts +288 -0
- package/dist/focus-links/index.js +7 -0
- package/dist/focus-links/index.js.map +1 -0
- package/dist/index.d.ts +13 -0
- package/dist/index.js +14 -0
- package/dist/index.js.map +1 -0
- package/dist/inline/accordion-element.js +1 -0
- package/dist/inline/auto-focus-element.js +1 -0
- package/dist/inline/autocomplete-element.js +2 -0
- package/dist/inline/button-element.js +1 -0
- package/dist/inline/character-count-element.js +1 -0
- package/dist/inline/choices-element.js +1 -0
- package/dist/inline/disclosure-element.js +1 -0
- package/dist/inline/exit-this-page-element.js +1 -0
- package/dist/inline/file-upload-element.js +1 -0
- package/dist/inline/focus-links-element.js +1 -0
- package/dist/inline/modal-element.js +1 -0
- package/dist/inline/password-input-element.js +1 -0
- package/dist/inline/tabs-element.js +1 -0
- package/dist/inline-script/index.d.ts +26 -0
- package/dist/inline-script/index.js +31 -0
- package/dist/inline-script/index.js.map +1 -0
- package/dist/modal/element.d.ts +10 -0
- package/dist/modal/element.js +114 -0
- package/dist/modal/element.js.map +1 -0
- package/dist/modal/index.d.ts +36 -0
- package/dist/modal/index.js +109 -0
- package/dist/modal/index.js.map +1 -0
- package/dist/password-input/element.d.ts +1 -0
- package/dist/password-input/element.js +134 -0
- package/dist/password-input/element.js.map +1 -0
- package/dist/password-input/index.d.ts +318 -0
- package/dist/password-input/index.js +35 -0
- package/dist/password-input/index.js.map +1 -0
- package/dist/tabs/element.d.ts +19 -0
- package/dist/tabs/element.js +416 -0
- package/dist/tabs/element.js.map +1 -0
- package/dist/tabs/index.d.ts +83 -0
- package/dist/tabs/index.js +138 -0
- package/dist/tabs/index.js.map +1 -0
- package/dist/utils/dom.d.ts +9 -0
- package/dist/utils/dom.js +27 -0
- package/dist/utils/dom.js.map +1 -0
- package/dist/utils/on-property-set.d.ts +9 -0
- package/dist/utils/on-property-set.js +38 -0
- package/dist/utils/on-property-set.js.map +1 -0
- package/dist/utils/set-focus.d.ts +11 -0
- package/dist/utils/set-focus.js +38 -0
- package/dist/utils/set-focus.js.map +1 -0
- package/dist/utils/set-up-when-ready.d.ts +6 -0
- package/dist/utils/set-up-when-ready.js +16 -0
- package/dist/utils/set-up-when-ready.js.map +1 -0
- package/dist/utils/slot.d.ts +13 -0
- package/dist/utils/slot.js +40 -0
- package/dist/utils/slot.js.map +1 -0
- package/dist/utils/translation.d.ts +13 -0
- package/dist/utils/translation.js +57 -0
- package/dist/utils/translation.js.map +1 -0
- package/licences/accessible-autocomplete.txt +20 -0
- package/package.json +49 -4
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
import * as React from 'react';
|
|
3
|
+
import { isValidElement } from 'react';
|
|
4
|
+
import { Slot } from '../utils/slot.js';
|
|
5
|
+
import { tagName } from './element.js';
|
|
6
|
+
export function Root({ disabled, ...props }) {
|
|
7
|
+
return React.createElement(tagName, { ...props, disabled: disabled ? '' : undefined });
|
|
8
|
+
}
|
|
9
|
+
/** The tabindex of the target, or with `asChild`, of its child. */
|
|
10
|
+
function tabIndexOf({ asChild, tabIndex, children }) {
|
|
11
|
+
if (tabIndex !== undefined || !asChild)
|
|
12
|
+
return tabIndex;
|
|
13
|
+
return isValidElement(children) ? children.props.tabIndex : undefined;
|
|
14
|
+
}
|
|
15
|
+
/** What gets focus. */
|
|
16
|
+
export function Target({ asChild, disabled, ...props }) {
|
|
17
|
+
const Element = asChild ? Slot : 'div';
|
|
18
|
+
let focus = {};
|
|
19
|
+
if (!disabled) {
|
|
20
|
+
// `data-focus-tabindex` says the tabindex is only for this, so it goes when focus moves on.
|
|
21
|
+
focus =
|
|
22
|
+
tabIndexOf({ asChild, ...props }) === undefined
|
|
23
|
+
? { tabIndex: -1, autoFocus: true, 'data-focus-tabindex': '' }
|
|
24
|
+
: { autoFocus: true };
|
|
25
|
+
}
|
|
26
|
+
// The element owns `tabindex` and `autofocus` after it has moved focus.
|
|
27
|
+
return React.createElement(Element, { "data-auto-focus": "", suppressHydrationWarning: true, ...focus, ...props });
|
|
28
|
+
}
|
|
29
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/auto-focus/index.tsx"],"names":[],"mappings":"AAAA,YAAY,CAAC;AAEb,OAAO,KAAK,KAAK,MAAM,OAAO,CAAC;AAC/B,OAAO,EAAE,cAAc,EAAE,MAAM,OAAO,CAAC;AAEvC,OAAO,EAAE,IAAI,EAAE,MAAM,kBAAkB,CAAC;AACxC,OAAO,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AAavC,MAAM,UAAU,IAAI,CAAC,EAAE,QAAQ,EAAE,GAAG,KAAK,EAAa;IACpD,OAAO,KAAK,CAAC,aAAa,CAAC,OAAO,EAAE,EAAE,GAAG,KAAK,EAAE,QAAQ,EAAE,QAAQ,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,SAAS,EAAE,CAAC,CAAC;AACzF,CAAC;AASD,mEAAmE;AACnE,SAAS,UAAU,CAAC,EAAE,OAAO,EAAE,QAAQ,EAAE,QAAQ,EAAe;IAC9D,IAAI,QAAQ,KAAK,SAAS,IAAI,CAAC,OAAO;QAAE,OAAO,QAAQ,CAAC;IACxD,OAAO,cAAc,CAAwB,QAAQ,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC,KAAK,CAAC,QAAQ,CAAC,CAAC,CAAC,SAAS,CAAC;AAC/F,CAAC;AAED,uBAAuB;AACvB,MAAM,UAAU,MAAM,CAAC,EAAE,OAAO,EAAE,QAAQ,EAAE,GAAG,KAAK,EAAe;IACjE,MAAM,OAAO,GAAG,OAAO,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,KAAK,CAAC;IACvC,IAAI,KAAK,GAAG,EAAE,CAAC;IACf,IAAI,CAAC,QAAQ,EAAE,CAAC;QACd,4FAA4F;QAC5F,KAAK;YACH,UAAU,CAAC,EAAE,OAAO,EAAE,GAAG,KAAK,EAAE,CAAC,KAAK,SAAS;gBAC7C,CAAC,CAAC,EAAE,QAAQ,EAAE,CAAC,CAAC,EAAE,SAAS,EAAE,IAAI,EAAE,qBAAqB,EAAE,EAAE,EAAE;gBAC9D,CAAC,CAAC,EAAE,SAAS,EAAE,IAAI,EAAE,CAAC;IAC5B,CAAC;IACD,wEAAwE;IACxE,OAAO,oBAAC,OAAO,uBAAiB,EAAE,EAAC,wBAAwB,WAAK,KAAK,KAAM,KAAK,GAAI,CAAC;AACvF,CAAC","sourcesContent":["'use client';\n\nimport * as React from 'react';\nimport { isValidElement } from 'react';\nimport type { ComponentProps } from 'react';\nimport { Slot } from '../utils/slot.js';\nimport { tagName } from './element.js';\n\n/*\n * Headless auto focus: the <govuk-react-auto-focus> element (./element.ts) moves focus to its\n * `Target` when it appears. The server renders the target with `autofocus`, so the browser focuses\n * it when the page loads, even without JavaScript.\n */\n\nexport type RootProps = React.HTMLAttributes<HTMLElement> & {\n /** Don't move focus. */\n disabled?: boolean;\n};\n\nexport function Root({ disabled, ...props }: RootProps) {\n return React.createElement(tagName, { ...props, disabled: disabled ? '' : undefined });\n}\n\nexport type TargetProps = ComponentProps<'div'> & {\n /** Render the only child instead, with this part's props merged onto it. */\n asChild?: boolean;\n /** Don't move focus. Pass the same as to `Root`. */\n disabled?: boolean;\n};\n\n/** The tabindex of the target, or with `asChild`, of its child. */\nfunction tabIndexOf({ asChild, tabIndex, children }: TargetProps) {\n if (tabIndex !== undefined || !asChild) return tabIndex;\n return isValidElement<{ tabIndex?: number }>(children) ? children.props.tabIndex : undefined;\n}\n\n/** What gets focus. */\nexport function Target({ asChild, disabled, ...props }: TargetProps) {\n const Element = asChild ? Slot : 'div';\n let focus = {};\n if (!disabled) {\n // `data-focus-tabindex` says the tabindex is only for this, so it goes when focus moves on.\n focus =\n tabIndexOf({ asChild, ...props }) === undefined\n ? { tabIndex: -1, autoFocus: true, 'data-focus-tabindex': '' }\n : { autoFocus: true };\n }\n // The element owns `tabindex` and `autofocus` after it has moved focus.\n return <Element data-auto-focus=\"\" suppressHydrationWarning {...focus} {...props} />;\n}\n"]}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/*! Based on accessible-autocomplete, Copyright (c) 2017 Crown Copyright (Government Digital Service), MIT licence: licences/accessible-autocomplete.txt */
|
|
2
|
+
export declare const tagName = "govuk-react-autocomplete";
|
|
3
|
+
export type Filter = 'contains' | 'starts-with' | 'none';
|
|
4
|
+
export interface AutocompleteElement extends HTMLElement {
|
|
5
|
+
/** The query, once upgraded. */
|
|
6
|
+
readonly query: string | undefined;
|
|
7
|
+
}
|
|
8
|
+
declare global {
|
|
9
|
+
interface HTMLElementEventMap {
|
|
10
|
+
'govuk-react-autocomplete:query': CustomEvent<{
|
|
11
|
+
query: string;
|
|
12
|
+
}>;
|
|
13
|
+
'govuk-react-autocomplete:confirm': CustomEvent<{
|
|
14
|
+
value: string;
|
|
15
|
+
}>;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
/** accessible-autocomplete's English text. */
|
|
19
|
+
export declare const defaults: {
|
|
20
|
+
'status-query-too-short': string;
|
|
21
|
+
'status-no-results': string;
|
|
22
|
+
'status-selected-option': string;
|
|
23
|
+
'status-results': {
|
|
24
|
+
one: string;
|
|
25
|
+
other: string;
|
|
26
|
+
};
|
|
27
|
+
};
|
|
@@ -0,0 +1,597 @@
|
|
|
1
|
+
/*! Based on accessible-autocomplete, Copyright (c) 2017 Crown Copyright (Government Digital Service), MIT licence: licences/accessible-autocomplete.txt */
|
|
2
|
+
var _a;
|
|
3
|
+
/*
|
|
4
|
+
* <govuk-react-autocomplete>: a headless, light-DOM custom element that suggests options as the
|
|
5
|
+
* user types into a text input, with or without React. A port of GOV.UK's accessible-autocomplete
|
|
6
|
+
* (https://github.com/alphagov/accessible-autocomplete), which enhances an existing input rather
|
|
7
|
+
* than rendering its own.
|
|
8
|
+
*
|
|
9
|
+
* Without JavaScript the input is a plain input and the options stay hidden. On upgrade the input
|
|
10
|
+
* becomes a combobox, as in accessible-autocomplete: options matching the query show in the
|
|
11
|
+
* listbox, Up and Down move focus through them, Enter, Space or a click confirms one, and Escape
|
|
12
|
+
* closes the menu. Screen readers hear how many options there are once the user pauses.
|
|
13
|
+
*
|
|
14
|
+
* The options are whatever the listbox holds, so they can be rendered on the server, or replaced
|
|
15
|
+
* as the user types, such as from a `govuk-react-autocomplete:query` listener. The rendered
|
|
16
|
+
* markup is never moved, so React can keep rendering it.
|
|
17
|
+
*
|
|
18
|
+
* Markup inside the element:
|
|
19
|
+
* - `input[data-autocomplete-input]`: the input, with an `id`.
|
|
20
|
+
* - `[data-autocomplete-listbox]`: the options, rendered `hidden`.
|
|
21
|
+
* - `[data-autocomplete-option]`: an option. Its value is `data-value`, or its text.
|
|
22
|
+
* - `[data-autocomplete-no-results]` (optional): shown when no option matches, rendered `hidden`.
|
|
23
|
+
* - `[data-autocomplete-status]` (optional): visually hidden, holding two
|
|
24
|
+
* `[data-autocomplete-status-region]`s, each with `role="status"`. They're updated alternately,
|
|
25
|
+
* so a repeated message is still announced.
|
|
26
|
+
* - `[data-autocomplete-hint]` (optional): how to use the options, rendered `hidden`. It
|
|
27
|
+
* describes the input while the query is empty.
|
|
28
|
+
*
|
|
29
|
+
* The element gets `data-focused` while focus is in it, and the option in focus, or highlighted
|
|
30
|
+
* while focus is on the input, gets `data-focused`.
|
|
31
|
+
*
|
|
32
|
+
* Attributes:
|
|
33
|
+
* - `min-length`: the length the query needs before options show. 0 by default.
|
|
34
|
+
* - `filter`: which options match the query: "contains" (the default), "starts-with", or "none"
|
|
35
|
+
* for options that already match, such as results from a server.
|
|
36
|
+
* - `confirm-on-blur`: "false" to leave the query alone when focus leaves an option. By default,
|
|
37
|
+
* as in accessible-autocomplete, the highlighted option is confirmed.
|
|
38
|
+
* - `submit-on-confirm`: submit the input's form when the user confirms an option, and let Enter
|
|
39
|
+
* submit it while the menu is open with no option highlighted.
|
|
40
|
+
* - `status-query-too-short`, `status-no-results`, `status-selected-option`, `status-results`:
|
|
41
|
+
* what screen readers hear, English by default, with `%{count}` for the number and, in
|
|
42
|
+
* `status-selected-option`, `%{option}` and `%{position}`. Add a plural form to the name, such
|
|
43
|
+
* as `status-results-one`, for a message for each form.
|
|
44
|
+
*
|
|
45
|
+
* Events, which bubble:
|
|
46
|
+
* - `govuk-react-autocomplete:query`, when the user changes the query and options are due.
|
|
47
|
+
* - `govuk-react-autocomplete:confirm`, when an option is confirmed. Cancel it to stop
|
|
48
|
+
* `submit-on-confirm` submitting the form.
|
|
49
|
+
*/
|
|
50
|
+
import { translate, translationAttributeNames, translationFromAttributes, withLocale, } from '../utils/translation.js';
|
|
51
|
+
import { setText, setAttribute } from '../utils/dom.js';
|
|
52
|
+
export const tagName = 'govuk-react-autocomplete';
|
|
53
|
+
/** accessible-autocomplete's English text. */
|
|
54
|
+
export const defaults = {
|
|
55
|
+
'status-query-too-short': 'Type in %{count} or more characters for results',
|
|
56
|
+
'status-no-results': 'No search results',
|
|
57
|
+
'status-selected-option': '%{option} %{position} of %{count} is highlighted',
|
|
58
|
+
'status-results': { one: '%{count} result is available.', other: '%{count} results are available.' },
|
|
59
|
+
};
|
|
60
|
+
/** As accessible-autocomplete, which waits this long after the last change before announcing. */
|
|
61
|
+
const statusDelay = 1400;
|
|
62
|
+
/** As accessible-autocomplete, which polls for speech recognition software changing the value. */
|
|
63
|
+
const pollInterval = 100;
|
|
64
|
+
const isIosDevice = () => typeof navigator !== 'undefined' &&
|
|
65
|
+
/(iPod|iPhone|iPad)/.test(navigator.userAgent) &&
|
|
66
|
+
/AppleWebKit/.test(navigator.userAgent);
|
|
67
|
+
const valueSetter = typeof HTMLInputElement === 'undefined'
|
|
68
|
+
? undefined
|
|
69
|
+
: (_a = Object.getOwnPropertyDescriptor(HTMLInputElement.prototype, 'value')) === null || _a === void 0 ? void 0 : _a.set;
|
|
70
|
+
if (typeof HTMLElement !== 'undefined' && typeof customElements !== 'undefined') {
|
|
71
|
+
class GovukReactAutocomplete extends HTMLElement {
|
|
72
|
+
constructor() {
|
|
73
|
+
super(...arguments);
|
|
74
|
+
// accessible-autocomplete's state. `focused` and `selected` are -1 for the input.
|
|
75
|
+
this.focused = null;
|
|
76
|
+
this.hovered = null;
|
|
77
|
+
this.menuOpen = false;
|
|
78
|
+
this.options = [];
|
|
79
|
+
this.currentQuery = '';
|
|
80
|
+
this.validChoiceMade = false;
|
|
81
|
+
this.selected = null;
|
|
82
|
+
this.ariaHint = true;
|
|
83
|
+
this.lastFocused = null;
|
|
84
|
+
this.settingValue = false;
|
|
85
|
+
this.bump = false;
|
|
86
|
+
this.generatedIds = 0;
|
|
87
|
+
this.onPoll = () => {
|
|
88
|
+
// Speech recognition software, such as Dragon NaturallySpeaking, can set the value directly.
|
|
89
|
+
if (this.input && this.input.value !== this.currentQuery)
|
|
90
|
+
this.handleInputChange(this.input.value);
|
|
91
|
+
};
|
|
92
|
+
this.onInput = () => {
|
|
93
|
+
if (this.input && !this.settingValue)
|
|
94
|
+
this.handleInputChange(this.input.value);
|
|
95
|
+
};
|
|
96
|
+
this.onInputFocus = () => {
|
|
97
|
+
const reopen = !this.validChoiceMade && this.queryLongEnough && this.options.length > 0;
|
|
98
|
+
this.focused = -1;
|
|
99
|
+
if (reopen) {
|
|
100
|
+
this.menuOpen = true;
|
|
101
|
+
this.selected = -1;
|
|
102
|
+
}
|
|
103
|
+
this.render();
|
|
104
|
+
};
|
|
105
|
+
this.onFocusOut = (event) => {
|
|
106
|
+
const { target, relatedTarget } = event;
|
|
107
|
+
if (target === this.input) {
|
|
108
|
+
// Focus moving to an option has already set `focused`.
|
|
109
|
+
if (this.focused !== -1)
|
|
110
|
+
return;
|
|
111
|
+
const ios = isIosDevice();
|
|
112
|
+
this.handleComponentBlur({
|
|
113
|
+
menuOpen: this.menuOpen && ios,
|
|
114
|
+
query: ios ? this.currentQuery : this.valueOf(this.selectedOption),
|
|
115
|
+
});
|
|
116
|
+
return;
|
|
117
|
+
}
|
|
118
|
+
// An option React removed while focused: the new options move focus back to the input.
|
|
119
|
+
if (!(target instanceof HTMLElement) || !target.isConnected)
|
|
120
|
+
return;
|
|
121
|
+
const index = this.options.indexOf(target);
|
|
122
|
+
if (index === -1)
|
|
123
|
+
return;
|
|
124
|
+
const focusingOutsideComponent = relatedTarget === null;
|
|
125
|
+
const focusingInput = relatedTarget === this.input;
|
|
126
|
+
const focusingAnotherOption = this.focused !== index && this.focused !== -1;
|
|
127
|
+
if ((!focusingAnotherOption && focusingOutsideComponent) || !(focusingAnotherOption || focusingInput)) {
|
|
128
|
+
this.handleComponentBlur({
|
|
129
|
+
menuOpen: this.menuOpen && isIosDevice(),
|
|
130
|
+
query: this.valueOf(this.selectedOption),
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
};
|
|
134
|
+
this.onKeyDown = (event) => {
|
|
135
|
+
var _a, _b, _c;
|
|
136
|
+
const { target } = event;
|
|
137
|
+
const onInput = target === this.input;
|
|
138
|
+
const onOption = target instanceof HTMLElement && this.options.includes(target);
|
|
139
|
+
if (!onInput && !onOption)
|
|
140
|
+
return;
|
|
141
|
+
const selected = (_a = this.selected) !== null && _a !== void 0 ? _a : -1;
|
|
142
|
+
switch (event.key) {
|
|
143
|
+
case 'ArrowUp':
|
|
144
|
+
event.preventDefault();
|
|
145
|
+
if (this.menuOpen && selected !== -1)
|
|
146
|
+
this.focusOption(selected - 1);
|
|
147
|
+
break;
|
|
148
|
+
case 'ArrowDown':
|
|
149
|
+
event.preventDefault();
|
|
150
|
+
if (this.menuOpen && selected !== this.options.length - 1)
|
|
151
|
+
this.focusOption(selected + 1);
|
|
152
|
+
break;
|
|
153
|
+
case ' ':
|
|
154
|
+
if (this.focused !== null && this.focused !== -1) {
|
|
155
|
+
event.preventDefault();
|
|
156
|
+
this.confirm(this.focused);
|
|
157
|
+
}
|
|
158
|
+
break;
|
|
159
|
+
case 'Enter':
|
|
160
|
+
if (!this.menuOpen)
|
|
161
|
+
break;
|
|
162
|
+
if (selected >= 0) {
|
|
163
|
+
event.preventDefault();
|
|
164
|
+
this.confirm(selected);
|
|
165
|
+
}
|
|
166
|
+
else if (!this.submitOnConfirm) {
|
|
167
|
+
event.preventDefault();
|
|
168
|
+
}
|
|
169
|
+
break;
|
|
170
|
+
case 'Escape':
|
|
171
|
+
// Rather than leave focus on a hidden option.
|
|
172
|
+
if (onOption)
|
|
173
|
+
(_b = this.input) === null || _b === void 0 ? void 0 : _b.focus();
|
|
174
|
+
this.handleComponentBlur({ menuOpen: false, query: this.currentQuery });
|
|
175
|
+
break;
|
|
176
|
+
default:
|
|
177
|
+
if (onOption &&
|
|
178
|
+
!event.ctrlKey &&
|
|
179
|
+
!event.metaKey &&
|
|
180
|
+
!event.altKey &&
|
|
181
|
+
(event.key.length === 1 || event.key === 'Backspace')) {
|
|
182
|
+
(_c = this.input) === null || _c === void 0 ? void 0 : _c.focus();
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
};
|
|
186
|
+
// Stops the input blurring, so a click confirms the option instead of closing the menu.
|
|
187
|
+
this.onOptionMouseDown = (event) => {
|
|
188
|
+
if (this.optionOf(event.target) !== -1)
|
|
189
|
+
event.preventDefault();
|
|
190
|
+
};
|
|
191
|
+
this.onOptionClick = (event) => {
|
|
192
|
+
const index = this.optionOf(event.target);
|
|
193
|
+
if (index !== -1)
|
|
194
|
+
this.confirm(index);
|
|
195
|
+
};
|
|
196
|
+
this.onOptionMouseOver = (event) => {
|
|
197
|
+
const index = this.optionOf(event.target);
|
|
198
|
+
// As accessible-autocomplete: iOS Safari drops the click if hovering changes the option.
|
|
199
|
+
if (index === -1 || index === this.hovered || isIosDevice())
|
|
200
|
+
return;
|
|
201
|
+
this.hovered = index;
|
|
202
|
+
this.render();
|
|
203
|
+
};
|
|
204
|
+
this.onListMouseLeave = () => {
|
|
205
|
+
if (this.hovered === null)
|
|
206
|
+
return;
|
|
207
|
+
this.hovered = null;
|
|
208
|
+
this.render();
|
|
209
|
+
};
|
|
210
|
+
}
|
|
211
|
+
static get observedAttributes() {
|
|
212
|
+
return [
|
|
213
|
+
'min-length',
|
|
214
|
+
'filter',
|
|
215
|
+
'confirm-on-blur',
|
|
216
|
+
'submit-on-confirm',
|
|
217
|
+
...Object.keys(defaults).flatMap(translationAttributeNames),
|
|
218
|
+
];
|
|
219
|
+
}
|
|
220
|
+
get query() {
|
|
221
|
+
return this.input ? this.currentQuery : undefined;
|
|
222
|
+
}
|
|
223
|
+
connectedCallback() {
|
|
224
|
+
// The parts may be parsed or rendered later, and React can replace the options at any time.
|
|
225
|
+
this.observer = new MutationObserver((records) => this.onMutation(records));
|
|
226
|
+
this.observer.observe(this, {
|
|
227
|
+
childList: true,
|
|
228
|
+
subtree: true,
|
|
229
|
+
characterData: true,
|
|
230
|
+
attributes: true,
|
|
231
|
+
attributeFilter: ['aria-describedby', 'data-value'],
|
|
232
|
+
});
|
|
233
|
+
this.setup();
|
|
234
|
+
}
|
|
235
|
+
disconnectedCallback() {
|
|
236
|
+
var _a;
|
|
237
|
+
(_a = this.observer) === null || _a === void 0 ? void 0 : _a.disconnect();
|
|
238
|
+
this.unbind();
|
|
239
|
+
}
|
|
240
|
+
attributeChangedCallback() {
|
|
241
|
+
// Wait for every attribute: React sets them one by one before connecting the element.
|
|
242
|
+
if (!this.isConnected)
|
|
243
|
+
return;
|
|
244
|
+
if (this.input)
|
|
245
|
+
this.render();
|
|
246
|
+
else
|
|
247
|
+
this.setup();
|
|
248
|
+
}
|
|
249
|
+
own(selector) {
|
|
250
|
+
return this.ownAll(selector)[0];
|
|
251
|
+
}
|
|
252
|
+
ownAll(selector) {
|
|
253
|
+
return Array.from(this.querySelectorAll(selector)).filter((element) => element.closest(tagName) === this);
|
|
254
|
+
}
|
|
255
|
+
get allOptions() {
|
|
256
|
+
return this.ownAll('[data-autocomplete-option]');
|
|
257
|
+
}
|
|
258
|
+
get minLength() {
|
|
259
|
+
const value = Number(this.getAttribute('min-length'));
|
|
260
|
+
return Number.isFinite(value) && value > 0 ? value : 0;
|
|
261
|
+
}
|
|
262
|
+
get filterType() {
|
|
263
|
+
const filter = this.getAttribute('filter');
|
|
264
|
+
return filter === 'starts-with' || filter === 'none' ? filter : 'contains';
|
|
265
|
+
}
|
|
266
|
+
get confirmOnBlur() {
|
|
267
|
+
return this.getAttribute('confirm-on-blur') !== 'false';
|
|
268
|
+
}
|
|
269
|
+
get submitOnConfirm() {
|
|
270
|
+
return this.hasAttribute('submit-on-confirm');
|
|
271
|
+
}
|
|
272
|
+
get locale() {
|
|
273
|
+
var _a, _b;
|
|
274
|
+
return ((_b = ((_a = this.input) !== null && _a !== void 0 ? _a : this).closest('[lang]')) === null || _b === void 0 ? void 0 : _b.getAttribute('lang')) || 'en';
|
|
275
|
+
}
|
|
276
|
+
setup() {
|
|
277
|
+
const input = this.own('[data-autocomplete-input]');
|
|
278
|
+
const listbox = this.own('[data-autocomplete-listbox]');
|
|
279
|
+
if (!(input instanceof HTMLInputElement) || !input.id || !listbox) {
|
|
280
|
+
this.unbind();
|
|
281
|
+
delete this.dataset.upgraded;
|
|
282
|
+
return;
|
|
283
|
+
}
|
|
284
|
+
if (input === this.input && listbox === this.listbox) {
|
|
285
|
+
this.render();
|
|
286
|
+
return;
|
|
287
|
+
}
|
|
288
|
+
this.unbind();
|
|
289
|
+
this.input = input;
|
|
290
|
+
this.listbox = listbox;
|
|
291
|
+
input.addEventListener('input', this.onInput);
|
|
292
|
+
input.addEventListener('click', this.onInput);
|
|
293
|
+
input.addEventListener('focus', this.onInputFocus);
|
|
294
|
+
this.addEventListener('keydown', this.onKeyDown);
|
|
295
|
+
this.addEventListener('focusout', this.onFocusOut);
|
|
296
|
+
listbox.addEventListener('mousedown', this.onOptionMouseDown);
|
|
297
|
+
listbox.addEventListener('click', this.onOptionClick);
|
|
298
|
+
listbox.addEventListener('mouseover', this.onOptionMouseOver);
|
|
299
|
+
listbox.addEventListener('mouseleave', this.onListMouseLeave);
|
|
300
|
+
this.poll = window.setInterval(this.onPoll, pollInterval);
|
|
301
|
+
this.currentQuery = input.value;
|
|
302
|
+
this.ariaHint = input.value === '';
|
|
303
|
+
this.options = this.queryLongEnough ? this.matchingOptions() : [];
|
|
304
|
+
this.focused = document.activeElement === input ? -1 : null;
|
|
305
|
+
this.lastFocused = this.focused;
|
|
306
|
+
this.selected = null;
|
|
307
|
+
this.menuOpen = false;
|
|
308
|
+
this.validChoiceMade = false;
|
|
309
|
+
this.dataset.upgraded = '';
|
|
310
|
+
this.render();
|
|
311
|
+
}
|
|
312
|
+
unbind() {
|
|
313
|
+
const { input, listbox } = this;
|
|
314
|
+
window.clearInterval(this.poll);
|
|
315
|
+
window.clearTimeout(this.statusTimer);
|
|
316
|
+
if (!input || !listbox)
|
|
317
|
+
return;
|
|
318
|
+
input.removeEventListener('input', this.onInput);
|
|
319
|
+
input.removeEventListener('click', this.onInput);
|
|
320
|
+
input.removeEventListener('focus', this.onInputFocus);
|
|
321
|
+
this.removeEventListener('keydown', this.onKeyDown);
|
|
322
|
+
this.removeEventListener('focusout', this.onFocusOut);
|
|
323
|
+
listbox.removeEventListener('mousedown', this.onOptionMouseDown);
|
|
324
|
+
listbox.removeEventListener('click', this.onOptionClick);
|
|
325
|
+
listbox.removeEventListener('mouseover', this.onOptionMouseOver);
|
|
326
|
+
listbox.removeEventListener('mouseleave', this.onListMouseLeave);
|
|
327
|
+
this.input = undefined;
|
|
328
|
+
this.listbox = undefined;
|
|
329
|
+
}
|
|
330
|
+
onMutation(records) {
|
|
331
|
+
var _a, _b;
|
|
332
|
+
const status = this.own('[data-autocomplete-status]');
|
|
333
|
+
const relevant = records.filter((record) => {
|
|
334
|
+
var _a;
|
|
335
|
+
return !((_a = status === null || status === void 0 ? void 0 : status.contains(record.target)) !== null && _a !== void 0 ? _a : false) &&
|
|
336
|
+
!(record.type === 'attributes' && record.target === this.input && this.describedByIsCurrent());
|
|
337
|
+
});
|
|
338
|
+
if (!relevant.length)
|
|
339
|
+
return;
|
|
340
|
+
if (!((_a = this.input) === null || _a === void 0 ? void 0 : _a.isConnected) || !((_b = this.listbox) === null || _b === void 0 ? void 0 : _b.isConnected) || !this.contains(this.input)) {
|
|
341
|
+
this.setup();
|
|
342
|
+
return;
|
|
343
|
+
}
|
|
344
|
+
if (relevant.some((record) => { var _a; return (_a = this.listbox) === null || _a === void 0 ? void 0 : _a.contains(record.target); }))
|
|
345
|
+
this.updateOptions();
|
|
346
|
+
this.render();
|
|
347
|
+
}
|
|
348
|
+
/** New options from the page, as accessible-autocomplete's `source` callback gives them. */
|
|
349
|
+
updateOptions() {
|
|
350
|
+
const highlighted = this.selectedOption;
|
|
351
|
+
if (!this.queryLongEnough || this.currentQuery === '') {
|
|
352
|
+
this.options = [];
|
|
353
|
+
return;
|
|
354
|
+
}
|
|
355
|
+
this.options = this.matchingOptions();
|
|
356
|
+
if (this.focused === null || this.validChoiceMade)
|
|
357
|
+
return;
|
|
358
|
+
this.menuOpen = this.options.length > 0;
|
|
359
|
+
// Keep the highlighted option if it's still there, and otherwise go back to the input.
|
|
360
|
+
const index = highlighted ? this.options.indexOf(highlighted) : -1;
|
|
361
|
+
this.selected = index;
|
|
362
|
+
if (this.focused >= 0)
|
|
363
|
+
this.focused = index;
|
|
364
|
+
}
|
|
365
|
+
get queryLongEnough() {
|
|
366
|
+
return this.currentQuery.length >= this.minLength;
|
|
367
|
+
}
|
|
368
|
+
valueOf(option) {
|
|
369
|
+
var _a, _b, _c;
|
|
370
|
+
if (!option)
|
|
371
|
+
return undefined;
|
|
372
|
+
return (_c = (_a = option.dataset.value) !== null && _a !== void 0 ? _a : (_b = option.textContent) === null || _b === void 0 ? void 0 : _b.trim()) !== null && _c !== void 0 ? _c : '';
|
|
373
|
+
}
|
|
374
|
+
matchingOptions() {
|
|
375
|
+
const query = this.currentQuery.toLowerCase();
|
|
376
|
+
const filter = this.filterType;
|
|
377
|
+
return this.allOptions.filter((option) => {
|
|
378
|
+
var _a;
|
|
379
|
+
if (filter === 'none')
|
|
380
|
+
return true;
|
|
381
|
+
const value = ((_a = this.valueOf(option)) !== null && _a !== void 0 ? _a : '').toLowerCase();
|
|
382
|
+
return filter === 'starts-with' ? value.startsWith(query) : value.includes(query);
|
|
383
|
+
});
|
|
384
|
+
}
|
|
385
|
+
isQueryAnOption(query) {
|
|
386
|
+
return this.options.some((option) => { var _a; return ((_a = this.valueOf(option)) === null || _a === void 0 ? void 0 : _a.toLowerCase()) === query.toLowerCase(); });
|
|
387
|
+
}
|
|
388
|
+
handleInputChange(query) {
|
|
389
|
+
const queryChanged = this.currentQuery !== query;
|
|
390
|
+
this.currentQuery = query;
|
|
391
|
+
this.ariaHint = query === '';
|
|
392
|
+
if (query !== '' && queryChanged && this.queryLongEnough) {
|
|
393
|
+
this.dispatchEvent(new CustomEvent('govuk-react-autocomplete:query', { bubbles: true, detail: { query } }));
|
|
394
|
+
this.options = this.matchingOptions();
|
|
395
|
+
this.menuOpen = this.options.length > 0;
|
|
396
|
+
this.selected = -1;
|
|
397
|
+
this.validChoiceMade = false;
|
|
398
|
+
}
|
|
399
|
+
else if (query === '' || !this.queryLongEnough) {
|
|
400
|
+
this.menuOpen = false;
|
|
401
|
+
this.options = [];
|
|
402
|
+
}
|
|
403
|
+
this.render();
|
|
404
|
+
}
|
|
405
|
+
get selectedOption() {
|
|
406
|
+
return this.selected === null || this.selected < 0 ? undefined : this.options[this.selected];
|
|
407
|
+
}
|
|
408
|
+
handleComponentBlur({ menuOpen, query }) {
|
|
409
|
+
var _a;
|
|
410
|
+
let newQuery = this.currentQuery;
|
|
411
|
+
const option = this.selectedOption;
|
|
412
|
+
if (this.confirmOnBlur) {
|
|
413
|
+
newQuery = query || this.currentQuery;
|
|
414
|
+
if (option)
|
|
415
|
+
this.confirmEvent((_a = this.valueOf(option)) !== null && _a !== void 0 ? _a : '');
|
|
416
|
+
}
|
|
417
|
+
this.focused = null;
|
|
418
|
+
this.menuOpen = menuOpen;
|
|
419
|
+
this.selected = null;
|
|
420
|
+
this.validChoiceMade = this.isQueryAnOption(newQuery);
|
|
421
|
+
this.setQuery(newQuery);
|
|
422
|
+
this.render();
|
|
423
|
+
}
|
|
424
|
+
/** Sets the input's value as the user would, so React's `onChange` hears it. */
|
|
425
|
+
setQuery(query) {
|
|
426
|
+
this.currentQuery = query;
|
|
427
|
+
const { input } = this;
|
|
428
|
+
if (!input || input.value === query)
|
|
429
|
+
return;
|
|
430
|
+
this.settingValue = true;
|
|
431
|
+
if (valueSetter)
|
|
432
|
+
valueSetter.call(input, query);
|
|
433
|
+
else
|
|
434
|
+
input.value = query;
|
|
435
|
+
input.dispatchEvent(new Event('input', { bubbles: true }));
|
|
436
|
+
this.settingValue = false;
|
|
437
|
+
}
|
|
438
|
+
confirmEvent(value) {
|
|
439
|
+
return this.dispatchEvent(new CustomEvent('govuk-react-autocomplete:confirm', { bubbles: true, cancelable: true, detail: { value } }));
|
|
440
|
+
}
|
|
441
|
+
confirm(index) {
|
|
442
|
+
var _a, _b;
|
|
443
|
+
const option = this.options[index];
|
|
444
|
+
if (!option)
|
|
445
|
+
return;
|
|
446
|
+
const value = (_a = this.valueOf(option)) !== null && _a !== void 0 ? _a : '';
|
|
447
|
+
this.focused = -1;
|
|
448
|
+
this.hovered = null;
|
|
449
|
+
this.menuOpen = false;
|
|
450
|
+
this.selected = -1;
|
|
451
|
+
this.validChoiceMade = true;
|
|
452
|
+
this.setQuery(value);
|
|
453
|
+
this.render();
|
|
454
|
+
const proceed = this.confirmEvent(value);
|
|
455
|
+
// After React has updated a controlled input's state, so an `onSubmit` sees the option.
|
|
456
|
+
const form = (_b = this.input) === null || _b === void 0 ? void 0 : _b.form;
|
|
457
|
+
if (proceed && this.submitOnConfirm && form)
|
|
458
|
+
window.setTimeout(() => form.requestSubmit());
|
|
459
|
+
}
|
|
460
|
+
focusOption(index) {
|
|
461
|
+
this.focused = index;
|
|
462
|
+
this.hovered = null;
|
|
463
|
+
this.selected = index;
|
|
464
|
+
this.render();
|
|
465
|
+
}
|
|
466
|
+
optionOf(target) {
|
|
467
|
+
if (!(target instanceof Element))
|
|
468
|
+
return -1;
|
|
469
|
+
const option = target.closest('[data-autocomplete-option]');
|
|
470
|
+
return option ? this.options.indexOf(option) : -1;
|
|
471
|
+
}
|
|
472
|
+
ensureId(element, suffix) {
|
|
473
|
+
var _a;
|
|
474
|
+
if (!element.id) {
|
|
475
|
+
this.generatedIds += 1;
|
|
476
|
+
element.id = `${(_a = this.input) === null || _a === void 0 ? void 0 : _a.id}__${suffix}${suffix.endsWith('--') ? this.generatedIds : ''}`;
|
|
477
|
+
}
|
|
478
|
+
return element.id;
|
|
479
|
+
}
|
|
480
|
+
get hint() {
|
|
481
|
+
return this.own('[data-autocomplete-hint]');
|
|
482
|
+
}
|
|
483
|
+
describedBy() {
|
|
484
|
+
var _a;
|
|
485
|
+
const { input } = this;
|
|
486
|
+
if (!input)
|
|
487
|
+
return null;
|
|
488
|
+
const hint = this.hint;
|
|
489
|
+
const hintId = hint ? this.ensureId(hint, 'assistiveHint') : undefined;
|
|
490
|
+
const tokens = ((_a = input.getAttribute('aria-describedby')) !== null && _a !== void 0 ? _a : '')
|
|
491
|
+
.split(/\s+/)
|
|
492
|
+
.filter((token) => token && token !== hintId);
|
|
493
|
+
if (this.ariaHint && hintId)
|
|
494
|
+
tokens.push(hintId);
|
|
495
|
+
return tokens.length ? tokens.join(' ') : null;
|
|
496
|
+
}
|
|
497
|
+
describedByIsCurrent() {
|
|
498
|
+
var _a, _b;
|
|
499
|
+
return ((_b = (_a = this.input) === null || _a === void 0 ? void 0 : _a.getAttribute('aria-describedby')) !== null && _b !== void 0 ? _b : null) === this.describedBy();
|
|
500
|
+
}
|
|
501
|
+
/** Brings the markup up to date with the state, as accessible-autocomplete's render does. */
|
|
502
|
+
render() {
|
|
503
|
+
var _a, _b;
|
|
504
|
+
const { input, listbox } = this;
|
|
505
|
+
if (!input || !listbox)
|
|
506
|
+
return;
|
|
507
|
+
const { focused, hovered, menuOpen, options, selected } = this;
|
|
508
|
+
const inputFocused = focused === -1;
|
|
509
|
+
const showNoOptionsFound = inputFocused && options.length === 0 && this.currentQuery !== '' && this.queryLongEnough;
|
|
510
|
+
const optionFocused = focused !== null && focused !== -1;
|
|
511
|
+
const listboxId = this.ensureId(listbox, 'listbox');
|
|
512
|
+
setAttribute(listbox, 'role', 'listbox');
|
|
513
|
+
if (!listbox.hasAttribute('aria-labelledby'))
|
|
514
|
+
listbox.setAttribute('aria-labelledby', input.id);
|
|
515
|
+
listbox.hidden = !(menuOpen || showNoOptionsFound);
|
|
516
|
+
const all = this.allOptions;
|
|
517
|
+
all.forEach((option) => {
|
|
518
|
+
const index = options.indexOf(option);
|
|
519
|
+
this.ensureId(option, 'option--');
|
|
520
|
+
option.hidden = index === -1;
|
|
521
|
+
setAttribute(option, 'role', 'option');
|
|
522
|
+
setAttribute(option, 'tabindex', '-1');
|
|
523
|
+
setAttribute(option, 'aria-selected', focused === index && index !== -1 ? 'true' : 'false');
|
|
524
|
+
setAttribute(option, 'aria-posinset', index === -1 ? null : String(index + 1));
|
|
525
|
+
setAttribute(option, 'aria-setsize', index === -1 ? null : String(options.length));
|
|
526
|
+
const showFocused = index !== -1 && (focused === -1 ? selected === index : focused === index);
|
|
527
|
+
setAttribute(option, 'data-focused', showFocused && hovered === null ? '' : null);
|
|
528
|
+
});
|
|
529
|
+
const noResults = this.own('[data-autocomplete-no-results]');
|
|
530
|
+
if (noResults) {
|
|
531
|
+
noResults.hidden = !showNoOptionsFound;
|
|
532
|
+
setAttribute(noResults, 'role', 'option');
|
|
533
|
+
setAttribute(noResults, 'aria-disabled', 'true');
|
|
534
|
+
}
|
|
535
|
+
setAttribute(input, 'role', 'combobox');
|
|
536
|
+
setAttribute(input, 'autocomplete', 'off');
|
|
537
|
+
setAttribute(input, 'aria-autocomplete', 'list');
|
|
538
|
+
setAttribute(input, 'aria-controls', listboxId);
|
|
539
|
+
setAttribute(input, 'aria-expanded', menuOpen ? 'true' : 'false');
|
|
540
|
+
setAttribute(input, 'aria-activedescendant', optionFocused ? ((_b = (_a = options[focused]) === null || _a === void 0 ? void 0 : _a.id) !== null && _b !== void 0 ? _b : null) : null);
|
|
541
|
+
setAttribute(input, 'aria-describedby', this.describedBy());
|
|
542
|
+
setAttribute(this, 'data-focused', focused === null ? null : '');
|
|
543
|
+
// As accessible-autocomplete's componentDidUpdate: focus follows `focused`, and the query is
|
|
544
|
+
// selected when focus comes into the input.
|
|
545
|
+
if (focused !== this.lastFocused && focused !== null) {
|
|
546
|
+
const element = focused === -1 ? input : options[focused];
|
|
547
|
+
if (element && document.activeElement !== element)
|
|
548
|
+
element.focus();
|
|
549
|
+
// Some input types, such as email, have no selection.
|
|
550
|
+
if (focused === -1 && this.lastFocused === null && input.selectionStart !== null) {
|
|
551
|
+
input.setSelectionRange(0, input.value.length);
|
|
552
|
+
}
|
|
553
|
+
}
|
|
554
|
+
this.lastFocused = focused;
|
|
555
|
+
this.scheduleStatus();
|
|
556
|
+
}
|
|
557
|
+
message(name) {
|
|
558
|
+
var _a;
|
|
559
|
+
return (_a = translationFromAttributes(this, name)) !== null && _a !== void 0 ? _a : defaults[name];
|
|
560
|
+
}
|
|
561
|
+
statusText() {
|
|
562
|
+
const { locale, options } = this;
|
|
563
|
+
if (!this.queryLongEnough)
|
|
564
|
+
return translate(this.message('status-query-too-short'), this.minLength, locale);
|
|
565
|
+
if (options.length === 0)
|
|
566
|
+
return translate(this.message('status-no-results'), 0, locale);
|
|
567
|
+
const results = translate(this.message('status-results'), options.length, locale);
|
|
568
|
+
const option = this.valueOf(this.selectedOption);
|
|
569
|
+
if (option === undefined || this.selected === null)
|
|
570
|
+
return results;
|
|
571
|
+
const position = withLocale(locale, (l) => new Intl.NumberFormat(l)).format(this.selected + 1);
|
|
572
|
+
const highlighted = translate(this.message('status-selected-option'), options.length, locale)
|
|
573
|
+
.replaceAll('%{option}', () => option)
|
|
574
|
+
.replaceAll('%{position}', () => position);
|
|
575
|
+
return `${results} ${highlighted}`;
|
|
576
|
+
}
|
|
577
|
+
// As accessible-autocomplete: every change clears the status, which is set again once there
|
|
578
|
+
// have been no changes for a while, in the other region, so the same text is announced again.
|
|
579
|
+
// It stays silent once focus has left or the user has made a choice.
|
|
580
|
+
scheduleStatus() {
|
|
581
|
+
const regions = this.ownAll('[data-autocomplete-status-region]');
|
|
582
|
+
if (!regions.length)
|
|
583
|
+
return;
|
|
584
|
+
regions.forEach((region) => setText(region, ''));
|
|
585
|
+
window.clearTimeout(this.statusTimer);
|
|
586
|
+
this.statusTimer = window.setTimeout(() => {
|
|
587
|
+
this.bump = !this.bump;
|
|
588
|
+
const silenced = this.focused === null || this.validChoiceMade;
|
|
589
|
+
const text = silenced ? '' : this.statusText();
|
|
590
|
+
regions.forEach((region, index) => setText(region, (index === 0) === this.bump ? text : ''));
|
|
591
|
+
}, statusDelay);
|
|
592
|
+
}
|
|
593
|
+
}
|
|
594
|
+
if (!customElements.get(tagName))
|
|
595
|
+
customElements.define(tagName, GovukReactAutocomplete);
|
|
596
|
+
}
|
|
597
|
+
//# sourceMappingURL=element.js.map
|