This repository has been archived by the owner on Aug 1, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
anypoint-radio-group.html
170 lines (153 loc) · 4.51 KB
/
anypoint-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
<!--
@license
Copyright 2017 Mulesoft.
All rights reserved.
-->
<link rel="import" href="../polymer/polymer.html">
<link rel="import" href="../iron-a11y-keys-behavior/iron-a11y-keys-behavior.html">
<link rel="import" href="../iron-menu-behavior/iron-menubar-behavior.html">
<link rel="import" href="../anypoint-radio-button/anypoint-radio-button.html">
<!--
`<anypoint-radio-group>` A group of Anypoint radio buttons
`anypoint-radio-group` allows user to select at most one radio button from a set.
Checking one radio button that belongs to a radio group unchecks any
previously checked radio button within the same group. Use
`selected` to get or set the selected radio button.
The `<anypoint-radio-buttons>` inside the group must have the `name` attribute
set.
Example:
```html
<anypoint-radio-group selected="small">
<anypoint-radio-button name="small">Small</anypoint-radio-button>
<anypoint-radio-button name="medium">Medium</anypoint-radio-button>
<anypoint-radio-button name="large">Large</anypoint-radio-button>
</anypoint-radio-group>
```
Radio-button-groups can be made optional, and allow zero buttons to be selected:
```html
<anypoint-radio-group selected="small" allow-empty-selection>
<anypoint-radio-button name="small">Small</anypoint-radio-button>
<anypoint-radio-button name="medium">Medium</anypoint-radio-button>
<anypoint-radio-button name="large">Large</anypoint-radio-button>
</anypoint-radio-group>
```
See `anypoint-radio-buttonz for more information.
### Styling
Custom property | Description | Default
----------------|-------------|----------
`--anypoint-radio-group-item-padding` | The padding of the item | `12px`
@group Anypoint Elements
@element anypoint-radio-group
@demo demo/index.html
-->
<dom-module id="anypoint-radio-group">
<template>
<style>
:host {
display: inline-block;
}
:host ::content > * {
padding: var(--anypoint-radio-group-item-padding, 12px);
}
</style>
<content id="items" select="*"></content>
</template>
<script>
Polymer({
is: 'anypoint-radio-group',
behaviors: [
Polymer.IronMenubarBehavior
],
hostAttributes: {
role: 'radiogroup',
tabindex: 0
},
/**
* Fired when the radio group selection changes.
*
* @event anypoint-radio-group-changed
*/
properties: {
/**
* Overriden from Polymer.IronSelectableBehavior
*/
attrForSelected: {
type: String,
value: 'name'
},
/**
* Overriden from Polymer.IronSelectableBehavior
*/
selectedAttribute: {
type: String,
value: 'checked'
},
/**
* Overriden from Polymer.IronSelectableBehavior
*/
selectable: {
type: String,
value: 'anypoint-radio-button'
},
/**
* If true, radio-buttons can be deselected
*/
allowEmptySelection: {
type: Boolean,
value: false
}
},
/**
* Selects the given value.
*/
select: function(value) {
var newItem = this._valueToItem(value);
if (newItem && newItem.hasAttribute('disabled')) {
return;
}
if (this.selected) {
var oldItem = this._valueToItem(this.selected);
if (this.selected === value) {
// If deselecting is allowed we'll have to apply an empty selection.
// Otherwise, we should force the selection to stay and make this
// action a no-op.
if (this.allowEmptySelection) {
value = '';
} else {
if (oldItem) {
oldItem.checked = true;
}
return;
}
}
if (oldItem) {
oldItem.checked = false;
}
}
Polymer.IronSelectableBehavior.select.apply(this, [value]);
this.fire('anypoint-radio-group-changed');
},
_activateFocusedItem: function() {
this._itemActivate(this._valueForItem(this.focusedItem), this.focusedItem);
},
_onUpKey: function(event) {
this._focusPrevious();
event.preventDefault();
this._activateFocusedItem();
},
_onDownKey: function(event) {
this._focusNext();
event.preventDefault();
this._activateFocusedItem();
},
_onLeftKey: function() {
Polymer.IronMenubarBehaviorImpl._onLeftKey.apply(this, arguments);
this._activateFocusedItem();
},
_onRightKey: function() {
Polymer.IronMenubarBehaviorImpl._onRightKey.apply(this, arguments);
this._activateFocusedItem();
}
});
</script>
</dom-module>