Skip to content

Commit 2bce7a1

Browse files
fnagelarschmitz
authored andcommitted
Selectmenu: Introduce _renderButtonItem() method
Fixes #10142 Closes gh-1299
1 parent ca04ad1 commit 2bce7a1

File tree

2 files changed

+71
-18
lines changed

2 files changed

+71
-18
lines changed

tests/unit/selectmenu/selectmenu_core.js

+34
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,40 @@ asyncTest( "accessibility", function() {
4545
});
4646

4747

48+
test( "_renderButtonItem()", function() {
49+
expect( 2 );
50+
51+
var option,
52+
element = $( "#speed" ).selectmenu(),
53+
instance = element.selectmenu( "instance" ),
54+
button = element.selectmenu( "widget" ),
55+
menu = element.selectmenu( "menuWidget" );
56+
57+
instance._renderButtonItem = function( item ) {
58+
var buttonItem = $( "<span>" );
59+
this._setText( buttonItem, item.label + item.index );
60+
61+
return buttonItem;
62+
};
63+
64+
element.selectmenu( "refresh" );
65+
option = element.find( "option:selected" );
66+
equal(
67+
option.text() + element[ 0 ].selectedIndex,
68+
button.text(),
69+
"refresh: button item text"
70+
);
71+
72+
button.trigger( "click" );
73+
menu.find( "li" ).last().simulate( "mouseover" ).trigger( "click" );
74+
option = element.find( "option" ).last();
75+
equal(
76+
option.text() + element[ 0 ].selectedIndex,
77+
button.text(),
78+
"click: button item text"
79+
);
80+
});
81+
4882
$.each([
4983
{
5084
type: "default",

ui/selectmenu.js

+37-18
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,11 @@ return $.widget( "ui.selectmenu", {
6767
},
6868

6969
_drawButton: function() {
70-
var that = this;
70+
var that = this,
71+
item = this._parseOption(
72+
this.element.find( "option:selected" ),
73+
this.element[ 0 ].selectedIndex
74+
);
7175

7276
// Associate existing label with the new button
7377
this.label = $( "label[for='" + this.ids.element + "']" ).attr( "for", this.ids.button );
@@ -99,12 +103,9 @@ return $.widget( "ui.selectmenu", {
99103
})
100104
.prependTo( this.button );
101105

102-
this.buttonText = $( "<span>", {
103-
"class": "ui-selectmenu-text"
104-
})
106+
this.buttonItem = this._renderButtonItem( item )
105107
.appendTo( this.button );
106108

107-
this._setText( this.buttonText, this.element.find( "option:selected" ).text() );
108109
this._resizeButton();
109110

110111
this._on( this.button, this._buttonEvents );
@@ -190,7 +191,11 @@ return $.widget( "ui.selectmenu", {
190191

191192
refresh: function() {
192193
this._refreshMenu();
193-
this._setText( this.buttonText, this._getSelectedItem().text() );
194+
this.buttonItem.replaceWith(
195+
this.buttonItem = this._renderButtonItem(
196+
this._getSelectedItem().data( "ui-selectmenu-item" )
197+
)
198+
);
194199
if ( !this.options.width ) {
195200
this._resizeButton();
196201
}
@@ -275,6 +280,15 @@ return $.widget( "ui.selectmenu", {
275280
return this.menu;
276281
},
277282

283+
_renderButtonItem: function( item ) {
284+
var buttonItem = $( "<span>", {
285+
"class": "ui-selectmenu-text"
286+
});
287+
this._setText( buttonItem, item.label );
288+
289+
return buttonItem;
290+
},
291+
278292
_renderMenu: function( ul, items ) {
279293
var that = this,
280294
currentOptgroup = "";
@@ -480,7 +494,7 @@ return $.widget( "ui.selectmenu", {
480494

481495
// Change native select element
482496
this.element[ 0 ].selectedIndex = item.index;
483-
this._setText( this.buttonText, item.label );
497+
this.buttonItem.replaceWith( this.buttonItem = this._renderButtonItem( item ) );
484498
this._setAria( item );
485499
this._trigger( "select", event, { item: item } );
486500

@@ -590,22 +604,27 @@ return $.widget( "ui.selectmenu", {
590604
},
591605

592606
_parseOptions: function( options ) {
593-
var data = [];
607+
var that = this,
608+
data = [];
594609
options.each(function( index, item ) {
595-
var option = $( item ),
596-
optgroup = option.parent( "optgroup" );
597-
data.push({
598-
element: option,
599-
index: index,
600-
value: option.attr( "value" ),
601-
label: option.text(),
602-
optgroup: optgroup.attr( "label" ) || "",
603-
disabled: optgroup.prop( "disabled" ) || option.prop( "disabled" )
604-
});
610+
data.push( that._parseOption( $( item ), index ) );
605611
});
606612
this.items = data;
607613
},
608614

615+
_parseOption: function( option, index ) {
616+
var optgroup = option.parent( "optgroup" );
617+
618+
return {
619+
element: option,
620+
index: index,
621+
value: option.attr( "value" ),
622+
label: option.text(),
623+
optgroup: optgroup.attr( "label" ) || "",
624+
disabled: optgroup.prop( "disabled" ) || option.prop( "disabled" )
625+
};
626+
},
627+
609628
_destroy: function() {
610629
this.menuWrap.remove();
611630
this.button.remove();

0 commit comments

Comments
 (0)