diff --git a/iron-multi-selectable.html b/iron-multi-selectable.html index 69c30a7..10290e0 100644 --- a/iron-multi-selectable.html +++ b/iron-multi-selectable.html @@ -50,13 +50,19 @@ ], /** - * Selects the given value. If the `multi` property is true, then the selected state of the - * `value` will be toggled; otherwise the `value` will be selected. + * Selects the given value if it's item is not disabled. + * If the `multi` property is true, then the selected state of the `value` will be toggled; + * otherwise the `value` will be selected. * * @method select * @param {string|number} value the value to select. + * @returns Returns true if `value` could be selected or toggled. */ select: function(value) { + if (this._isIneligibleForSelection(this._valueToItem(value))) { + return false; + } + if (this.multi) { if (this.selectedValues) { this._toggleSelected(value); @@ -66,6 +72,7 @@ } else { this.selected = value; } + return true; }, multiChanged: function(multi) { diff --git a/iron-selectable.html b/iron-selectable.html index 3cd73ba..e1a2188 100644 --- a/iron-selectable.html +++ b/iron-selectable.html @@ -172,34 +172,64 @@ }, /** - * Selects the given value. + * Selects the given value if it's item is not disabled. * * @method select * @param {string|number} value the value to select. + * @returns Returns true if `value` could be selected. */ select: function(value) { + if (this._isIneligibleForSelection(this._valueToItem(value))) { + return false; + } this.selected = value; + return true; }, /** - * Selects the previous item. + * Selects the previous enabled item. * * @method selectPrevious + * @returns Returns true if any item could be selected. */ selectPrevious: function() { + if (this.selected == null) { + return false; + } + var length = this.items.length; var index = (Number(this._valueToIndex(this.selected)) - 1 + length) % length; - this.selected = this._indexToValue(index); + var curr = index + 1; + while (index != curr) { + if (this.select(this._indexToValue(index))) { + break; + } + index = (index - 1 + length) % length; + } + return index != curr; }, /** - * Selects the next item. + * Selects the next enabled item. * * @method selectNext + * @returns Returns true if any item could be selected. */ selectNext: function() { - var index = (Number(this._valueToIndex(this.selected)) + 1) % this.items.length; - this.selected = this._indexToValue(index); + if (this.selected == null) { + return false; + } + + var length = this.items.length; + var index = (Number(this._valueToIndex(this.selected)) + 1 + length) % length; + var curr = index - 1; + while (index != curr) { + if (this.select(this._indexToValue(index))) { + break; + } + index = (index + 1 + length) % length; + } + return index != curr; }, /** @@ -242,10 +272,10 @@ }, _updateSelected: function() { - this._selectSelected(this.selected); + this._selectSelected(); }, - _selectSelected: function(selected) { + _selectSelected: function() { this._selection.select(this._valueToItem(this.selected)); }, @@ -337,8 +367,14 @@ {selected: value, item: item}, {cancelable: true}).defaultPrevented) { this.select(value); } - } + }, + _isIneligibleForSelection: function(item) { + if (item) { + return item.hasAttribute('disabled'); + } + return false; + } }; diff --git a/test/basic.html b/test/basic.html index d934f75..5116186 100644 --- a/test/basic.html +++ b/test/basic.html @@ -55,7 +55,7 @@
Item 0
Item 1
Item 2
-
Item 3
+
Item 3
Item 4
@@ -145,6 +145,18 @@ assert.equal(selectedEventCounter, 0); }); + test('select disabled item', function() { + // setup listener for iron-select event + var selectedEventCounter = 0; + s2.addEventListener('iron-select', function(e) { + selectedEventCounter++; + }); + // selecting a disabled item shouldn't change selected value + s2.select('item3'); + assert.equal(s2.selected, 'item2'); + assert.equal(selectedEventCounter, 0); + }); + test('force synchronous item update', function() { expect(s2.items.length).to.be.equal(5); Polymer.dom(s2).appendChild(document.createElement('div')); diff --git a/test/next-previous.html b/test/next-previous.html index 3a830c2..c36e259 100644 --- a/test/next-previous.html +++ b/test/next-previous.html @@ -51,6 +51,26 @@ + + + + + + + +