-
Notifications
You must be signed in to change notification settings - Fork 332
/
paper-radio-group.js
79 lines (66 loc) · 2.1 KB
/
paper-radio-group.js
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/**
* @module ember-paper
*/
import { inject as service } from '@ember/service';
import { filterBy, mapBy } from '@ember/object/computed';
import Component from '@ember/component';
import { assert } from '@ember/debug';
import layout from '../templates/components/paper-radio-group';
import FocusableMixin from 'ember-paper/mixins/focusable-mixin';
import { ParentMixin } from 'ember-composability-tools';
import { isPresent } from '@ember/utils';
import { invokeAction } from 'ember-invoke-action';
/**
* @class PaperRadioGroup
* @extends Ember.Component
* @uses FocusableMixin
* @uses ParentMixin
*/
export default Component.extend(FocusableMixin, ParentMixin, {
layout,
tagName: 'md-radio-group',
tabindex: 0,
/* FocusableMixin Overrides */
focusOnlyOnKey: true,
radioComponent: 'paper-radio',
constants: service(),
// Lifecycle hooks
init() {
this._super(...arguments);
assert('{{paper-radio-group}} requires an `onChange` action or null for no action', this.get('onChange') !== undefined);
},
enabledChildRadios: filterBy('childComponents', 'disabled', false),
childValues: mapBy('enabledChildRadios', 'value'),
keyDown(ev) {
switch (ev.which) {
case this.get('constants.KEYCODE.LEFT_ARROW'):
case this.get('constants.KEYCODE.UP_ARROW'):
ev.preventDefault();
this.select(-1);
break;
case this.get('constants.KEYCODE.RIGHT_ARROW'):
case this.get('constants.KEYCODE.DOWN_ARROW'):
ev.preventDefault();
this.select(1);
break;
}
},
select(increment) {
let groupValue = this.get('groupValue');
let index = 0;
if (isPresent(groupValue)) {
index = this.get('childValues').indexOf(groupValue);
index += increment;
let length = this.get('childValues.length');
index = ((index % length) + length) % length;
}
let childRadio = this.get('enabledChildRadios').objectAt(index);
childRadio.set('focused', true);
invokeAction(this, 'onChange', childRadio.get('value'));
},
actions: {
onChange(value) {
invokeAction(this, 'onChange', value);
}
}
});