Skip to content

Selectmenu: Support width: false and default to 14em #1467

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions demos/selectmenu/custom_render.html
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,6 @@
label {
display: block;
}
select {
width: 200px;
}

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

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

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

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

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

element.selectmenu();
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" );

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

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

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

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

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

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this check be consistent with the one above, which uses !== false?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The check above is inside _create(); the logic is: if we have any size to set, then do so.

This check is inside refresh(). If there is no inline style (false) or there's an explicit width (anything else beside null), then there's no action to take because the state will already be correct. If the value is null and we're copying the size of the original element, then we need to resize the button because that's something external that can change the behavior.

this._resizeButton();
}
},
Expand Down Expand Up @@ -603,7 +605,14 @@ return $.widget( "ui.selectmenu", {
_resizeButton: function() {
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();
this.element.hide();
}
Expand Down