| OLD | NEW |
| 1 <!-- | 1 <!-- |
| 2 @license | 2 @license |
| 3 Copyright (c) 2015 The Polymer Project Authors. All rights reserved. | 3 Copyright (c) 2015 The Polymer Project Authors. All rights reserved. |
| 4 This code may only be used under the BSD style license found at http://polymer.g
ithub.io/LICENSE.txt | 4 This code may only be used under the BSD style license found at http://polymer.g
ithub.io/LICENSE.txt |
| 5 The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt | 5 The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt |
| 6 The complete set of contributors may be found at http://polymer.github.io/CONTRI
BUTORS.txt | 6 The complete set of contributors may be found at http://polymer.github.io/CONTRI
BUTORS.txt |
| 7 Code distributed by Google as part of the polymer project is also | 7 Code distributed by Google as part of the polymer project is also |
| 8 subject to an additional IP rights grant found at http://polymer.github.io/PATEN
TS.txt | 8 subject to an additional IP rights grant found at http://polymer.github.io/PATEN
TS.txt |
| 9 --> | 9 --> |
| 10 | 10 |
| 11 <link rel="import" href="../polymer/polymer.html"> | 11 <link rel="import" href="../polymer/polymer.html"> |
| 12 <link rel="import" href="../iron-selector/iron-selector.html"> | 12 <link rel="import" href="../iron-selector/iron-selectable.html"> |
| 13 <link rel="import" href="../paper-radio-button/paper-radio-button.html"> | 13 <link rel="import" href="../paper-radio-button/paper-radio-button.html"> |
| 14 <link rel="import" href="../iron-a11y-keys-behavior/iron-a11y-keys-behavior.html
"> | 14 <link rel="import" href="../iron-a11y-keys-behavior/iron-a11y-keys-behavior.html
"> |
| 15 | 15 |
| 16 <!-- | 16 <!-- |
| 17 `paper-radio-group` allows user to select only one radio button from a set. | 17 `paper-radio-group` allows user to select only one radio button from a set. |
| 18 Checking one radio button that belongs to a radio group unchecks any | 18 Checking one radio button that belongs to a radio group unchecks any |
| 19 previously checked radio button within the same group. Use | 19 previously checked radio button within the same group. Use |
| 20 `selected` to get or set the selected radio button. | 20 `selected` to get or set the selected radio button. |
| 21 | 21 |
| 22 Example: | 22 Example: |
| (...skipping 12 matching lines...) Expand all Loading... |
| 35 @hero hero.svg | 35 @hero hero.svg |
| 36 @demo demo/index.html | 36 @demo demo/index.html |
| 37 --> | 37 --> |
| 38 | 38 |
| 39 <dom-module name="paper-radio-group"> | 39 <dom-module name="paper-radio-group"> |
| 40 <style> | 40 <style> |
| 41 :host { | 41 :host { |
| 42 display: inline-block; | 42 display: inline-block; |
| 43 } | 43 } |
| 44 | 44 |
| 45 iron-selector ::content > * { | 45 :host ::content > * { |
| 46 padding: 12px; | 46 padding: 12px; |
| 47 } | 47 } |
| 48 </style> | 48 </style> |
| 49 | 49 |
| 50 <template> | 50 <template> |
| 51 <iron-selector selected="{{selected}}" attr-for-selected="name" | |
| 52 selectable="paper-radio-button"> | |
| 53 <content id="items" select="*"></content> | 51 <content id="items" select="*"></content> |
| 54 </iron-selector> | |
| 55 </template> | 52 </template> |
| 56 | 53 |
| 57 </dom-module> | 54 </dom-module> |
| 58 | 55 |
| 59 <script> | 56 <script> |
| 60 Polymer({ | 57 Polymer({ |
| 61 is: 'paper-radio-group', | 58 is: 'paper-radio-group', |
| 62 | 59 |
| 63 behaviors: [ | 60 behaviors: [ |
| 64 Polymer.IronA11yKeysBehavior | 61 Polymer.IronA11yKeysBehavior, |
| 62 Polymer.IronSelectableBehavior |
| 65 ], | 63 ], |
| 66 | 64 |
| 67 hostAttributes: { | 65 hostAttributes: { |
| 68 role: 'radiogroup', | 66 role: 'radiogroup', |
| 69 tabindex: 0 | 67 tabindex: 0 |
| 70 }, | 68 }, |
| 71 | 69 |
| 72 properties: { | 70 properties: { |
| 73 /** | 71 /** |
| 74 * Fired when the selected element changes to user interaction. | 72 * Overriden from Polymer.IronSelectableBehavior |
| 75 * | |
| 76 * @event paper-radio-group-changed | |
| 77 */ | 73 */ |
| 74 attrForSelected: { |
| 75 type: String, |
| 76 value: 'name' |
| 77 }, |
| 78 | 78 |
| 79 /** | 79 /** |
| 80 * Gets or sets the selected element. Use the `name` attribute of the | 80 * Overriden from Polymer.IronSelectableBehavior |
| 81 * <paper-radio-button> that should be selected. | |
| 82 * | |
| 83 * @attribute selected | |
| 84 * @type String | |
| 85 * @default null | |
| 86 */ | 81 */ |
| 87 | 82 selectedAttribute: { |
| 88 selected: { | |
| 89 type: String, | 83 type: String, |
| 90 value: null, | 84 value: 'checked' |
| 91 notify: true, | |
| 92 reflectToAttribute: true, | |
| 93 observer: "_selectedChanged" | |
| 94 } | 85 } |
| 95 }, | 86 }, |
| 96 | 87 |
| 97 keyBindings: { | 88 keyBindings: { |
| 98 'left up': '_selectPrevious', | 89 'left up': 'selectPrevious', |
| 99 'right down': '_selectNext', | 90 'right down': 'selectNext', |
| 100 }, | |
| 101 | |
| 102 _selectedChanged: function() { | |
| 103 // TODO: This only needs to be async while a domReady event is unavailable
. | |
| 104 this.async(function() { | |
| 105 this._selectIndex(this._valueToIndex(this.items, this.selected)); | |
| 106 this.fire('paper-radio-group-changed'); | |
| 107 }); | |
| 108 }, | |
| 109 | |
| 110 _selectNext: function() { | |
| 111 this.selected = this._nextNode(); | |
| 112 }, | |
| 113 | |
| 114 _selectPrevious: function() { | |
| 115 this.selected = this._previousNode(); | |
| 116 }, | 91 }, |
| 117 | 92 |
| 118 /** | 93 /** |
| 119 * Returns an array of all items. | 94 * Selects the given value. |
| 120 * | |
| 121 * @property items | |
| 122 * @type array | |
| 123 */ | 95 */ |
| 124 get items() { | 96 select: function(value) { |
| 125 return Polymer.dom(this.$.items).getDistributedNodes(); | 97 if (this.selected) { |
| 98 var oldItem = this._valueToItem(this.selected); |
| 99 |
| 100 // Do not allow unchecking the selected item. |
| 101 if (this.selected == value) { |
| 102 oldItem.checked = true; |
| 103 return; |
| 104 } |
| 105 |
| 106 if (oldItem) |
| 107 oldItem.checked = false; |
| 108 } |
| 109 |
| 110 Polymer.IronSelectableBehavior.select.apply(this, [value]); |
| 111 this.fire('paper-radio-group-changed'); |
| 126 }, | 112 }, |
| 127 | 113 |
| 128 _nextNode: function() { | 114 /** |
| 129 var items = this.items; | 115 * Selects the previous item. If the previous item is disabled, then it is |
| 130 var index = this._selectedIndex; | 116 * skipped, and its previous item is selected |
| 131 var newIndex = index; | 117 */ |
| 118 selectPrevious: function() { |
| 119 var length = this.items.length; |
| 120 var newIndex = Number(this._valueToIndex(this.selected)); |
| 121 |
| 132 do { | 122 do { |
| 133 newIndex = (newIndex + 1) % items.length; | 123 newIndex = (newIndex - 1 + length) % length; |
| 134 if (newIndex === index) { | 124 } while (this.items[newIndex].disabled) |
| 135 break; | 125 |
| 136 } | 126 this.select(this._indexToValue(newIndex)); |
| 137 } while (items[newIndex].disabled); | |
| 138 return this._valueForNode(items[newIndex]); | |
| 139 }, | 127 }, |
| 140 | 128 |
| 141 _previousNode: function() { | 129 /** |
| 142 var items = this.items; | 130 * Selects the next item. If the next item is disabled, then it is |
| 143 var index = this._selectedIndex; | 131 * skipped, and its nexy item is selected |
| 144 var newIndex = index; | 132 */ |
| 133 selectNext: function() { |
| 134 var length = this.items.length; |
| 135 var newIndex = Number(this._valueToIndex(this.selected)); |
| 136 |
| 145 do { | 137 do { |
| 146 newIndex = (newIndex || items.length) - 1; | 138 newIndex = (newIndex + 1 + length) % length; |
| 147 if (newIndex === index) { | 139 } while (this.items[newIndex].disabled) |
| 148 break; | 140 |
| 149 } | 141 this.select(this._indexToValue(newIndex)); |
| 150 } while (items[newIndex].disabled); | |
| 151 return this._valueForNode(items[newIndex]); | |
| 152 }, | 142 }, |
| 153 | |
| 154 _selectIndex: function(index) { | |
| 155 if (index == this._selectedIndex) | |
| 156 return; | |
| 157 | |
| 158 var nodes = this.items; | |
| 159 | |
| 160 // If there was a previously selected node, deselect it. | |
| 161 if (nodes[this._selectedIndex]) { | |
| 162 nodes[this._selectedIndex].checked = false; | |
| 163 } | |
| 164 | |
| 165 // Select a new node. | |
| 166 nodes[index].checked = true; | |
| 167 nodes[index].focus(); | |
| 168 this._selectedIndex = index; | |
| 169 }, | |
| 170 | |
| 171 _valueForNode: function(node) { | |
| 172 return node["name"] || node.getAttribute("name"); | |
| 173 }, | |
| 174 | |
| 175 // Finds an item with value == |value| and return its index. | |
| 176 _valueToIndex: function(items, value) { | |
| 177 for (var index = 0, node; (node = items[index]); index++) { | |
| 178 if (this._valueForNode(node) == value) { | |
| 179 return index; | |
| 180 } | |
| 181 } | |
| 182 // If no item found, the value itself is probably the index. | |
| 183 return value; | |
| 184 } | |
| 185 }); | 143 }); |
| 186 </script> | 144 </script> |
| OLD | NEW |