-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathvaadin-radio-button.html
233 lines (198 loc) · 5.92 KB
/
vaadin-radio-button.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
<!--
@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/mixins/gesture-event-listeners.html">
<link rel="import" href="../../vaadin-themable-mixin/vaadin-themable-mixin.html">
<link rel="import" href="../../vaadin-control-state-mixin/vaadin-control-state-mixin.html">
<link rel="import" href="../../vaadin-element-mixin/vaadin-element-mixin.html">
<dom-module id="vaadin-radio-button">
<template>
<style>
:host {
display: inline-block;
}
label {
display: inline-flex;
align-items: baseline;
outline: none;
}
[part="radio"] {
position: relative;
display: inline-block;
flex: none;
}
input[type="radio"] {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
opacity: 0;
cursor: inherit;
margin: 0;
}
:host([disabled]) {
-webkit-tap-highlight-color: transparent;
}
</style>
<label on-click="_preventDefault">
<span part="radio">
<input
type="radio"
checked="{{checked::change}}"
disabled$="[[disabled]]"
role="presentation"
tabindex="-1">
</span>
<span part="label">
<slot></slot>
</span>
</label>
</template>
<script>
(function() {
/**
* `<vaadin-radio-button>` is a Web Component for radio buttons.
*
* ```html
* <vaadin-radio-button value="foo">Foo</vaadin-radio-button>
* ```
*
* ### Styling
*
* The following shadow DOM parts are available for styling:
*
* Part name | Description
* ------------------|----------------
* `radio` | The radio button element
* `label` | The label content element
*
* The following state attributes are available for styling:
*
* Attribute | Description | Part name
* -----------|-------------|------------
* `disabled` | Set when the radio button is disabled. | :host
* `focus-ring` | Set when the radio button is focused using the keyboard. | :host
* `focused` | Set when the radio button is focused. | :host
* `checked` | Set when the radio button is checked. | :host
*
* See [ThemableMixin – how to apply styles for shadow parts](https://github.com/vaadin/vaadin-themable-mixin/wiki)
*
* @memberof Vaadin
* @mixes Vaadin.ElementMixin
* @mixes Vaadin.ControlStateMixin
* @mixes Vaadin.ThemableMixin
* @mixes Polymer.GestureEventListeners
* @element vaadin-radio-button
* @demo demo/index.html
*/
class RadioButtonElement extends
Vaadin.ElementMixin(
Vaadin.ControlStateMixin(
Vaadin.ThemableMixin(
Polymer.GestureEventListeners(Polymer.Element)))) {
static get is() {
return 'vaadin-radio-button';
}
static get version() {
return '1.1.0-beta2';
}
static get properties() {
return {
/**
* True if the radio button is checked.
*/
checked: {
type: Boolean,
value: false,
notify: true,
observer: '_checkedChanged',
reflectToAttribute: true
},
/**
* The value for this element.
*/
value: {
type: String,
value: 'on'
}
};
}
constructor() {
super();
/**
* @type {string}
* Name of the element.
*/
this.name;
}
get name() {
return this.checked ? this._storedName : '';
}
set name(name) {
this._storedName = name;
}
ready() {
super.ready();
this.setAttribute('role', 'radio');
this.addEventListener('click', this._handleClick.bind(this));
this._addActiveListeners();
const attrName = this.getAttribute('name');
if (attrName) {
this.name = attrName;
}
}
_checkedChanged(checked) {
this.setAttribute('aria-checked', checked);
}
_addActiveListeners() {
this._addEventListenerToNode(this, 'down', (e) => {
if (!this.disabled) {
this.setAttribute('active', '');
}
});
this._addEventListenerToNode(this, 'up', (e) => {
this.removeAttribute('active');
if (!this.checked && !this.disabled) {
this.checked = true;
}
});
this.addEventListener('keydown', e => {
if (!this.disabled && e.keyCode === 32) {
e.preventDefault();
this.setAttribute('active', '');
}
});
this.addEventListener('keyup', e => {
if (!this.disabled && e.keyCode === 32) {
e.preventDefault();
this.setAttribute('checked', '');
this.removeAttribute('active');
}
});
}
_handleClick(e) {
if (!this.disabled) {
e.preventDefault();
this.checked = true;
}
}
/** @protected */
get focusElement() {
return this.shadowRoot.querySelector('input');
}
_preventDefault(e) {
e.preventDefault();
}
}
customElements.define(RadioButtonElement.is, RadioButtonElement);
/**
* @namespace Vaadin
*/
window.Vaadin.RadioButtonElement = RadioButtonElement;
})();
</script>
</dom-module>