Skip to content

Commit 369bf75

Browse files
authored
Merge branch 'main' into scroll-into-view
2 parents b0dec8e + 3fe250b commit 369bf75

File tree

3 files changed

+31
-0
lines changed

3 files changed

+31
-0
lines changed

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,16 @@ list.addEventListener('combobox-commit', function (event) {
6969
7070
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.
7171

72+
A bubbling `combobox-select` event is fired on the list element when an option is selected but not yet committed.
73+
74+
For example, autocomplete when an option is selected but not yet committed:
75+
76+
```js
77+
list.addEventListener('combobox-select', function (event) {
78+
console.log('Element selected : ', event.target)
79+
})
80+
```
81+
7282
## Settings
7383

7484
For advanced configuration, the constructor takes an optional third argument. For example:

src/index.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,9 @@ export default class Combobox {
110110
if (target === el) {
111111
this.input.setAttribute('aria-activedescendant', target.id)
112112
target.setAttribute('aria-selected', 'true')
113+
fireSelectEvent(target)
113114
target.scrollIntoView(this.scrollIntoViewOptions)
115+
scrollTo(this.list, target)
114116
} else {
115117
el.removeAttribute('aria-selected')
116118
}
@@ -191,6 +193,10 @@ function fireCommitEvent(target: Element, detail?: Record<string, unknown>): voi
191193
target.dispatchEvent(new CustomEvent('combobox-commit', {bubbles: true, detail}))
192194
}
193195

196+
function fireSelectEvent(target: Element): void {
197+
target.dispatchEvent(new Event('combobox-select', {bubbles: true}))
198+
}
199+
194200
function visible(el: HTMLElement): boolean {
195201
return (
196202
!el.hidden &&

test/test.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,21 @@ describe('combobox-nav', function () {
164164
assert.equal(expectedTargets[1], 'baymax')
165165
})
166166

167+
it('fires select events on navigating', function () {
168+
const expectedTargets = []
169+
170+
document.addEventListener('combobox-select', function ({target}) {
171+
expectedTargets.push(target.id)
172+
})
173+
174+
press(input, 'ArrowDown')
175+
press(input, 'ArrowDown')
176+
177+
assert.equal(expectedTargets.length, 2)
178+
assert.equal(expectedTargets[0], 'baymax')
179+
assert.equal(expectedTargets[1], 'hubot')
180+
})
181+
167182
it('clear selection on input operation', function () {
168183
press(input, 'ArrowDown')
169184
assert.equal(options[0].getAttribute('aria-selected'), 'true')

0 commit comments

Comments
 (0)