Skip to content

Commit a65a6d6

Browse files
committed
Selectmenu: Introduce _renderButtonItem method
Fixes #10142
1 parent 06fe70b commit a65a6d6

File tree

1 file changed

+29
-16
lines changed

1 file changed

+29
-16
lines changed

ui/selectmenu.js

+29-16
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,11 @@ return $.widget( "ui.selectmenu", {
6868

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

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

103-
this.buttonText = $( "<span>", {
107+
this.buttonItem = $( "<span>", {
104108
"class": "ui-selectmenu-text"
105109
})
106110
.appendTo( this.button );
107111

108-
this._setText( this.buttonText, this.element.find( "option:selected" ).text() );
112+
this._renderButtonItem( this.buttonItem, item );
109113
this._resizeButton();
110114

111115
this._on( this.button, this._buttonEvents );
@@ -185,7 +189,7 @@ return $.widget( "ui.selectmenu", {
185189

186190
refresh: function() {
187191
this._refreshMenu();
188-
this._setText( this.buttonText, this._getSelectedItem().text() );
192+
this._renderButtonItem( this.buttonItem, this._getSelectedItem().data( "ui-selectmenu-item" ) );
189193
if ( !this.options.width ) {
190194
this._resizeButton();
191195
}
@@ -267,6 +271,10 @@ return $.widget( "ui.selectmenu", {
267271
return this.menu;
268272
},
269273

274+
_renderButtonItem: function( buttonItem, item ) {
275+
this._setText( buttonItem, item.label );
276+
},
277+
270278
_renderMenu: function( ul, items ) {
271279
var that = this,
272280
currentOptgroup = "";
@@ -434,7 +442,7 @@ return $.widget( "ui.selectmenu", {
434442

435443
// Change native select element
436444
this.element[ 0 ].selectedIndex = item.index;
437-
this._setText( this.buttonText, item.label );
445+
this._renderButtonItem( this.buttonItem, item );
438446
this._setAria( item );
439447
this._trigger( "select", event, { item: item } );
440448

@@ -544,22 +552,27 @@ return $.widget( "ui.selectmenu", {
544552
},
545553

546554
_parseOptions: function( options ) {
547-
var data = [];
555+
var that = this,
556+
data = [];
548557
options.each(function( index, item ) {
549-
var option = $( item ),
550-
optgroup = option.parent( "optgroup" );
551-
data.push({
552-
element: option,
553-
index: index,
554-
value: option.attr( "value" ),
555-
label: option.text(),
556-
optgroup: optgroup.attr( "label" ) || "",
557-
disabled: optgroup.prop( "disabled" ) || option.prop( "disabled" )
558-
});
558+
data.push( that._parseOption( $( item ), index ) );
559559
});
560560
this.items = data;
561561
},
562562

563+
_parseOption: function( option, index ) {
564+
var optgroup = option.parent( "optgroup" );
565+
566+
return {
567+
element: option,
568+
index: index,
569+
value: option.attr( "value" ),
570+
label: option.text(),
571+
optgroup: optgroup.attr( "label" ) || "",
572+
disabled: optgroup.prop( "disabled" ) || option.prop( "disabled" )
573+
};
574+
},
575+
563576
_destroy: function() {
564577
this.menuWrap.remove();
565578
this.button.remove();

0 commit comments

Comments
 (0)