Skip to content
This repository has been archived by the owner on Dec 19, 2024. It is now read-only.

Fix issue #63 - "Paper radio button names that evaluate to false can be deselected without allow-empty-selection" #85

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions paper-radio-group.html
Original file line number Diff line number Diff line change
Expand Up @@ -132,24 +132,27 @@
return;
}

if (this.selected) {
if ( (this.selected !== undefined) && (this.selected !== null)) {
var oldItem = this._valueToItem(this.selected);

if (this.selected == value) {
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)
if (oldItem) {
oldItem.checked = true;
}

return;
}
}

if (oldItem)
if (oldItem) {
oldItem.checked = false;
}
}

Polymer.IronSelectableBehavior.select.apply(this, [value]);
Expand Down
31 changes: 31 additions & 0 deletions test/basic.html
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,15 @@
</template>
</test-fixture>

<test-fixture id="WithFalsyValueSelection">
<template>
<paper-radio-group required selected="">
<paper-radio-button name="">No</paper-radio-button>
<paper-radio-button name="a_value">Yes</paper-radio-button>
</paper-radio-group>
</template>
</test-fixture>

<test-fixture id="WithDisabled">
<template>
<paper-radio-group selected="r1">
Expand Down Expand Up @@ -192,6 +201,28 @@
}, 1);
});

test('clicking the selected item should not deselect with falsy values', function (done) {
var g = fixture('WithFalsyValueSelection');
MockInteractions.focus(g);

// Needs to be async since the underlying iron-selector uses observeNodes.
Polymer.Base.async(function() {
var items = g.items;

expect(items[0].checked).to.be.equal(true);

// The selection should not change, but wait for a little bit just
// in case it would and an event would be fired.
setTimeout(function() {
expect(items[0].checked).to.be.equal(true);
expect(items[1].checked).to.be.equal(false);
done();
}, 1);

MockInteractions.tap(items[0]);
}, 1);
});

test('clicking the selected item should deselect if allow-empty-selection is set', function (done) {
var g = fixture('WithSelection');
g.allowEmptySelection = true;
Expand Down