Skip to content

Commit 3fe250b

Browse files
authored
Merge pull request #76 from nicowenterodt/combobox-select-event
Introduce combobox-select event
2 parents a3d2178 + da9f673 commit 3fe250b

File tree

3 files changed

+30
-0
lines changed

3 files changed

+30
-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: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ export default class Combobox {
107107
if (target === el) {
108108
this.input.setAttribute('aria-activedescendant', target.id)
109109
target.setAttribute('aria-selected', 'true')
110+
fireSelectEvent(target)
110111
scrollTo(this.list, target)
111112
} else {
112113
el.removeAttribute('aria-selected')
@@ -188,6 +189,10 @@ function fireCommitEvent(target: Element, detail?: Record<string, unknown>): voi
188189
target.dispatchEvent(new CustomEvent('combobox-commit', {bubbles: true, detail}))
189190
}
190191

192+
function fireSelectEvent(target: Element): void {
193+
target.dispatchEvent(new Event('combobox-select', {bubbles: true}))
194+
}
195+
191196
function visible(el: HTMLElement): boolean {
192197
return (
193198
!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)