Merge branch 'main' into scroll-into-view

This commit is contained in:
Keith Cirkel 2023-09-25 10:06:51 +01:00 коммит произвёл GitHub
Родитель b0dec8e73d 3fe250b4b7
Коммит 369bf75ab0
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
3 изменённых файлов: 31 добавлений и 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:

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

@ -110,7 +110,9 @@ export default class Combobox {
if (target === el) {
this.input.setAttribute('aria-activedescendant', target.id)
target.setAttribute('aria-selected', 'true')
fireSelectEvent(target)
target.scrollIntoView(this.scrollIntoViewOptions)
scrollTo(this.list, target)
} else {
el.removeAttribute('aria-selected')
}
@ -191,6 +193,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 Event('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')