Skip to content

Commit

Permalink
Merge pull request #26 from github/clearselection
Browse files Browse the repository at this point in the history
Avoid clearning selection on ctrlKey
  • Loading branch information
muan authored May 12, 2020
2 parents f1dd8ee + d4ff922 commit 4cfd57e
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 14 deletions.
10 changes: 5 additions & 5 deletions .github/workflows/nodejs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,19 @@ on:
- master
jobs:
build:

runs-on: ubuntu-latest
runs-on: ${{ matrix.os }}

strategy:
fail-fast: false
matrix:
node-version: [8.x, 10.x, 12.x]
os: [ubuntu-latest, windows-latest, macos-latest]

steps:
- uses: actions/checkout@v1
- name: Use Node.js ${{ matrix.node-version }}
- name: Use Node.js 12.x
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}
node-version: 12.x
- name: npm install, build, and test
run: |
npm install
Expand Down
6 changes: 6 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export default class Combobox {
input: HTMLTextAreaElement | HTMLInputElement
keyboardEventHandler: (event: KeyboardEvent) => void
compositionEventHandler: (event: Event) => void
inputHandler: (event: Event) => void

constructor(input: HTMLTextAreaElement | HTMLInputElement, list: HTMLElement) {
this.input = input
Expand All @@ -20,6 +21,7 @@ export default class Combobox {

this.keyboardEventHandler = event => keyboardBindings(event, this)
this.compositionEventHandler = event => trackComposition(event, this)
this.inputHandler = this.clearSelection.bind(this)
input.setAttribute('role', 'combobox')
input.setAttribute('aria-controls', list.id)
input.setAttribute('aria-expanded', 'false')
Expand All @@ -42,6 +44,7 @@ export default class Combobox {
this.input.setAttribute('aria-expanded', 'true')
this.input.addEventListener('compositionstart', this.compositionEventHandler)
this.input.addEventListener('compositionend', this.compositionEventHandler)
this.input.addEventListener('input', this.inputHandler)
;(this.input as HTMLElement).addEventListener('keydown', this.keyboardEventHandler)
this.list.addEventListener('click', commitWithElement)
}
Expand All @@ -51,6 +54,7 @@ export default class Combobox {
this.input.setAttribute('aria-expanded', 'false')
this.input.removeEventListener('compositionstart', this.compositionEventHandler)
this.input.removeEventListener('compositionend', this.compositionEventHandler)
this.input.removeEventListener('input', this.inputHandler)
;(this.input as HTMLElement).removeEventListener('keydown', this.keyboardEventHandler)
this.list.removeEventListener('click', commitWithElement)
}
Expand Down Expand Up @@ -95,6 +99,7 @@ export default class Combobox {

function keyboardBindings(event: KeyboardEvent, combobox: Combobox) {
if (event.shiftKey || event.metaKey || event.altKey) return
if (!ctrlBindings && event.ctrlKey) return
if (combobox.isComposing) return

switch (event.key) {
Expand Down Expand Up @@ -128,6 +133,7 @@ function keyboardBindings(event: KeyboardEvent, combobox: Combobox) {
}
break
default:
if (event.ctrlKey) break
combobox.clearSelection()
}
}
Expand Down
24 changes: 15 additions & 9 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import Combobox from '../dist/index.js'

const ctrlBindings = !!navigator.userAgent.match(/Macintosh/)

function press(input, key, ctrlKey) {
input.dispatchEvent(new KeyboardEvent('keydown', {key, ctrlKey}))
}
Expand Down Expand Up @@ -105,17 +108,17 @@ describe('combobox-nav', function() {

press(input, 'Enter')

press(input, 'ArrowDown')
ctrlBindings ? press(input, 'n', true) : press(input, 'ArrowDown')
assert.equal(options[2].getAttribute('aria-selected'), 'true')
assert.equal(input.getAttribute('aria-activedescendant'), 'r2-d2')

press(input, 'ArrowDown')
ctrlBindings ? press(input, 'n', true) : press(input, 'ArrowDown')
assert.equal(options[4].getAttribute('aria-selected'), 'true')
assert.equal(input.getAttribute('aria-activedescendant'), 'wall-e')
press(input, 'Enter')
click(document.getElementById('wall-e'))

press(input, 'ArrowDown')
ctrlBindings ? press(input, 'n', true) : press(input, 'ArrowDown')
assert.equal(options[5].getAttribute('aria-selected'), 'true')
assert.equal(input.getAttribute('aria-activedescendant'), 'link')

Expand All @@ -127,19 +130,18 @@ describe('combobox-nav', function() {
assert.equal(options[0].getAttribute('aria-selected'), 'true')
assert.equal(input.getAttribute('aria-activedescendant'), 'baymax')

press(input, 'ArrowUp')
ctrlBindings ? press(input, 'p', true) : press(input, 'ArrowUp')
assert(!list.querySelector('[aria-selected=true]'), 'Nothing should be selected')
assert(!input.hasAttribute('aria-activedescendant'), 'Nothing should be selected')

press(input, 'ArrowDown')
press(input, 'ArrowDown')
assert.equal(options[1].getAttribute('aria-selected'), 'true')
assert.equal(input.getAttribute('aria-activedescendant'), 'hubot')
press(input, 'ArrowUp')
assert.equal(options[5].getAttribute('aria-selected'), 'true')
assert.equal(input.getAttribute('aria-activedescendant'), 'link')

press(input, 'Enter')
assert.equal(expectedTargets.length, 2)
assert.equal(expectedTargets[0], 'hubot')
assert.equal(expectedTargets[1], 'hubot')
assert.equal(expectedTargets[1], 'link')
})

it('fires commit events on click', function() {
Expand Down Expand Up @@ -171,6 +173,10 @@ describe('combobox-nav', function() {
assert.equal(options[0].getAttribute('aria-selected'), 'true')
assert.equal(input.getAttribute('aria-activedescendant'), 'baymax')

press(input, 'Control', true)
assert.equal(options[0].getAttribute('aria-selected'), 'true', 'Selection stays on modifier keydown')
assert.equal(input.getAttribute('aria-activedescendant'), 'baymax', 'Selection stays on modifier keydown')

press(input, 'Backspace')
assert(!list.querySelector('[aria-selected=true]'), 'Nothing should be selected')
assert(!input.hasAttribute('aria-activedescendant'), 'Nothing should be selected')
Expand Down

0 comments on commit 4cfd57e

Please sign in to comment.