all files / ember-aria-checkbox/components/ aria-checkbox.js

100% Statements 15/15
100% Branches 6/6
100% Functions 5/5
100% Lines 15/15
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58      1×         1×                         15×           3× 3×   2× 2× 2×   1×     3× 2× 2×           2×       7×       2×        
import Ember from 'ember';
import layout from '../templates/components/aria-checkbox';
 
const KEYCODES = {
  'RETURN': 13,
  'SPACE': 32
};
 
const {
  Component,
  computed,
  get,
  set
} = Ember;
 
export default Component.extend({
  layout,
  classNameBindings: ['focus:focus'],
  attributeBindings: ['role', 'ariaChecked:aria-checked', 'tabindex'],
  role: 'checkbox',
  ariaChecked: computed('checked', function() {
    return get(this, 'checked') ? 'true' : 'false';
  }),
  focus: false,
  checked: false,
  tabindex: 0,
  keyDown(e) {
    let flag = false;
    switch(e.keyCode) {
    case KEYCODES.SPACE:
      this.toggleProperty('checked');
      flag = true;
      break;
    default:
      break;
    }
 
    if(flag) {
      e.stopPropagation();
      e.preventDefault();
    }
 
  },
 
  click() {
    this.toggleProperty('checked');
  },
 
  focusIn() {
    set(this, 'focus', true);
  },
 
  focusOut() {
    set(this, 'focus', false);
  }
});