-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(Listbox): Keyboard navigation (#778)
* fix: trim space when parsing engine URLs * refactor: add support for keyboard navigation * refactor: move key handling to own file test: cover new functionality with unit tests * refactor: rename and fix focus * refactor: improve filling the range * fix: adaptations to merge conflicts * refactor: restore style overrides for focus
- Loading branch information
1 parent
11a705c
commit 6d23e24
Showing
8 changed files
with
401 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
142 changes: 142 additions & 0 deletions
142
apis/nucleus/src/components/listbox/__tests__/list-box-keyboard-navigation.spec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
import getKeyboardNavigation from '../listbox-keyboard-navigation'; | ||
|
||
describe('keyboard navigation', () => { | ||
let actions; | ||
let sandbox; | ||
let handleKeyDown; | ||
|
||
before(() => { | ||
global.document = {}; | ||
sandbox = sinon.createSandbox(); | ||
actions = { | ||
select: sandbox.stub(), | ||
cancel: sandbox.stub(), | ||
confirm: sandbox.stub(), | ||
}; | ||
}); | ||
|
||
afterEach(() => { | ||
sandbox.reset(); | ||
}); | ||
|
||
after(() => { | ||
sandbox.restore(); | ||
}); | ||
|
||
beforeEach(() => { | ||
handleKeyDown = getKeyboardNavigation(actions); | ||
}); | ||
|
||
it('select values with Space', () => { | ||
expect(actions.select).not.called; | ||
expect(actions.confirm).not.called; | ||
expect(actions.cancel).not.called; | ||
|
||
// Space should select values | ||
const event = { | ||
nativeEvent: { keyCode: 32 }, | ||
currentTarget: { getAttribute: sandbox.stub().withArgs('data-n').returns(1) }, | ||
preventDefault: sandbox.stub(), | ||
stopPropagation: sandbox.stub(), | ||
}; | ||
handleKeyDown(event); | ||
expect(actions.select).calledOnce.calledWithExactly([1]); | ||
expect(event.preventDefault).calledOnce; | ||
expect(event.stopPropagation).not.called; | ||
}); | ||
|
||
it('confirm selections with Enter', () => { | ||
const eventConfirm = { | ||
nativeEvent: { keyCode: 13 }, | ||
preventDefault: sandbox.stub(), | ||
}; | ||
handleKeyDown(eventConfirm); | ||
expect(actions.confirm).calledOnce; | ||
}); | ||
|
||
it('cancel selections with Escape', () => { | ||
const eventCancel = { | ||
nativeEvent: { keyCode: 27 }, | ||
preventDefault: sandbox.stub(), | ||
}; | ||
const blur = sandbox.stub(); | ||
global.document.activeElement = { blur }; | ||
handleKeyDown(eventCancel); | ||
expect(actions.cancel).calledOnce; | ||
expect(blur).calledOnce; | ||
}); | ||
|
||
it('arrow up should move focus upwards', () => { | ||
const focus = sandbox.stub(); | ||
const eventArrowUp = { | ||
nativeEvent: { keyCode: 38 }, | ||
currentTarget: { | ||
parentElement: { | ||
previousElementSibling: { | ||
querySelector: () => ({ focus }), | ||
}, | ||
}, | ||
}, | ||
preventDefault: sandbox.stub(), | ||
}; | ||
handleKeyDown(eventArrowUp); | ||
expect(focus).calledOnce; | ||
}); | ||
|
||
it('arrow down should move focus downwards', () => { | ||
const focus = sandbox.stub(); | ||
const eventArrowDown = { | ||
nativeEvent: { keyCode: 40 }, | ||
currentTarget: { | ||
parentElement: { | ||
nextElementSibling: { | ||
querySelector: () => ({ focus }), | ||
}, | ||
}, | ||
}, | ||
preventDefault: sandbox.stub(), | ||
}; | ||
handleKeyDown(eventArrowDown); | ||
expect(focus).calledOnce; | ||
}); | ||
|
||
it('arrow down with Shift should range select values downwards (current and next element)', () => { | ||
const focus = sandbox.stub(); | ||
expect(actions.select).not.called; | ||
const eventArrowDown = { | ||
nativeEvent: { keyCode: 40, shiftKey: true }, | ||
currentTarget: { | ||
parentElement: { | ||
nextElementSibling: { | ||
querySelector: () => ({ focus, getAttribute: sandbox.stub().withArgs('data-n').returns(2) }), | ||
}, | ||
}, | ||
getAttribute: sandbox.stub().withArgs('data-n').returns(1), | ||
}, | ||
preventDefault: sandbox.stub(), | ||
}; | ||
handleKeyDown(eventArrowDown); | ||
expect(focus).calledOnce; | ||
expect(actions.select).calledOnce.calledWithExactly([2], true); | ||
}); | ||
|
||
it('arrow up with Shift should range select values upwards (current and previous element)', () => { | ||
const focus = sandbox.stub(); | ||
expect(actions.select).not.called; | ||
const eventArrowUp = { | ||
nativeEvent: { keyCode: 38, shiftKey: true }, | ||
currentTarget: { | ||
getAttribute: sandbox.stub().withArgs('data-n').returns(2), | ||
parentElement: { | ||
previousElementSibling: { | ||
querySelector: () => ({ focus, getAttribute: sandbox.stub().withArgs('data-n').returns(1) }), | ||
}, | ||
}, | ||
}, | ||
preventDefault: sandbox.stub(), | ||
}; | ||
handleKeyDown(eventArrowUp); | ||
expect(focus).calledOnce; | ||
expect(actions.select).calledOnce.calledWithExactly([1], true); | ||
}); | ||
}); |
Oops, something went wrong.