introduce combobox-select event

This commit is contained in:
Nico Wenterodt 2023-09-14 15:55:27 +02:00
Родитель a3d21782fe
Коммит e4401286f3
3 изменённых файлов: 30 добавлений и 0 удалений

Просмотреть файл

@ -69,6 +69,16 @@ list.addEventListener('combobox-commit', function (event) {
When a label is clicked on, `click` event is fired from both `<label>` and its associated input `label.control`. Since combobox does not know about the control, `combobox-commit` cannot be used as an indicator of the item's selection state.
A bubbling `combobox-select` event is fired on the list element when an option is selected but not yet committed.
For example, autocomplete when an option is selected but not yet committed:
```js
list.addEventListener('combobox-select', function (event) {
console.log('Element selected : ', event.target)
})
```
## Settings
For advanced configuration, the constructor takes an optional third argument. For example:

Просмотреть файл

@ -107,6 +107,7 @@ export default class Combobox {
if (target === el) {
this.input.setAttribute('aria-activedescendant', target.id)
target.setAttribute('aria-selected', 'true')
fireSelectEvent(target)
scrollTo(this.list, target)
} else {
el.removeAttribute('aria-selected')
@ -188,6 +189,10 @@ function fireCommitEvent(target: Element, detail?: Record<string, unknown>): voi
target.dispatchEvent(new CustomEvent('combobox-commit', {bubbles: true, detail}))
}
function fireSelectEvent(target: Element): void {
target.dispatchEvent(new CustomEvent('combobox-select', {bubbles: true}))
}
function visible(el: HTMLElement): boolean {
return (
!el.hidden &&

Просмотреть файл

@ -164,6 +164,21 @@ describe('combobox-nav', function () {
assert.equal(expectedTargets[1], 'baymax')
})
it('fires select events on navigating', function () {
const expectedTargets = []
document.addEventListener('combobox-select', function ({target}) {
expectedTargets.push(target.id)
})
press(input, 'ArrowDown')
press(input, 'ArrowDown')
assert.equal(expectedTargets.length, 2)
assert.equal(expectedTargets[0], 'baymax')
assert.equal(expectedTargets[1], 'hubot')
})
it('clear selection on input operation', function () {
press(input, 'ArrowDown')
assert.equal(options[0].getAttribute('aria-selected'), 'true')