Skip to content

Commit d918911

Browse files
committed
Checkboxradio: allow the parent of the input to be the label
Conflicts: ui/checkboxradio.js
1 parent 88b4356 commit d918911

File tree

3 files changed

+25
-17
lines changed

3 files changed

+25
-17
lines changed

tests/unit/checkboxradio/checkboxradio.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,9 @@
8080
<input type="checkbox" class="foo" id="checkbox-option-icon"/>
8181
<label for="checkbox-option-label">checkbox label</label>
8282
<input type="checkbox" class="foo" id="checkbox-option-label"/>
83+
<label>
84+
<input type="checkbox" id="label-with-no-for"/>
85+
</label>
8386
</div>
8487
</body>
8588
</html>

tests/unit/checkboxradio/checkboxradio_core.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,8 @@ if ( !$.ui.ie || ( document.documentMode && document.documentMode > 8 ) ) {
9090
}, 1 );
9191
});
9292
}
93-
test( "Checkbox creation that requires a matching label does not find label in all cases", function() {
94-
expect( 5 );
93+
test( "Checkbox creation that requires a matching finds label in all cases", function() {
94+
expect( 6 );
9595
var group = $( "<span><label for='t7092a'></label><input type='checkbox' id='t7092a'></span>" );
9696
group.find( "input[type=checkbox]" ).checkboxradio();
9797
ok( group.find( "label" ).is( ".ui-button" ) );
@@ -111,6 +111,10 @@ test( "Checkbox creation that requires a matching label does not find label in a
111111
group = $( "<input type='checkbox' id='t7092e'><span><label for='t7092e'></label></span>" );
112112
group.filter( "input[type=checkbox]" ).checkboxradio();
113113
ok( group.find( "label" ).is( ".ui-button" ) );
114+
115+
group = $( "<span><label><input type='checkbox' id='t7092f'></label></span>" );
116+
group.find( "input[type=checkbox]" ).checkboxradio();
117+
ok( group.find( "label" ).is( ".ui-button" ) );
114118
});
115119

116120
asyncTest( "Resetting a button's form should refresh the visual state of the button widget to match.", function() {

ui/checkboxradio.js

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -54,21 +54,24 @@ var baseClasses = "ui-button ui-widget ui-corner-all",
5454

5555
$.widget( "ui.checkboxradio", {
5656
version: "@VERSION",
57-
defaultElement: "<input type='checkbox'>",
5857
options: {
5958
disabled: null,
6059
label: null,
6160
icon: false
6261
},
6362

6463
_getCreateOptions: function() {
65-
var options = {};
64+
var disabled,
65+
options = {};
6666

6767
this._readLabel();
6868

6969
this.originalLabel = this.label.html();
7070

71-
this._readDisabled( options );
71+
disabled = this.element.prop( "disabled" );
72+
if ( disabled != null ) {
73+
options.disabled = disabled;
74+
}
7275

7376
if ( this.originalLabel ) {
7477
options.label = this.originalLabel;
@@ -95,13 +98,9 @@ $.widget( "ui.checkboxradio", {
9598
formElement.off( "reset" + this.eventNamespace, formResetHandler );
9699
formElement.on( "reset" + this.eventNamespace, formResetHandler );
97100

98-
// If the option is a boolean its been set by either user or by
99-
// _getCreateOptions so we need to make sure the prop matches
100-
// If it is not a boolean the user set it explicitly to null so we need to check the dom
101-
if ( typeof this.options.disabled === "boolean" ) {
102-
this.element.prop( "disabled", this.options.disabled );
103-
} else {
104-
this._readDisabled( this.options );
101+
// If it is null the user set it explicitly to null so we need to check the dom
102+
if ( this.options.disabled == null ) {
103+
this.options.disabled = this.element.prop( "disabled" ) || false;
105104
}
106105

107106
// If the option is true we call set options to add the disabled
@@ -133,12 +132,12 @@ $.widget( "ui.checkboxradio", {
133132
},
134133

135134
_readLabel: function() {
136-
var ancestor, labelSelector,
137-
labels = this.element[ 0 ].labels;
138-
135+
var ancestor, labelSelector, parent = this.element.closest( "label" );
139136
// Check control.labels first
140-
if ( labels !== undefined && labels.length > 0 ) {
141-
this.label = $( labels[ 0 ] );
137+
if ( this.element[ 0 ].labels !== undefined && this.element[ 0 ].labels.length > 0 ){
138+
this.label = $( this.element[ 0 ].labels[ 0 ] );
139+
} else if ( parent.length > 0 ) {
140+
this.label = parent;
142141
} else {
143142

144143
// We don't search against the document in case the element
@@ -241,6 +240,8 @@ $.widget( "ui.checkboxradio", {
241240

242241
if ( this.type === "checkbox" ) {
243242
toAdd += checked ? "ui-icon-check" : "ui-icon-blank";
243+
} else {
244+
toAdd += "ui-icon-blank";
244245
}
245246
this.icon.addClass( toAdd ).appendTo( this.label );
246247
} else if ( this.icon !== undefined ) {

0 commit comments

Comments
 (0)