|
1 | | -import { |
2 | | - Button, |
3 | | - CharacterCount, |
4 | | - Checkboxes, |
5 | | - ErrorSummary, |
6 | | - Header, |
7 | | - NotificationBanner, |
8 | | - Radios, |
9 | | - SkipLink, |
10 | | - createAll |
11 | | -} from 'govuk-frontend' |
| 1 | +import { initAll } from '~/src/client/javascripts/shared.js' |
12 | 2 |
|
13 | | -createAll(Button) |
14 | | -createAll(CharacterCount) |
15 | | -createAll(Checkboxes) |
16 | | -createAll(ErrorSummary) |
17 | | -createAll(Header) |
18 | | -createAll(NotificationBanner) |
19 | | -createAll(Radios) |
20 | | -createAll(SkipLink) |
21 | | - |
22 | | -// Show preview close link via `rel="opener"` |
23 | | -if (window.opener) { |
24 | | - const $closeLink = document.querySelector('.js-preview-banner-close') |
25 | | - |
26 | | - $closeLink?.removeAttribute('hidden') |
27 | | - $closeLink?.addEventListener('click', (event) => { |
28 | | - event.preventDefault() |
29 | | - window.close() |
30 | | - }) |
31 | | -} |
32 | | - |
33 | | -/** |
34 | | - * Initialise autocomplete |
35 | | - * @param {HTMLSelectElement | null} $select |
36 | | - * @param {(config: object) => void} init |
37 | | - */ |
38 | | -function initAutocomplete($select, init) { |
39 | | - if (!$select) { |
40 | | - return |
41 | | - } |
42 | | - |
43 | | - const config = { |
44 | | - id: $select.id, |
45 | | - selectElement: $select |
46 | | - } |
47 | | - |
48 | | - init(config) |
49 | | - |
50 | | - /** @type {HTMLInputElement | null} */ |
51 | | - const $input = document.querySelector(`#${config.id}`) |
52 | | - |
53 | | - // Allowed values for input |
54 | | - const inputValues = [...$select.options].map((option) => option.text) |
55 | | - |
56 | | - // Reset select when input value is not allowed |
57 | | - $input?.addEventListener('blur', () => { |
58 | | - if (!$input.value || !inputValues.includes($input.value)) { |
59 | | - $select.value = '' |
60 | | - } |
61 | | - }) |
62 | | -} |
63 | | - |
64 | | -// Find all autocompletes |
65 | | -const $autocompletes = document.querySelectorAll( |
66 | | - `[data-module="govuk-accessible-autocomplete"]` |
67 | | -) |
68 | | - |
69 | | -// Lazy load autocomplete component |
70 | | -if ($autocompletes.length) { |
71 | | - // @ts-expect-error -- No types available |
72 | | - import('accessible-autocomplete') |
73 | | - .then((component) => { |
74 | | - const { default: accessibleAutocomplete } = component |
75 | | - |
76 | | - // Initialise each autocomplete |
77 | | - $autocompletes.forEach(($module) => |
78 | | - initAutocomplete( |
79 | | - $module.querySelector('select'), |
80 | | - accessibleAutocomplete.enhanceSelectElement |
81 | | - ) |
82 | | - ) |
83 | | - }) |
84 | | - |
85 | | - // eslint-disable-next-line no-console |
86 | | - .catch(console.error) |
87 | | -} |
| 3 | +initAll() |
0 commit comments