|
3 | 3 | <head> |
4 | 4 | <meta charset="utf-8"> |
5 | 5 | <title>combobox-nav demo</title> |
6 | | - <!-- <script type="text/javascript" src="../dist/combobox-nav.umd.js"></script> --> |
7 | | - <script type="text/javascript" src="https://unpkg.com/@github/combobox-nav@latest/dist/combobox-nav.umd.js"></script> |
8 | 6 | <style>[aria-selected="true"] { font-weight: bold; }</style> |
9 | 7 | </head> |
10 | 8 | <body> |
|
21 | 19 | <li role="presentation"><a id="wall-e" href="#wall-e" role="option">Wall-E</a></li> |
22 | 20 | </ul> |
23 | 21 | </form> |
24 | | - <pre class="events"></pre> |
25 | | - <script type="text/javascript"> |
| 22 | + <pre class="events" aria-live="polite"></pre> |
| 23 | + |
| 24 | + <script type="module"> |
| 25 | + // import Combobox from '../dist/index.js' |
| 26 | + import Combobox from 'https://unpkg.com/@github/combobox-nav@latest/dist/index.js' |
26 | 27 | const input = document.querySelector('input') |
27 | 28 | const list = document.querySelector('ul') |
28 | | - comboboxNav.install(input, list) |
| 29 | + const comboboxController = new Combobox(input, list) |
29 | 30 |
|
30 | | - function toggleList() { |
31 | | - const hidden = input.value.length === 0 |
| 31 | + function toggleList(show) { |
| 32 | + const hidden = show === true ? false : input.value.length === 0 |
32 | 33 | if (hidden) { |
33 | | - comboboxNav.stop(input) |
| 34 | + comboboxController.stop() |
34 | 35 | } else { |
35 | | - comboboxNav.start(input) |
| 36 | + comboboxController.start() |
36 | 37 | } |
37 | 38 | list.hidden = hidden |
38 | 39 | } |
39 | | - input.addEventListener('input', toggleList) |
40 | | - input.addEventListener('focus', toggleList) |
| 40 | + input.addEventListener('keydown', event => { |
| 41 | + if (!['ArrowDown', 'ArrowUp'].includes(event.key) || !list.hidden) return |
| 42 | + toggleList(true) |
| 43 | + comboboxController.navigate(event.key === 'ArrowDown' ? 1 : -1) |
| 44 | + }) |
| 45 | + input.addEventListener('input', () => toggleList()) |
| 46 | + input.addEventListener('focus', () => toggleList()) |
41 | 47 | input.addEventListener('blur', () => { |
42 | 48 | list.hidden = true |
43 | | - comboboxNav.stop(input) |
| 49 | + comboboxController.clearSelection() |
| 50 | + comboboxController.stop() |
44 | 51 | }) |
45 | 52 |
|
46 | 53 | const events = document.querySelector('.events') |
47 | 54 | document.addEventListener('combobox-commit', function(event) { |
48 | | - events.append(`combobox-commit event on ${event.target.textContent}\n`) |
| 55 | + events.append(`${event.target.textContent} committed, combobox-commit event fired\n`) |
| 56 | + list.hidden = true |
| 57 | + comboboxController.clearSelection() |
| 58 | + comboboxController.stop() |
49 | 59 | }) |
50 | 60 | </script> |
51 | 61 | </body> |
|
0 commit comments