Skip to content

Commit e88f8dd

Browse files
authored
Merge branch 'master' into titles
2 parents 91ba29d + 28ab143 commit e88f8dd

File tree

3 files changed

+17
-8
lines changed

3 files changed

+17
-8
lines changed

ipywidgets/widgets/widget_selectioncontainer.py

+12-3
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,16 @@
1010
from .widget_box import Box
1111
from .widget import register
1212
from .widget_core import CoreWidget
13-
from traitlets import Unicode, Dict, CInt, TraitError, validate
13+
from traitlets import Unicode, Dict, CInt, TraitError, validate, observe
1414
from .trait_types import TypedTuple
1515

16-
1716
class _SelectionContainer(Box, CoreWidget):
1817
"""Base class used to display multiple child widgets."""
1918
titles = TypedTuple(trait=Unicode(), help="Titles of the pages").tag(sync=True)
2019
selected_index = CInt(
2120
help="""The index of the selected page. This is either an integer selecting a particular sub-widget, or None to have no widgets selected.""",
22-
allow_none=True
21+
allow_none=True,
22+
default_value=None
2323
).tag(sync=True)
2424

2525
@validate('selected_index')
@@ -29,6 +29,11 @@ def _validated_index(self, proposal):
2929
else:
3030
raise TraitError('Invalid selection: index out of bounds')
3131

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+
3237
@register
3338
class Accordion(_SelectionContainer):
3439
"""Displays children each on a separate accordion page."""
@@ -42,6 +47,10 @@ class Tab(_SelectionContainer):
4247
_view_name = Unicode('TabView').tag(sync=True)
4348
_model_name = Unicode('TabModel').tag(sync=True)
4449

50+
def __init__(self, **kwargs):
51+
if 'children' in kwargs and 'selected_index' not in kwargs and len(kwargs['children']) > 0:
52+
kwargs['selected_index'] = 0
53+
super(Tab, self).__init__(**kwargs)
4554

4655
@register
4756
class Stacked(_SelectionContainer):

packages/controls/src/widget_selectioncontainer.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ export class SelectionContainerModel extends BoxModel {
3131
return {
3232
...super.defaults(),
3333
_model_name: 'SelectionContainerModel',
34-
selected_index: 0,
35-
titles: []
34+
selected_index: null,
35+
_titles: []
3636
};
3737
}
3838
}

packages/schema/jupyterwidgetmodels.latest.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ Attribute | Type | Default | Help
7777
`box_style` | string (one of `'success'`, `'info'`, `'warning'`, `'danger'`, `''`) | `''` | Use a predefined styling for the box.
7878
`children` | array of reference to Widget widget | `[]` | List of widget children
7979
`layout` | reference to Layout widget | reference to new instance |
80-
`selected_index` | `null` or number (integer) | `0` | The index of the selected page. This is either an integer selecting a particular sub-widget, or None to have no widgets selected.
80+
`selected_index` | `null` or number (integer) | `null` | The index of the selected page. This is either an integer selecting a particular sub-widget, or None to have no widgets selected.
8181
`tabbable` | `null` or boolean | `null` | Is widget tabbable?
8282
`titles` | array of string | `[]` | Titles of the pages
8383
`tooltip` | `null` or string | `null` | A tooltip caption.
@@ -955,7 +955,7 @@ Attribute | Type | Default | Help
955955
`box_style` | string (one of `'success'`, `'info'`, `'warning'`, `'danger'`, `''`) | `''` | Use a predefined styling for the box.
956956
`children` | array of reference to Widget widget | `[]` | List of widget children
957957
`layout` | reference to Layout widget | reference to new instance |
958-
`selected_index` | `null` or number (integer) | `0` | The index of the selected page. This is either an integer selecting a particular sub-widget, or None to have no widgets selected.
958+
`selected_index` | `null` or number (integer) | `null` | The index of the selected page. This is either an integer selecting a particular sub-widget, or None to have no widgets selected.
959959
`tabbable` | `null` or boolean | `null` | Is widget tabbable?
960960
`titles` | array of string | `[]` | Titles of the pages
961961
`tooltip` | `null` or string | `null` | A tooltip caption.
@@ -974,7 +974,7 @@ Attribute | Type | Default | Help
974974
`box_style` | string (one of `'success'`, `'info'`, `'warning'`, `'danger'`, `''`) | `''` | Use a predefined styling for the box.
975975
`children` | array of reference to Widget widget | `[]` | List of widget children
976976
`layout` | reference to Layout widget | reference to new instance |
977-
`selected_index` | `null` or number (integer) | `0` | The index of the selected page. This is either an integer selecting a particular sub-widget, or None to have no widgets selected.
977+
`selected_index` | `null` or number (integer) | `null` | The index of the selected page. This is either an integer selecting a particular sub-widget, or None to have no widgets selected.
978978
`tabbable` | `null` or boolean | `null` | Is widget tabbable?
979979
`titles` | array of string | `[]` | Titles of the pages
980980
`tooltip` | `null` or string | `null` | A tooltip caption.

0 commit comments

Comments
 (0)