-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathvaadin-radio-group.html
302 lines (256 loc) · 8.82 KB
/
vaadin-radio-group.html
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
<!--
@license
Copyright (c) 2017 Vaadin Ltd.
This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
-->
<link rel="import" href="../../polymer/polymer-element.html">
<link rel="import" href="../../polymer/lib/utils/flattened-nodes-observer.html">
<link rel="import" href="../../vaadin-themable-mixin/vaadin-themable-mixin.html">
<link rel="import" href="vaadin-radio-button.html">
<dom-module id="vaadin-radio-group">
<template>
<style>
:host {
display: inline-block;
}
:host([has-label])::before {
/* Label height + margin */
margin-top: calc(var(--lumo-font-size-s) * 1.5);
}
:host([has-label]) {
padding-top: var(--lumo-space-m);
}
[part="label"] {
display: block;
align-self: flex-start;
color: var(--lumo-secondary-text-color);
font-weight: 500;
font-size: var(--lumo-font-size-s);
margin-left: calc(var(--lumo-border-radius) / 4);
transition: color 0.2s;
line-height: 1;
padding-bottom: 0.5em;
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
position: relative;
max-width: 100%;
box-sizing: border-box;
}
[part="label"]:empty {
display: none;
}
</style>
<label part="label">[[label]]</label>
<slot id="slot"></slot>
</template>
<script>
(function() {
/**
* `<vaadin-radio-group>` is a Polymer element for grouping vaadin-radio-buttons.
*
* ```html
* <vaadin-radio-group>
* <vaadin-radio-button name="foo">Foo</vaadin-radio-button>
* <vaadin-radio-button name="bar">Bar</vaadin-radio-button>
* <vaadin-radio-button name="baz">Baz</vaadin-radio-button>
* </vaadin-radio-group>
* ```
*
* ### Styling
*
* The following shadow DOM parts are available for styling:
*
* Part name | Description
* ----------------|----------------
* `label` | The label element
*
* The following state attributes are available for styling:
*
* Attribute | Description | Part name
* -----------|-------------|------------
* `disabled` | Set when the radio group and its children are disabled. | :host
* `has-label` | Set when the element has a label | :host
*
* See [ThemableMixin – how to apply styles for shadow parts](https://github.com/vaadin/vaadin-themable-mixin/wiki)
*
* @memberof Vaadin
* @mixes Vaadin.ThemableMixin
* @element vaadin-radio-group
* @demo demo/index.html
*/
class RadioGroupElement extends Vaadin.ThemableMixin(Polymer.Element) {
static get is() {
return 'vaadin-radio-group';
}
static get properties() {
return {
/**
* The current disabled state of the radio group. True if group and all internal radio buttons are disabled.
*/
disabled: {
type: Boolean,
reflectToAttribute: true,
observer: '_disabledChanged'
},
_checkedButton: {
type: Object,
observer: '_checkedButtonChanged'
},
/**
* String used for the label element.
*/
label: {
type: String,
value: '',
observer: '_labelChanged'
},
/**
* Value of the radio group.
*/
value: {
type: String,
notify: true,
observer: '_valueChanged'
}
};
}
/**
* @private
*/
connectedCallback() {
super.connectedCallback();
this._observer = new Polymer.FlattenedNodesObserver(this, (info) => {
var checkedChangedListener = (e) => {
if (e.target.checked) {
this._changeSelectedButton(e.target);
}
};
this._filterRadioButtons(info.addedNodes).forEach(button => {
button.addEventListener('checked-changed', checkedChangedListener);
if (this.disabled) {
button.disabled = true;
}
});
this._filterRadioButtons(info.removedNodes).forEach(button => {
button.removeEventListener('checked-changed', checkedChangedListener);
});
});
}
ready() {
super.ready();
this._addActiveListeners();
if (this._radioButtons.length) {
this._setFocusable(0);
}
this.setAttribute('role', 'radiogroup');
}
get _radioButtons() {
return this._filterRadioButtons(this.querySelectorAll('*'));
}
_filterRadioButtons(nodes) {
return Array.from(nodes)
.filter(child => child instanceof Vaadin.RadioButtonElement);
}
_disabledChanged(disabled) {
this.setAttribute('aria-disabled', disabled);
this._radioButtons.forEach(button => button.disabled = disabled);
}
_addActiveListeners() {
this.addEventListener('keydown', e => {
// if e.target is vaadin-radio-group then assign to checkedRadioButton currently checked radio button
var checkedRadioButton = (e.target == this) ? this._checkedButton : e.target;
if (this.disabled) {
return;
}
// LEFT, UP - select previous radio button
if (e.keyCode === 37 || e.keyCode === 38) {
e.preventDefault();
this._selectPreviousButton(checkedRadioButton);
}
// RIGHT, DOWN - select next radio button
if (e.keyCode === 39 || e.keyCode === 40) {
e.preventDefault();
this._selectNextButton(checkedRadioButton);
}
});
}
_selectButton(element, setFocusRing) {
if (this._containsFocus()) {
element.focus();
if (setFocusRing) {
element.setAttribute('focus-ring', '');
}
}
this._changeSelectedButton(element);
}
_containsFocus() {
const root = this.getRootNode();
// Safari 9 needs polyfilled `_activeElement` to return correct node
const activeElement = root._activeElement !== undefined ? root._activeElement : root.activeElement;
return this.contains(activeElement);
}
_hasEnabledButtons() {
return !this._radioButtons.every((button) => button.disabled);
}
_selectNextButton(element) {
if (!this._hasEnabledButtons()) {
return;
}
var nextButton = element.nextElementSibling || this.firstElementChild;
if (nextButton.disabled) {
this._selectNextButton(nextButton);
} else {
this._selectButton(nextButton, true);
}
}
_selectPreviousButton(element) {
if (!this._hasEnabledButtons()) {
return;
}
var previousButton = element.previousElementSibling || this.lastElementChild;
if (previousButton.disabled) {
this._selectPreviousButton(previousButton);
} else {
this._selectButton(previousButton, true);
}
}
_changeSelectedButton(button) {
this._checkedButton = button;
this._setFocusable(this._radioButtons.indexOf(button));
}
_checkedButtonChanged(checkedButton) {
this._radioButtons.forEach(button => button.checked = button === checkedButton);
this.value = checkedButton.value;
}
_valueChanged(newV, oldV) {
if (!this._checkedButton || newV != this._checkedButton.value) {
const newCheckedButton = this._radioButtons.filter(button => button.value == newV)[0];
if (newCheckedButton) {
this._selectButton(newCheckedButton);
} else {
console.warn(`No <vaadin-radio-button> with value ${newV} found.`);
}
}
}
_setFocusable(idx) {
const items = this._radioButtons;
items.forEach(e => e.tabindex = e === items[idx] ? 0 : -1);
}
_labelChanged(label) {
if (label !== '' && label != null) {
this.setAttribute('has-label', '');
} else {
this.removeAttribute('has-label');
}
}
}
customElements.define(RadioGroupElement.is, RadioGroupElement);
/**
* @namespace Vaadin
*/
window.Vaadin = window.Vaadin || {};
Vaadin.RadioGroupElement = RadioGroupElement;
})();
</script>
</dom-module>