Skip to content

Commit 0376350

Browse files
committed
Allow none in accordion widget
1 parent 5da2c82 commit 0376350

File tree

3 files changed

+25
-19
lines changed

3 files changed

+25
-19
lines changed

ipywidgets/widgets/widget_selectioncontainer.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
class _SelectionContainer(Box, CoreWidget):
1717
"""Base class used to display multiple child widgets."""
1818
_titles = Dict(help="Titles of the pages").tag(sync=True)
19-
selected_index = CInt(help="The index of the selected page.").tag(sync=True)
19+
selected_index = CInt(help="The index of the selected page.", allow_none=True).tag(sync=True)
2020

2121
# Public methods
2222
def set_title(self, index, title):

packages/controls/src/phosphor/currentselection.ts

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ class Selection<T> {
9393
*/
9494
set value(value: T) {
9595
if (value === null) {
96-
this.index = -1;
96+
this.index = null;
9797
} else {
9898
this.index = ArrayExt.firstIndexOf(this._array, value);
9999
}
@@ -103,9 +103,9 @@ class Selection<T> {
103103
* Get the index of the currently selected item.
104104
*
105105
* #### Notes
106-
* This will be `-1` if no item is selected.
106+
* This will be `null` if no item is selected.
107107
*/
108-
get index(): number {
108+
get index(): number | null {
109109
return this._index;
110110
}
111111

@@ -115,14 +115,20 @@ class Selection<T> {
115115
* @param index - The index to select.
116116
*
117117
* #### Notes
118-
* If the value is out of range, the index will be set to `-1`, which
118+
* If the value is out of range, the index will be set to `null`, which
119119
* indicates no item is selected.
120120
*/
121-
set index(index: number) {
121+
set index(index: number | null) {
122122
// Coerce the value to an index.
123-
let i = Math.floor(index);
124-
if (i < 0 || i >= this._array.length) {
125-
i = -1;
123+
let i;
124+
if (index !== null) {
125+
i = Math.floor(index);
126+
if (i < 0 || i >= this._array.length) {
127+
i = null;
128+
}
129+
}
130+
else {
131+
i = null;
126132
}
127133

128134
// Bail early if the index will not change.
@@ -194,7 +200,7 @@ class Selection<T> {
194200

195201
// Handle the behavior where the new item is always selected,
196202
// or the behavior where the new item is selected if needed.
197-
if (bh === 'select-item' || (bh === 'select-item-if-needed' && ci === -1)) {
203+
if (bh === 'select-item' || (bh === 'select-item-if-needed' && ci === null)) {
198204
this._index = i;
199205
this._value = item;
200206
this._previousValue = cv;
@@ -240,19 +246,19 @@ class Selection<T> {
240246
let pv = this._value;
241247

242248
// Reset the current index and previous item.
243-
this._index = -1;
249+
this._index = null;
244250
this._value = null;
245251
this._previousValue = null;
246252

247253
// If no item was selected, there's nothing else to do.
248-
if (pi === -1) {
254+
if (pi === null) {
249255
return;
250256
}
251257

252258
// Emit the current changed signal.
253259
this._selectionChanged.emit({
254260
previousIndex: pi, previousValue: pv,
255-
currentIndex: -1, currentValue: null
261+
currentIndex: this._index, currentValue: this._value
256262
});
257263
}
258264

@@ -283,12 +289,12 @@ class Selection<T> {
283289
// No item gets selected if the vector is empty.
284290
if (this._array.length === 0) {
285291
// Reset the current index and previous item.
286-
this._index = -1;
292+
this._index = null;
287293
this._value = null;
288294
this._previousValue = null;
289295
this._selectionChanged.emit({
290296
previousIndex: i, previousValue: item,
291-
currentIndex: -1, currentValue: null
297+
currentIndex: this._index, currentValue: this._value
292298
});
293299
return;
294300
}
@@ -334,12 +340,12 @@ class Selection<T> {
334340
}
335341

336342
// Otherwise, no item gets selected.
337-
this._index = -1;
343+
this._index = null;
338344
this._value = null;
339345
this._previousValue = null;
340346
this._selectionChanged.emit({
341347
previousIndex: i, previousValue: item,
342-
currentIndex: -1, currentValue: null
348+
currentIndex: this._index, currentValue: this._value
343349
});
344350
}
345351

@@ -348,7 +354,7 @@ class Selection<T> {
348354
*/
349355
private _updateSelectedValue() {
350356
let i = this._index;
351-
this._value = i !== -1 ? this._array[i] : null;
357+
this._value = i !== null ? this._array[i] : null;
352358
}
353359

354360
private _array: ReadonlyArray<T> = null;

packages/controls/src/widget_selectioncontainer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ class AccordionView extends DOMWidgetView {
156156
// tabs before updating so we don't get spurious changes in the index,
157157
// which would then set off another sync cycle.
158158
this.updatingChildren = true;
159-
this.pWidget.selection.index = -1;
159+
this.pWidget.selection.index = null;
160160
this.children_views.update(this.model.get('children'));
161161
this.update_selected_index();
162162
this.updatingChildren = false;

0 commit comments

Comments
 (0)