Skip to content
This repository was archived by the owner on Oct 8, 2021. It is now read-only.

Commit ce4e3b8

Browse files
committed
simplify button text method, handle persisting option class for multiple selects
1 parent 62881f3 commit ce4e3b8

File tree

3 files changed

+66
-14
lines changed

3 files changed

+66
-14
lines changed

js/jquery.mobile.forms.select.js

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,6 @@ $.widget( "mobile.selectmenu", $.mobile.widget, {
100100

101101
// TODO values buttonId and menuId are undefined here
102102
button = this.button
103-
.text( $( this.select[ 0 ].options.item( selectedIndex ) ).text() )
104103
.insertBefore( this.select )
105104
.buttonMarkup( {
106105
theme: options.theme,
@@ -113,6 +112,8 @@ $.widget( "mobile.selectmenu", $.mobile.widget, {
113112
mini: mini
114113
});
115114

115+
this.setButtonText();
116+
116117
// Opera does not properly support opacity on select elements
117118
// In Mini, it hides the element, but not its text
118119
// On the desktop,it seems to do the opposite
@@ -198,21 +199,24 @@ $.widget( "mobile.selectmenu", $.mobile.widget, {
198199
},
199200

200201
setButtonText: function() {
201-
var self = this, selected = this.selected();
202-
203-
this.button.find( ".ui-btn-text" ).html( function() {
204-
205-
if ( !self.isMultiple ) {
206-
return $( document.createElement("span") )
207-
.addClass( self.select.attr("class") )
208-
.addClass( selected.attr("class") )
209-
.text( selected.text() );
202+
var self = this,
203+
selected = this.selected(),
204+
text = this.placeholder,
205+
span = $( document.createElement("span") );
206+
207+
this.button.find( ".ui-btn-text" ).html(function() {
208+
if ( selected.length ) {
209+
text = selected.map(function() {
210+
return $( this ).text();
211+
}).get().join( ", " );
212+
} else {
213+
text = self.placeholder;
210214
}
211215

212-
//TODO: apply the span as above to preserve the css-class of the original select
213-
return selected.length ? selected.map( function() {
214-
return $( this ).text();
215-
}).get().join( ", " ) : self.placeholder;
216+
// TODO possibly aggregate multiple select option classes
217+
return span.text( text )
218+
.addClass( self.select.attr("class") )
219+
.addClass( selected.attr("class") );
216220
});
217221
},
218222

tests/unit/select/index.html

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,34 @@ <h2 id="qunit-userAgent"></h2>
385385
<select name="select-disabled-enhancetest" id="select-disabled-enhancetest" disabled="disabled" data-nstest-native-menu="false">
386386
<option value="overnight">disabled enhance test</option>
387387
</select>
388+
389+
<select name="select-preserve-option-class" id="select-preserve-option-class">
390+
<option value="standard" class="foo" selected>Standard: 7 day</option>
391+
<option value="rush">Rush: 3 days</option>
392+
<option value="express">Express: next day</option>
393+
<option value="overnight">Overnight</option>
394+
</select>
395+
396+
<select name="select-preserve-option-class-multiple" id="select-preserve-option-class-multiple" multiple>
397+
<option value="standard" class="foo" selected>Standard: 7 day</option>
398+
<option value="rush">Rush: 3 days</option>
399+
<option value="express">Express: next day</option>
400+
<option value="overnight">Overnight</option>
401+
</select>
402+
403+
<select name="select-aggregate-option-text" id="select-aggregate-option-textxb" multiple>
404+
<option value="standard" selected>Standard: 7 day</option>
405+
<option value="rush" selected>Rush: 3 days</option>
406+
<option value="express">Express: next day</option>
407+
<option value="overnight">Overnight</option>
408+
</select>
409+
410+
<select name="select-default-option-text" id="select-default-option-text">
411+
<option value="standard">Standard: 7 day</option>
412+
<option value="rush">Rush: 3 days</option>
413+
<option value="express">Express: next day</option>
414+
<option value="overnight">Overnight</option>
415+
</select>
388416
</div>
389417

390418

tests/unit/select/select_core.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,4 +384,24 @@
384384
var menu = $(".ui-selectmenu").not( ".ui-selectmenu-hidden" );
385385
ok( menu.text().indexOf("disabled enhance test") > -1, "the right select is showing" );
386386
});
387+
388+
test( "selected option 1classes are persisted to the button text", function() {
389+
var $select = $( "#select-preserve-option-class" ),
390+
selectedOptionClasses = $select.find( "option:selected" ).attr( "class" );
391+
392+
deepEqual( $select.parent().find( ".ui-btn-text > span" ).attr( "class" ), selectedOptionClasses );
393+
});
394+
395+
test( "multiple select option classes are persisted from the first selected option to the button text", function() {
396+
var $select = $( "#select-preserve-option-class-multiple" ),
397+
selectedOptionClasses = $select.find( "option:selected" ).first().attr( "class" );
398+
399+
deepEqual( $select.parent().find( ".ui-btn-text > span" ).attr( "class" ), selectedOptionClasses );
400+
});
401+
402+
test( "multple select text values are aggregated in the button text", function() {
403+
var $select = $( "#select-aggregate-option-text" );
404+
405+
deepEqual( "Standard: 7 day, Rush: 3 days", $select.parent().find( ".ui-btn-text" ).text() );
406+
});
387407
})(jQuery);

0 commit comments

Comments
 (0)