Skip to content

Commit af4c35d

Browse files
committed
Selectmenu: Support width: false and default to 14em
`width: null` still matches the width of the original element. `width: false` prevents an inline style from being set for the width. This makes it easy to set the width via a stylesheet and allows the use of any unit for setting the width, such as the new default of `14em`. Fixes #11198 Closes gh-1467
1 parent 47a32fb commit af4c35d

File tree

7 files changed

+21
-17
lines changed

7 files changed

+21
-17
lines changed

demos/selectmenu/custom_render.html

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,6 @@
5959
label {
6060
display: block;
6161
}
62-
select {
63-
width: 200px;
64-
}
6562

6663
/* select with custom icons */
6764
.ui-selectmenu-menu .ui-menu.customicons .ui-menu-item-wrapper {

demos/selectmenu/default.html

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,6 @@
3232
display: block;
3333
margin: 30px 0 0 0;
3434
}
35-
select {
36-
width: 200px;
37-
}
3835
.overflow {
3936
height: 200px;
4037
}

demos/selectmenu/product-selection.html

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,6 @@
4141
display: block;
4242
margin: 20px 0 0 0;
4343
}
44-
select {
45-
width: 200px;
46-
}
4744

4845
#circle {
4946
float: left;

tests/unit/selectmenu/selectmenu_common.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ TestHelpers.commonWidgetTests( "selectmenu", {
1010
at: "left bottom",
1111
collision: "none"
1212
},
13-
width: null,
13+
width: false,
1414

1515
// callbacks
1616
change: null,

tests/unit/selectmenu/selectmenu_options.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,14 +85,17 @@ test( "CSS styles", function() {
8585
});
8686

8787
test( "width", function() {
88-
expect( 5 );
88+
expect( 6 );
8989

9090
var button,
9191
element = $( "#speed" );
9292

9393
element.selectmenu();
9494
button = element.selectmenu( "widget" );
9595

96+
equal( button[ 0 ].style.width, "", "no inline style" );
97+
98+
element.selectmenu( "option", "width", null );
9699
equal( button.outerWidth(), element.outerWidth(), "button width auto" );
97100

98101
element.outerWidth( 100 );
@@ -107,15 +110,15 @@ test( "width", function() {
107110

108111
element
109112
.append( $( "<option>", { text: "Option with a little longer text" } ) )
110-
.selectmenu( "option", "width", "" )
113+
.selectmenu( "option", "width", null )
111114
.selectmenu( "refresh" );
112115
equal( button.outerWidth(), element.outerWidth(), "button width with long option" );
113116

114117
element.parent().outerWidth( 300 );
115118
element
116119
.selectmenu( "destroy" )
117120
.css( "width", "100%" )
118-
.selectmenu();
121+
.selectmenu({ width: null });
119122
button = element.selectmenu( "widget" );
120123
equal( button.outerWidth(), 300, "button width fills container" );
121124
});

themes/base/selectmenu.css

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
position: relative;
4040
text-decoration: none;
4141
cursor: pointer;
42+
width: 14em;
4243
}
4344
.ui-selectmenu-button span.ui-icon {
4445
right: 0.5em;

ui/selectmenu.js

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ return $.widget( "ui.selectmenu", {
4848
at: "left bottom",
4949
collision: "none"
5050
},
51-
width: null,
51+
width: false,
5252

5353
// callbacks
5454
change: null,
@@ -118,7 +118,9 @@ return $.widget( "ui.selectmenu", {
118118
this.buttonItem = this._renderButtonItem( item )
119119
.appendTo( this.button );
120120

121-
this._resizeButton();
121+
if ( this.options.width !== false ) {
122+
this._resizeButton();
123+
}
122124

123125
this._on( this.button, this._buttonEvents );
124126
this.button.one( "focusin", function() {
@@ -210,7 +212,7 @@ return $.widget( "ui.selectmenu", {
210212
this._getSelectedItem().data( "ui-selectmenu-item" ) || {}
211213
)
212214
);
213-
if ( !this.options.width ) {
215+
if ( this.options.width === null ) {
214216
this._resizeButton();
215217
}
216218
},
@@ -603,7 +605,14 @@ return $.widget( "ui.selectmenu", {
603605
_resizeButton: function() {
604606
var width = this.options.width;
605607

606-
if ( !width ) {
608+
// For `width: false`, just remove inline style and stop
609+
if ( width === false ) {
610+
this.button.css( "width", "" );
611+
return;
612+
}
613+
614+
// For `width: null`, match the width of the original element
615+
if ( width === null ) {
607616
width = this.element.show().outerWidth();
608617
this.element.hide();
609618
}

0 commit comments

Comments
 (0)