1<!--
2@license
3Copyright (c) 2015 The Polymer Project Authors. All rights reserved.
4This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
5The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
6The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
7Code distributed by Google as part of the polymer project is also
8subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
9-->
10
11<link rel="import" href="../polymer/polymer.html">
12<link rel="import" href="../iron-a11y-keys-behavior/iron-a11y-keys-behavior.html">
13<link rel="import" href="../iron-menu-behavior/iron-menubar-behavior.html">
14<link rel="import" href="../paper-radio-button/paper-radio-button.html">
15
16<!--
17Material design: [Radio button](https://www.google.com/design/spec/components/selection-controls.html#selection-controls-radio-button)
18
19`paper-radio-group` allows user to select at most one radio button from a set.
20Checking one radio button that belongs to a radio group unchecks any
21previously checked radio button within the same group. Use
22`selected` to get or set the selected radio button.
23
24The <paper-radio-buttons> inside the group must have the `name` attribute
25set.
26
27Example:
28
29    <paper-radio-group selected="small">
30      <paper-radio-button name="small">Small</paper-radio-button>
31      <paper-radio-button name="medium">Medium</paper-radio-button>
32      <paper-radio-button name="large">Large</paper-radio-button>
33    </paper-radio-group>
34
35Radio-button-groups can be made optional, and allow zero buttons to be selected:
36
37    <paper-radio-group selected="small" allow-empty-selection>
38      <paper-radio-button name="small">Small</paper-radio-button>
39      <paper-radio-button name="medium">Medium</paper-radio-button>
40      <paper-radio-button name="large">Large</paper-radio-button>
41    </paper-radio-group>
42
43See <a href="paper-radio-button">paper-radio-button</a> for more
44information about `paper-radio-button`.
45
46
47Custom property | Description | Default
48----------------|-------------|----------
49`--paper-radio-group-item-padding` | The padding of the item | `12px`
50
51@group Paper Elements
52@element paper-radio-group
53@hero hero.svg
54@demo demo/index.html
55-->
56
57<dom-module id="paper-radio-group">
58  <template>
59    <style>
60      :host {
61        display: inline-block;
62      }
63
64      :host ::content > * {
65        padding: var(--paper-radio-group-item-padding, 12px);
66      }
67    </style>
68
69    <content id="items" select="*"></content>
70  </template>
71</dom-module>
72
73<script>
74  Polymer({
75    is: 'paper-radio-group',
76
77    behaviors: [
78      Polymer.IronMenubarBehavior
79    ],
80
81    hostAttributes: {
82      role: 'radiogroup',
83      tabindex: 0
84    },
85
86    properties: {
87      /**
88       * Fired when the radio group selection changes.
89       *
90       * @event paper-radio-group-changed
91       */
92
93      /**
94       * Overriden from Polymer.IronSelectableBehavior
95       */
96      attrForSelected: {
97        type: String,
98        value: 'name'
99      },
100
101      /**
102       * Overriden from Polymer.IronSelectableBehavior
103       */
104      selectedAttribute: {
105        type: String,
106        value: 'checked'
107      },
108
109      /**
110       * Overriden from Polymer.IronSelectableBehavior
111       */
112      selectable: {
113        type: String,
114        value: 'paper-radio-button'
115      },
116
117      /**
118       * If true, radio-buttons can be deselected
119       */
120      allowEmptySelection: {
121        type: Boolean,
122        value: false
123      }
124    },
125
126    /**
127     * Selects the given value.
128     */
129    select: function(value) {
130      var newItem = this._valueToItem(value);
131      if (newItem && newItem.hasAttribute('disabled')) {
132        return;
133      }
134
135      if (this.selected) {
136        var oldItem = this._valueToItem(this.selected);
137
138        if (this.selected == value) {
139          // If deselecting is allowed we'll have to apply an empty selection.
140          // Otherwise, we should force the selection to stay and make this
141          // action a no-op.
142          if (this.allowEmptySelection) {
143            value = '';
144          } else {
145            if (oldItem)
146              oldItem.checked = true;
147            return;
148          }
149        }
150
151        if (oldItem)
152          oldItem.checked = false;
153      }
154
155      Polymer.IronSelectableBehavior.select.apply(this, [value]);
156      this.fire('paper-radio-group-changed');
157    },
158
159    _activateFocusedItem: function() {
160      this._itemActivate(this._valueForItem(this.focusedItem), this.focusedItem);
161    },
162
163    _onUpKey: function(event) {
164      this._focusPrevious();
165      event.preventDefault();
166      this._activateFocusedItem();
167    },
168
169    _onDownKey: function(event) {
170      this._focusNext();
171      event.preventDefault();
172      this._activateFocusedItem();
173    },
174
175    _onLeftKey: function(event) {
176      Polymer.IronMenubarBehaviorImpl._onLeftKey.apply(this, arguments);
177      this._activateFocusedItem();
178    },
179
180    _onRightKey: function(event) {
181      Polymer.IronMenubarBehaviorImpl._onRightKey.apply(this, arguments);
182      this._activateFocusedItem();
183    }
184  });
185</script>
186