Skip to content

Commit 65584c1

Browse files
committed
Selectmenu: Better handling when there are no options
Fixes #10662 Closes gh-1370 Closes gh-1423
1 parent 42099e4 commit 65584c1

File tree

2 files changed

+34
-8
lines changed

2 files changed

+34
-8
lines changed

tests/unit/selectmenu/selectmenu_methods.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,20 @@ test( "refresh - disabled optgroup", function() {
154154
}
155155
});
156156

157+
test( "refresh - remove all options", function() {
158+
expect( 2 );
159+
160+
var element = $( "#speed" ).selectmenu(),
161+
button = element.selectmenu( "widget" ),
162+
menu = element.selectmenu( "menuWidget" );
163+
164+
element.children().remove();
165+
element.selectmenu( "refresh" );
166+
equal( button.find( ".ui-selectmenu-text" ).html(), $( "<span>&#160;</span>" ).html(),
167+
"Empty button text" );
168+
equal( menu.children().length, 0, "Empty menu" );
169+
});
170+
157171
test( "widget and menuWidget", function() {
158172
expect( 4 );
159173

ui/selectmenu.js

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@ return $.widget( "ui.selectmenu", {
6666
this._drawButton();
6767
this._drawMenu();
6868

69+
this._rendered = false;
70+
this.menuItems = $();
71+
6972
if ( this.options.disabled ) {
7073
this.disable();
7174
}
@@ -119,7 +122,7 @@ return $.widget( "ui.selectmenu", {
119122

120123
// Delay rendering the menu items until the button receives focus.
121124
// The menu may have already been rendered via a programmatic open.
122-
if ( !that.menuItems ) {
125+
if ( !that._rendered ) {
123126
that._refreshMenu();
124127
}
125128
});
@@ -199,7 +202,9 @@ return $.widget( "ui.selectmenu", {
199202
this._refreshMenu();
200203
this.buttonItem.replaceWith(
201204
this.buttonItem = this._renderButtonItem(
202-
this._getSelectedItem().data( "ui-selectmenu-item" )
205+
206+
// Fall back to an empty object in case there are no options
207+
this._getSelectedItem().data( "ui-selectmenu-item" ) || {}
203208
)
204209
);
205210
if ( !this.options.width ) {
@@ -208,14 +213,10 @@ return $.widget( "ui.selectmenu", {
208213
},
209214

210215
_refreshMenu: function() {
211-
this.menu.empty();
212-
213216
var item,
214217
options = this.element.find( "option" );
215218

216-
if ( !options.length ) {
217-
return;
218-
}
219+
this.menu.empty();
219220

220221
this._parseOptions( options );
221222
this._renderMenu( this.menu, this.items );
@@ -225,6 +226,12 @@ return $.widget( "ui.selectmenu", {
225226
.not( ".ui-selectmenu-optgroup" )
226227
.find( ".ui-menu-item-wrapper" );
227228

229+
this._rendered = true;
230+
231+
if ( !options.length ) {
232+
return;
233+
}
234+
228235
item = this._getSelectedItem();
229236

230237
// Update the menu to have the correct item focused
@@ -241,7 +248,7 @@ return $.widget( "ui.selectmenu", {
241248
}
242249

243250
// If this is the first time the menu is being opened, render the items
244-
if ( !this.menuItems ) {
251+
if ( !this._rendered ) {
245252
this._refreshMenu();
246253
} else {
247254

@@ -250,6 +257,11 @@ return $.widget( "ui.selectmenu", {
250257
this.menuInstance.focus( null, this._getSelectedItem() );
251258
}
252259

260+
// If there are no options, don't open the menu
261+
if ( !this.menuItems.length ) {
262+
return;
263+
}
264+
253265
this.isOpen = true;
254266
this._toggleAttr();
255267
this._resizeMenu();

0 commit comments

Comments
 (0)