Skip to content

Commit

Permalink
use keyboard constants
Browse files Browse the repository at this point in the history
  • Loading branch information
jhchen committed Mar 12, 2018
1 parent cf48ce8 commit ae99e07
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 17 deletions.
12 changes: 4 additions & 8 deletions test/unit/ui/picker.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
import Keyboard from '../../../modules/keyboard';
import Picker from '../../../ui/picker';

const KEYCODES = {
ENTER: 13,
ESCAPE: 27,
}

function createKeydownEvent(keyCode) {
let event;
if (typeof Event === 'function') {
Expand Down Expand Up @@ -64,7 +60,7 @@ describe('Picker', function() {
const pickerLabel = this.pickerSelector.querySelector('.ql-picker-label');

// Select picker via enter key
const e = createKeydownEvent(KEYCODES.ENTER);
const e = createKeydownEvent(Keyboard.keys.ENTER);
pickerLabel.dispatchEvent(e);

expect(pickerLabel.getAttribute('aria-expanded')).toEqual('true');
Expand Down Expand Up @@ -110,7 +106,7 @@ describe('Picker', function() {

const pickerItem = this.pickerSelector.querySelector('.ql-picker-item');
// Select picker item via enter key
const e = createKeydownEvent(KEYCODES.ENTER);
const e = createKeydownEvent(Keyboard.keys.ENTER);
pickerItem.dispatchEvent(e);

expect(pickerLabel.getAttribute('aria-expanded')).toEqual('false');
Expand All @@ -131,7 +127,7 @@ describe('Picker', function() {
pickerLabel.click();

// Escape out of the picker
const e = createKeydownEvent(KEYCODES.ESCAPE);
const e = createKeydownEvent(Keyboard.keys.ESCAPE);
pickerLabel.dispatchEvent(e);

expect(pickerLabel.getAttribute('aria-expanded')).toEqual('false');
Expand Down
14 changes: 5 additions & 9 deletions ui/picker.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
import Keyboard from '../modules/keyboard';
import DropdownIcon from '../assets/icons/dropdown.svg';

const KEYCODES = {
ENTER: 13,
ESCAPE: 27,
}

let OPTIONS_ID = 0;

function toggleAriaAttribute(element, attribute) {
Expand All @@ -25,12 +21,12 @@ class Picker {
this.label.addEventListener('keydown', (event) => {
switch(event.keyCode) {
// Allows the "Enter" key to open the picker
case KEYCODES.ENTER:
case Keyboard.keys.ENTER:
this.togglePicker();
break;

// Allows the "Escape" key to close the picker
case KEYCODES.ESCAPE:
case Keyboard.keys.ESCAPE:
this.escape();
event.preventDefault();
break;
Expand Down Expand Up @@ -65,13 +61,13 @@ class Picker {
item.addEventListener('keydown', (event) => {
switch(event.keyCode) {
// Allows the "Enter" key to select an item
case KEYCODES.ENTER:
case Keyboard.keys.ENTER:
this.selectItem(item, true);
event.preventDefault();
break;

// Allows the "Escape" key to close the picker
case KEYCODES.ESCAPE:
case Keyboard.keys.ESCAPE:
this.escape();
event.preventDefault();
break;
Expand Down

0 comments on commit ae99e07

Please sign in to comment.