aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorScott González <scott.gonzalez@gmail.com>2015-01-12 11:23:08 -0500
committerScott González <scott.gonzalez@gmail.com>2015-01-12 13:00:38 -0500
commit65584c1640f8c7430587738c3c4cd7dd8010a2a5 (patch)
treed02d45463f89ffe5aa727e3c6cc6fc84f7cd562e
parent42099e44610face58172ed0ed27115fb2b84ab50 (diff)
downloadjquery-ui-65584c1640f8c7430587738c3c4cd7dd8010a2a5.tar.gz
jquery-ui-65584c1640f8c7430587738c3c4cd7dd8010a2a5.zip
Selectmenu: Better handling when there are no options
Fixes #10662 Closes gh-1370 Closes gh-1423
-rw-r--r--tests/unit/selectmenu/selectmenu_methods.js14
-rw-r--r--ui/selectmenu.js28
2 files changed, 34 insertions, 8 deletions
diff --git a/tests/unit/selectmenu/selectmenu_methods.js b/tests/unit/selectmenu/selectmenu_methods.js
index ca1b8c6a3..0ff0f95c2 100644
--- a/tests/unit/selectmenu/selectmenu_methods.js
+++ b/tests/unit/selectmenu/selectmenu_methods.js
@@ -154,6 +154,20 @@ test( "refresh - disabled optgroup", function() {
}
});
+test( "refresh - remove all options", function() {
+ expect( 2 );
+
+ var element = $( "#speed" ).selectmenu(),
+ button = element.selectmenu( "widget" ),
+ menu = element.selectmenu( "menuWidget" );
+
+ element.children().remove();
+ element.selectmenu( "refresh" );
+ equal( button.find( ".ui-selectmenu-text" ).html(), $( "<span>&#160;</span>" ).html(),
+ "Empty button text" );
+ equal( menu.children().length, 0, "Empty menu" );
+});
+
test( "widget and menuWidget", function() {
expect( 4 );
diff --git a/ui/selectmenu.js b/ui/selectmenu.js
index 0bcf2a4db..19f27e809 100644
--- a/ui/selectmenu.js
+++ b/ui/selectmenu.js
@@ -66,6 +66,9 @@ return $.widget( "ui.selectmenu", {
this._drawButton();
this._drawMenu();
+ this._rendered = false;
+ this.menuItems = $();
+
if ( this.options.disabled ) {
this.disable();
}
@@ -119,7 +122,7 @@ return $.widget( "ui.selectmenu", {
// Delay rendering the menu items until the button receives focus.
// The menu may have already been rendered via a programmatic open.
- if ( !that.menuItems ) {
+ if ( !that._rendered ) {
that._refreshMenu();
}
});
@@ -199,7 +202,9 @@ return $.widget( "ui.selectmenu", {
this._refreshMenu();
this.buttonItem.replaceWith(
this.buttonItem = this._renderButtonItem(
- this._getSelectedItem().data( "ui-selectmenu-item" )
+
+ // Fall back to an empty object in case there are no options
+ this._getSelectedItem().data( "ui-selectmenu-item" ) || {}
)
);
if ( !this.options.width ) {
@@ -208,14 +213,10 @@ return $.widget( "ui.selectmenu", {
},
_refreshMenu: function() {
- this.menu.empty();
-
var item,
options = this.element.find( "option" );
- if ( !options.length ) {
- return;
- }
+ this.menu.empty();
this._parseOptions( options );
this._renderMenu( this.menu, this.items );
@@ -225,6 +226,12 @@ return $.widget( "ui.selectmenu", {
.not( ".ui-selectmenu-optgroup" )
.find( ".ui-menu-item-wrapper" );
+ this._rendered = true;
+
+ if ( !options.length ) {
+ return;
+ }
+
item = this._getSelectedItem();
// Update the menu to have the correct item focused
@@ -241,7 +248,7 @@ return $.widget( "ui.selectmenu", {
}
// If this is the first time the menu is being opened, render the items
- if ( !this.menuItems ) {
+ if ( !this._rendered ) {
this._refreshMenu();
} else {
@@ -250,6 +257,11 @@ return $.widget( "ui.selectmenu", {
this.menuInstance.focus( null, this._getSelectedItem() );
}
+ // If there are no options, don't open the menu
+ if ( !this.menuItems.length ) {
+ return;
+ }
+
this.isOpen = true;
this._toggleAttr();
this._resizeMenu();