Skip to content

Commit 9ebeb06

Browse files
committed
Tabs: Walk previous tabs (and loop) in refresh() in case the tab we're trying to activate is disabled.
1 parent 9e805c0 commit 9ebeb06

File tree

2 files changed

+33
-3
lines changed

2 files changed

+33
-3
lines changed

tests/unit/tabs/tabs_methods.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,26 @@ test( "refresh", function() {
148148
disabled( element, false );
149149
});
150150

151+
test( "refresh - looping", function() {
152+
expect( 6 );
153+
154+
var element = $( "#tabs1" ).tabs({
155+
disabled: [ 0 ],
156+
active: 1
157+
});
158+
state( element, 0, 1, 0 );
159+
disabled( element, [ 0 ] );
160+
161+
// remove active, jump to previous
162+
// previous is disabled, just back one more
163+
// reached first tab, move to end
164+
// activate last tab
165+
element.find( ".ui-tabs-nav li" ).eq( 2 ).remove();
166+
element.tabs( "refresh" );
167+
state( element, 0, 1 );
168+
disabled( element, [ 0 ] );
169+
});
170+
151171
asyncTest( "load", function() {
152172
expect( 30 );
153173

ui/jquery.ui.tabs.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ $.widget( "ui.tabs", {
229229
}
230230
},
231231

232-
_focusNextTab: function( index, goingForward ) {
232+
_findNextTab: function( index, goingForward ) {
233233
var lastTabIndex = this.tabs.length - 1;
234234

235235
function constrain() {
@@ -246,6 +246,11 @@ $.widget( "ui.tabs", {
246246
index = goingForward ? index + 1 : index - 1;
247247
}
248248

249+
return index;
250+
},
251+
252+
_focusNextTab: function( index, goingForward ) {
253+
index = this._findNextTab( index, goingForward );
249254
this.tabs.eq( index ).focus();
250255
return index;
251256
},
@@ -309,9 +314,14 @@ $.widget( "ui.tabs", {
309314
this.active = $();
310315
// was active, but active tab is gone
311316
} else if ( this.active.length && !$.contains( this.tablist[ 0 ], this.active[ 0 ] ) ) {
317+
// all remaining tabs are disabled
318+
if ( this.tabs.length === options.disabled.length ) {
319+
options.active = false;
320+
this.active = $();
312321
// activate previous tab
313-
next = options.active - 1;
314-
this._activate( next >= 0 ? next : 0 );
322+
} else {
323+
this._activate( this._findNextTab( Math.max( 0, options.active - 1 ), false ) );
324+
}
315325
// was active, active tab still exists
316326
} else {
317327
// make sure active index is correct

0 commit comments

Comments
 (0)