Skip to content

Commit 348cf22

Browse files
authored
Merge pull request #2827 from jasongrout/tabselection
Fix the logic for tab selection when the children changes to select a tab when possible
2 parents e55a5fb + eba6ddf commit 348cf22

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

ipywidgets/widgets/widget_selectioncontainer.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,22 @@ def __init__(self, **kwargs):
5252
kwargs['selected_index'] = 0
5353
super(Tab, self).__init__(**kwargs)
5454

55+
@observe('children')
56+
def _observe_children(self, change):
57+
# if there are no tabs, then none should be selected
58+
if len(change.new) == 0:
59+
self.selected_index = None
60+
61+
# if there are tabs, but none is selected, select the first one
62+
elif self.selected_index == None:
63+
self.selected_index = 0
64+
65+
# if there are tabs and a selection, but the selection is no longer
66+
# valid, select the last tab.
67+
elif len(change.new) < self.selected_index:
68+
self.selected_index = len(change.new) - 1
69+
70+
5571
@register
5672
class Stacked(_SelectionContainer):
5773
"""Displays only the selected child."""

0 commit comments

Comments
 (0)