зеркало из https://github.com/github/combobox-nav.git
More changes
This commit is contained in:
Родитель
dfbf7c5c3e
Коммит
4365fb9948
19
src/index.ts
19
src/index.ts
|
@ -80,7 +80,6 @@ export default class Combobox {
|
|||
;(this.input as HTMLElement).addEventListener('keydown', this.keyboardEventHandler)
|
||||
this.list.addEventListener('click', commitWithElement)
|
||||
this.indicateDefaultOption()
|
||||
this.selectFirstItemIfNeeded()
|
||||
}
|
||||
|
||||
stop(): void {
|
||||
|
@ -98,11 +97,7 @@ export default class Combobox {
|
|||
Array.from(this.list.querySelectorAll<HTMLElement>('[role="option"]:not([aria-disabled="true"])'))
|
||||
.filter(visible)[0]
|
||||
?.setAttribute('data-combobox-option-default', 'true')
|
||||
}
|
||||
}
|
||||
|
||||
selectFirstItemIfNeeded(): void {
|
||||
if (this.firstOptionSelectionMode === 'selected') {
|
||||
} else if (this.firstOptionSelectionMode === 'selected') {
|
||||
this.navigate(1)
|
||||
}
|
||||
}
|
||||
|
@ -113,7 +108,7 @@ export default class Combobox {
|
|||
const focusIndex = els.indexOf(focusEl)
|
||||
|
||||
if ((focusIndex === els.length - 1 && indexDiff === 1) || (focusIndex === 0 && indexDiff === -1)) {
|
||||
this.clearSelection()
|
||||
this.resetSelection()
|
||||
this.input.focus()
|
||||
return
|
||||
}
|
||||
|
@ -146,10 +141,11 @@ export default class Combobox {
|
|||
for (const el of this.list.querySelectorAll('[aria-selected="true"]')) {
|
||||
el.removeAttribute('aria-selected')
|
||||
}
|
||||
}
|
||||
|
||||
if (this.firstOptionSelectionMode === 'active') {
|
||||
this.indicateDefaultOption()
|
||||
}
|
||||
resetSelection(): void {
|
||||
this.clearSelection()
|
||||
this.indicateDefaultOption()
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -194,8 +190,7 @@ function keyboardBindings(event: KeyboardEvent, combobox: Combobox) {
|
|||
break
|
||||
default:
|
||||
if (event.ctrlKey) break
|
||||
combobox.clearSelection()
|
||||
combobox.selectFirstItemIfNeeded()
|
||||
combobox.resetSelection()
|
||||
}
|
||||
}
|
||||
|
||||
|
|
10
test/test.js
10
test/test.js
|
@ -308,10 +308,16 @@ describe('combobox-nav', function () {
|
|||
assert.equal(document.querySelectorAll('[data-combobox-option-default]').length, 0)
|
||||
})
|
||||
|
||||
it('resets default indication when selection cleared', () => {
|
||||
it('resets default indication when selection reset', () => {
|
||||
combobox.navigate(1)
|
||||
combobox.resetSelection()
|
||||
assert.equal(document.querySelectorAll('[data-combobox-option-default]').length, 1)
|
||||
})
|
||||
|
||||
it('removes default indication when selection cleared', () => {
|
||||
combobox.navigate(1)
|
||||
combobox.clearSelection()
|
||||
assert.equal(document.querySelectorAll('[data-combobox-option-default]').length, 1)
|
||||
assert.equal(document.querySelectorAll('[data-combobox-option-default]').length, 0)
|
||||
})
|
||||
|
||||
it('does not error when no options are visible', () => {
|
||||
|
|
Загрузка…
Ссылка в новой задаче