Skip to content

Commit 55bf408

Browse files
committed
Fix selection container logic around setting titles.
Previously, the title logic was being overridden by Tab. This refactors the code so we can just override the selected index logic, without overriding the titles logic. Fixes #3520
1 parent 056f1d1 commit 55bf408

File tree

1 file changed

+14
-7
lines changed

1 file changed

+14
-7
lines changed

python/ipywidgets/ipywidgets/widgets/widget_selectioncontainer.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,15 @@ def _validate_titles(self, proposal):
4141

4242
@observe('children')
4343
def _observe_children(self, change):
44-
if self.selected_index is not None and len(change.new) < self.selected_index:
44+
self._reset_selected_index()
45+
self._reset_titles()
46+
47+
def _reset_selected_index(self):
48+
if self.selected_index is not None and len(self.children) < self.selected_index:
4549
self.selected_index = None
46-
if len(self.titles) != len(change.new):
50+
51+
def _reset_titles(self):
52+
if len(self.titles) != len(self.children):
4753
# Run validation function
4854
self.titles = tuple(self.titles)
4955

@@ -90,10 +96,10 @@ def __init__(self, **kwargs):
9096
kwargs['selected_index'] = 0
9197
super(Tab, self).__init__(**kwargs)
9298

93-
@observe('children')
94-
def _observe_children(self, change):
99+
def _reset_selected_index(self):
95100
# if there are no tabs, then none should be selected
96-
if len(change.new) == 0:
101+
num_children = len(self.children)
102+
if num_children == 0:
97103
self.selected_index = None
98104

99105
# if there are tabs, but none is selected, select the first one
@@ -102,8 +108,9 @@ def _observe_children(self, change):
102108

103109
# if there are tabs and a selection, but the selection is no longer
104110
# valid, select the last tab.
105-
elif len(change.new) < self.selected_index:
106-
self.selected_index = len(change.new) - 1
111+
elif num_children < self.selected_index:
112+
self.selected_index = num_children - 1
113+
107114

108115

109116
@register

0 commit comments

Comments
 (0)