10
10
from .widget_box import Box
11
11
from .widget import register
12
12
from .widget_core import CoreWidget
13
- from traitlets import Unicode , Dict , CInt , TraitError , validate
13
+ from traitlets import Unicode , Dict , CInt , TraitError , validate , observe
14
14
15
15
16
16
class _SelectionContainer (Box , CoreWidget ):
17
17
"""Base class used to display multiple child widgets."""
18
18
_titles = Dict (help = "Titles of the pages" ).tag (sync = True )
19
19
selected_index = CInt (
20
20
help = """The index of the selected page. This is either an integer selecting a particular sub-widget, or None to have no widgets selected.""" ,
21
- allow_none = True
21
+ allow_none = True ,
22
+ default_value = None
22
23
).tag (sync = True )
23
24
24
25
@validate ('selected_index' )
@@ -28,6 +29,11 @@ def _validated_index(self, proposal):
28
29
else :
29
30
raise TraitError ('Invalid selection: index out of bounds' )
30
31
32
+ @observe ('children' )
33
+ def _observe_children (self , change ):
34
+ if self .selected_index is not None and len (change .new ) < self .selected_index :
35
+ self .selected_index = None
36
+
31
37
# Public methods
32
38
def set_title (self , index , title ):
33
39
"""Sets the title of a container page.
@@ -79,6 +85,15 @@ class Tab(_SelectionContainer):
79
85
_view_name = Unicode ('TabView' ).tag (sync = True )
80
86
_model_name = Unicode ('TabModel' ).tag (sync = True )
81
87
88
+ def __init__ (self , ** kwargs ):
89
+ if 'children' in kwargs and 'selected_index' not in kwargs and len (kwargs ['children' ]) > 0 :
90
+ kwargs ['selected_index' ] = 0
91
+ super (Tab , self ).__init__ (** kwargs )
92
+
93
+ @observe ('children' )
94
+ def _observe_children (self , change ):
95
+ if len (change .new ) > 0 :
96
+ self .selected_index = 0
82
97
83
98
@register
84
99
class Stacked (_SelectionContainer ):
0 commit comments