Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #659 from sproutcore/team/tim-evans/radio-view-dis…

…abled-items

Don't allow radio buttons to get checked when they're disabled
  • Loading branch information...
commit 16f6918ccc69a4ce818f1f5c31661007705f8240 2 parents fd9777b + 99e47b1
@etgryphon etgryphon authored
View
69 frameworks/desktop/tests/views/radio/methods.js
@@ -18,14 +18,29 @@ module("SC.RadioView Logic", {
items: 'Red Green'.w(),
value: 'Red',
isEnabled: YES
+ }),
+ SC.RadioView.extend({
+ layout: { right: 20, bottom: 20, width: 100, height: 23 },
+ itemTitleKey: 'title',
+ itemIsEnabledKey: 'enabled',
+ items: [{
+ title: 'Sugar',
+ enabled: NO
+ }, {
+ title: 'Salt',
+ enabled: NO
+ }, {
+ title: 'Salgar',
+ enabled: YES
+ }]
})]
});
pane.append(); // make sure there is a layer...
SC.RunLoop.end();
-
+
view = pane.childViews[0];
- },
-
+ },
+
teardown: function() {
pane.remove();
pane = view = null ;
@@ -59,11 +74,11 @@ test("isEnabled should alter group classname and sync with isEnabled property",
ok(view.get('isEnabled'), 'isEnabled should match value');
ok(!view.$().hasClass('disabled'), 'should NOT have disabled class');
- // update value -- make sure isEnabled changes.
+ // update value -- make sure isEnabled changes.
SC.RunLoop.begin();
view.set('isEnabled', 0); // make falsy. (but not NO exactly)
SC.RunLoop.end();
-
+
ok(!view.get('isEnabled'), 'isEnabled should now be NO');
ok(view.$().hasClass('disabled'), 'should have disabled class');
@@ -71,7 +86,7 @@ test("isEnabled should alter group classname and sync with isEnabled property",
SC.RunLoop.begin();
view.set('isEnabled', YES);
SC.RunLoop.end();
-
+
ok(view.get('isEnabled'), 'isEnabled should match value');
ok(!view.$().hasClass('disabled'), 'should NOT have disabled class');
});
@@ -85,23 +100,23 @@ test("clicking on a radio button will change toggle the value", function() {
});
test("pressing mouseDown and then mouseUp anywhere in a radio button should toggle the selection", function() {
- var elem = view.get('layer'),
+ var elem = view.get('layer'),
r = view.$('.sc-radio-button');
SC.Event.trigger(r[0], 'mousedown');
equals(view.get('value'), 'Red', 'value should not change yet');
ok(r.first().hasClass('active'), 'radio button should be active');
-
+
// simulate mouseUp and browser-native change to control
SC.Event.trigger(r[0],'mouseup');
//input.attr('checked', NO);
// loose focus of the element since it was changed
SC.Event.trigger(r[1],'mousedown');
SC.Event.trigger(r[1],'mouseup');
-
+
ok(!r.first().hasClass('active'), 'radio button should no longer be active');
equals(view.get('value'), 'Green', 'value should be undefined (none checked)');
-
+
elem = null ;
});
@@ -109,11 +124,41 @@ test("isEnabled=NO should add disabled attr to input", function() {
SC.RunLoop.begin();
view.set('isEnabled', NO);
SC.RunLoop.end();
-
+
ok(view.$().hasClass('disabled'), 'should have disabled attr');
-
+
ok(view.get('value'), 'precond - value should be true');
view.mouseDown();
view.mouseUp();
ok(view.get('value'), 'value should not change when clicked');
});
+
+test("disabled radio buttons do not become selected on click", function () {
+ view = pane.childViews[1];
+
+ var elem = view.get('layer'),
+ r = view.$('.sc-radio-button');
+
+ SC.Event.trigger(r[0], 'mousedown');
+ ok(!r.first().hasClass('active'), 'radio button should not be active');
+
+ // simulate mouseUp and browser-native change to control
+ SC.Event.trigger(r[0],'mouseup');
+ ok(!r.first().hasClass('active'), 'radio button should not be active');
+ equals(r.first().attr('aria-checked'), 'false', 'radio button should not be checked');
+ equals(view.get('value'), undefined, "value should be undefined (nothing is checked)");
+
+ // lose focus of the element since it was changed
+ SC.Event.trigger(r[1],'mousedown');
+ SC.Event.trigger(r[1],'mouseup');
+
+ equals(view.get('value'), undefined, "value should be undefined (nothing is checked)");
+
+ // last one is selectable
+ SC.Event.trigger(r[2],'mousedown');
+ SC.Event.trigger(r[2],'mouseup');
+
+ equals(view.get('value'), view.items[2], "value should be 'Salgar'");
+
+ elem = null ;
+});
View
16 frameworks/desktop/views/radio.js
@@ -356,8 +356,11 @@ SC.RadioView = SC.View.extend(SC.Control,
this._activeRadioButton = index;
if (index !== undefined) {
- this.get('displayItems')[index].set('isActive', YES);
- delegate.updateRadioAtIndex(proxy, elem, index);
+ var item = this.get('displayItems')[index];
+ if (item.get('isEnabled')) {
+ item.set('isActive', YES);
+ delegate.updateRadioAtIndex(proxy, elem, index);
+ }
}
// even if radiobuttons are not set to get firstResponder, allow default
@@ -387,9 +390,12 @@ SC.RadioView = SC.View.extend(SC.Control,
this._activeRadioButton = undefined;
if (index !== undefined) {
- displayItems[index].set('isActive', NO);
- delegate.updateRadioAtIndex(proxy, elem, index);
- this.set('value', displayItems[index].value);
+ var item = this.get('displayItems')[index];
+ if (item.get('isEnabled')) {
+ item.set('isActive', NO);
+ delegate.updateRadioAtIndex(proxy, elem, index);
+ this.set('value', item.value);
+ }
}
evt.allowDefault();
Please sign in to comment.
Something went wrong with that request. Please try again.