Skip to content

Commit 45e13ed

Browse files
committed
Selectmenu: Properly set width for button
Fixes #10145 Closes jquerygh-1296
1 parent d1e327c commit 45e13ed

File tree

2 files changed

+26
-8
lines changed

2 files changed

+26
-8
lines changed

tests/unit/selectmenu/selectmenu_options.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,8 @@ test( "CSS styles", function() {
8686
});
8787

8888

89-
test( "Width", function() {
90-
expect( 8 );
89+
test( "width", function() {
90+
expect( 9 );
9191

9292
var button, menu,
9393
element = $( "#speed" );
@@ -124,6 +124,14 @@ test( "Width", function() {
124124
equal( button.outerWidth(), element.outerWidth(), "button width with long option" );
125125
element.selectmenu( "open" );
126126
ok( menu.outerWidth() >= element.outerWidth(), "menu width with long option" );
127+
128+
element.parent().outerWidth( 300 );
129+
element
130+
.selectmenu( "destroy" )
131+
.css( "width", "100%" )
132+
.selectmenu();
133+
button = element.selectmenu( "widget" );
134+
equal( button.outerWidth(), 300, "button width fills container" );
127135
});
128136

129137
})( jQuery );

ui/selectmenu.js

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ return $.widget( "ui.selectmenu", {
106106
.appendTo( this.button );
107107

108108
this._setText( this.buttonText, this.element.find( "option:selected" ).text() );
109-
this._setOption( "width", this.options.width );
109+
this._resizeButton();
110110

111111
this._on( this.button, this._buttonEvents );
112112
this.button.one( "focusin", function() {
@@ -186,7 +186,9 @@ return $.widget( "ui.selectmenu", {
186186
refresh: function() {
187187
this._refreshMenu();
188188
this._setText( this.buttonText, this._getSelectedItem().text() );
189-
this._setOption( "width", this.options.width );
189+
if ( !this.options.width ) {
190+
this._resizeButton();
191+
}
190192
},
191193

192194
_refreshMenu: function() {
@@ -475,10 +477,7 @@ return $.widget( "ui.selectmenu", {
475477
}
476478

477479
if ( key === "width" ) {
478-
if ( !value ) {
479-
value = this.element.outerWidth();
480-
}
481-
this.button.outerWidth( value );
480+
this._resizeButton();
482481
}
483482
},
484483

@@ -511,6 +510,17 @@ return $.widget( "ui.selectmenu", {
511510
this.menu.attr( "aria-hidden", !this.isOpen );
512511
},
513512

513+
_resizeButton: function() {
514+
var width = this.options.width;
515+
516+
if ( !width ) {
517+
width = this.element.show().outerWidth();
518+
this.element.hide();
519+
}
520+
521+
this.button.outerWidth( width );
522+
},
523+
514524
_resizeMenu: function() {
515525
this.menu.outerWidth( Math.max(
516526
this.button.outerWidth(),

0 commit comments

Comments
 (0)