Skip to content

Commit 368af59

Browse files
committed
Accordion: Handle invalid values for the active option.
1 parent 8b23483 commit 368af59

File tree

1 file changed

+22
-11
lines changed

1 file changed

+22
-11
lines changed

ui/jquery.ui.accordion.js

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -173,11 +173,14 @@ $.widget( "ui.accordion", {
173173
},
174174

175175
_setOption: function( key, value ) {
176-
$.Widget.prototype._setOption.apply( this, arguments );
177-
178176
if ( key == "active" ) {
177+
// _activate() will handle invalid values and update this.options
179178
this._activate( value );
179+
return;
180180
}
181+
182+
$.Widget.prototype._setOption.apply( this, arguments );
183+
181184
if ( key == "icons" ) {
182185
this._destroyIcons();
183186
if ( value ) {
@@ -273,20 +276,28 @@ $.widget( "ui.accordion", {
273276
},
274277

275278
_activate: function( index ) {
276-
// TODO: handle invalid values
277-
this.options.active = index;
278279
var active = this._findActive( index )[ 0 ];
280+
if ( !active ) {
281+
if ( !this.options.collapsible ) {
282+
return;
283+
}
284+
index = false;
285+
}
286+
this.options.active = index;
279287
this._eventHandler( { target: active, currentTarget: active } );
280288
},
281289

282290
_findActive: function( selector ) {
283-
return selector
284-
? typeof selector === "number"
285-
? this.headers.filter( ":eq(" + selector + ")" )
286-
: this.headers.not( this.headers.not( selector ) )
287-
: selector === false
288-
? $( [] )
289-
: this.headers.filter( ":eq(0)" );
291+
// handle -1 separately, we should drop support for this
292+
// so that we can allow selecting via negative index, like .eq()
293+
if ( selector === -1 ) {
294+
selector = undefined;
295+
}
296+
return typeof selector === "number" ?
297+
this.headers.eq( selector ) :
298+
selector ?
299+
this.headers.filter( selector ) :
300+
$( [] );
290301
},
291302

292303
_eventHandler: function( event ) {

0 commit comments

Comments
 (0)