Skip to content

Commit

Permalink
Selectmenu: Support width: false and default to 14em
Browse files Browse the repository at this point in the history
`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
  • Loading branch information
scottgonzalez committed Mar 5, 2015
1 parent 47a32fb commit af4c35d
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 17 deletions.
3 changes: 0 additions & 3 deletions demos/selectmenu/custom_render.html
Expand Up @@ -59,9 +59,6 @@
label { label {
display: block; display: block;
} }
select {
width: 200px;
}


/* select with custom icons */ /* select with custom icons */
.ui-selectmenu-menu .ui-menu.customicons .ui-menu-item-wrapper { .ui-selectmenu-menu .ui-menu.customicons .ui-menu-item-wrapper {
Expand Down
3 changes: 0 additions & 3 deletions demos/selectmenu/default.html
Expand Up @@ -32,9 +32,6 @@
display: block; display: block;
margin: 30px 0 0 0; margin: 30px 0 0 0;
} }
select {
width: 200px;
}
.overflow { .overflow {
height: 200px; height: 200px;
} }
Expand Down
3 changes: 0 additions & 3 deletions demos/selectmenu/product-selection.html
Expand Up @@ -41,9 +41,6 @@
display: block; display: block;
margin: 20px 0 0 0; margin: 20px 0 0 0;
} }
select {
width: 200px;
}


#circle { #circle {
float: left; float: left;
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/selectmenu/selectmenu_common.js
Expand Up @@ -10,7 +10,7 @@ TestHelpers.commonWidgetTests( "selectmenu", {
at: "left bottom", at: "left bottom",
collision: "none" collision: "none"
}, },
width: null, width: false,


// callbacks // callbacks
change: null, change: null,
Expand Down
9 changes: 6 additions & 3 deletions tests/unit/selectmenu/selectmenu_options.js
Expand Up @@ -85,14 +85,17 @@ test( "CSS styles", function() {
}); });


test( "width", function() { test( "width", function() {
expect( 5 ); expect( 6 );


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


element.selectmenu(); element.selectmenu();
button = element.selectmenu( "widget" ); button = element.selectmenu( "widget" );


equal( button[ 0 ].style.width, "", "no inline style" );

element.selectmenu( "option", "width", null );
equal( button.outerWidth(), element.outerWidth(), "button width auto" ); equal( button.outerWidth(), element.outerWidth(), "button width auto" );


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


element element
.append( $( "<option>", { text: "Option with a little longer text" } ) ) .append( $( "<option>", { text: "Option with a little longer text" } ) )
.selectmenu( "option", "width", "" ) .selectmenu( "option", "width", null )
.selectmenu( "refresh" ); .selectmenu( "refresh" );
equal( button.outerWidth(), element.outerWidth(), "button width with long option" ); equal( button.outerWidth(), element.outerWidth(), "button width with long option" );


element.parent().outerWidth( 300 ); element.parent().outerWidth( 300 );
element element
.selectmenu( "destroy" ) .selectmenu( "destroy" )
.css( "width", "100%" ) .css( "width", "100%" )
.selectmenu(); .selectmenu({ width: null });
button = element.selectmenu( "widget" ); button = element.selectmenu( "widget" );
equal( button.outerWidth(), 300, "button width fills container" ); equal( button.outerWidth(), 300, "button width fills container" );
}); });
Expand Down
1 change: 1 addition & 0 deletions themes/base/selectmenu.css
Expand Up @@ -39,6 +39,7 @@
position: relative; position: relative;
text-decoration: none; text-decoration: none;
cursor: pointer; cursor: pointer;
width: 14em;
} }
.ui-selectmenu-button span.ui-icon { .ui-selectmenu-button span.ui-icon {
right: 0.5em; right: 0.5em;
Expand Down
17 changes: 13 additions & 4 deletions ui/selectmenu.js
Expand Up @@ -48,7 +48,7 @@ return $.widget( "ui.selectmenu", {
at: "left bottom", at: "left bottom",
collision: "none" collision: "none"
}, },
width: null, width: false,


// callbacks // callbacks
change: null, change: null,
Expand Down Expand Up @@ -118,7 +118,9 @@ return $.widget( "ui.selectmenu", {
this.buttonItem = this._renderButtonItem( item ) this.buttonItem = this._renderButtonItem( item )
.appendTo( this.button ); .appendTo( this.button );


this._resizeButton(); if ( this.options.width !== false ) {
this._resizeButton();
}


this._on( this.button, this._buttonEvents ); this._on( this.button, this._buttonEvents );
this.button.one( "focusin", function() { this.button.one( "focusin", function() {
Expand Down Expand Up @@ -210,7 +212,7 @@ return $.widget( "ui.selectmenu", {
this._getSelectedItem().data( "ui-selectmenu-item" ) || {} this._getSelectedItem().data( "ui-selectmenu-item" ) || {}
) )
); );
if ( !this.options.width ) { if ( this.options.width === null ) {
this._resizeButton(); this._resizeButton();
} }
}, },
Expand Down Expand Up @@ -603,7 +605,14 @@ return $.widget( "ui.selectmenu", {
_resizeButton: function() { _resizeButton: function() {
var width = this.options.width; var width = this.options.width;


if ( !width ) { // For `width: false`, just remove inline style and stop
if ( width === false ) {
this.button.css( "width", "" );
return;
}

// For `width: null`, match the width of the original element
if ( width === null ) {
width = this.element.show().outerWidth(); width = this.element.show().outerWidth();
this.element.hide(); this.element.hide();
} }
Expand Down

0 comments on commit af4c35d

Please sign in to comment.