Skip to content

Commit 5f0abd2

Browse files
committed
Introduce a ToggleButtons button_width style attribute.
Fixes #1189
1 parent 2c58d42 commit 5f0abd2

3 files changed

Lines changed: 47 additions & 4 deletions

File tree

ipywidgets/widgets/widget_selection.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515
from .domwidget import LabeledWidget
1616
from .valuewidget import ValueWidget
1717
from .widget_core import CoreWidget
18-
from .widget import register
18+
from .widget_style import Style
19+
from .trait_types import InstanceDict
20+
from .widget import register, widget_serialization
1921
from traitlets import (Unicode, Bool, Int, Any, Dict, TraitError, CaselessStrEnum,
2022
Tuple, List, Union, observe, validate)
2123
from ipython_genutils.py3compat import unicode_type
@@ -202,6 +204,11 @@ def _validate_value(self, proposal):
202204
except KeyError as k:
203205
raise TraitError('Invalid selection: %r'%(k.args[0],))
204206

207+
@register
208+
class ToggleButtonsStyle(Style, CoreWidget):
209+
"""Button style widget."""
210+
_model_name = Unicode('ToggleButtonsStyleModel').tag(sync=True)
211+
button_width = Unicode(help="The width of each button.").tag(sync=True)
205212

206213
@register
207214
class ToggleButtons(_Selection):
@@ -214,6 +221,7 @@ class ToggleButtons(_Selection):
214221

215222
tooltips = List(Unicode()).tag(sync=True)
216223
icons = List(Unicode()).tag(sync=True)
224+
style = InstanceDict(ToggleButtonsStyle).tag(sync=True, **widget_serialization)
217225

218226
button_style = CaselessStrEnum(
219227
values=['primary', 'success', 'info', 'warning', 'danger', ''],

jupyter-js-widgets/src/widget_selection.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ import {
55
CoreLabeledDOMWidgetModel,
66
} from './widget_core';
77

8+
import {
9+
StyleModel
10+
} from './widget_style';
11+
812
import {
913
LabeledDOMWidgetView, unpack_models, ViewList
1014
} from './widget';
@@ -316,6 +320,23 @@ class RadioButtonsView extends LabeledDOMWidgetView {
316320
container: HTMLDivElement;
317321
}
318322

323+
export
324+
class ToggleButtonsStyleModel extends StyleModel {
325+
defaults() {
326+
return _.extend(super.defaults(), {
327+
_model_name: 'ToggleButtonsStyleModel',
328+
});
329+
}
330+
331+
public static styleProperties = {
332+
button_width: {
333+
selector: '.widget-toggle-button',
334+
attribute: 'width',
335+
default: null
336+
}
337+
};
338+
}
339+
319340
export
320341
class ToggleButtonsModel extends SelectionModel {
321342
defaults() {
@@ -424,6 +445,12 @@ class ToggleButtonsView extends LabeledDOMWidgetView {
424445
}
425446
});
426447

448+
this.stylePromise.then(function(style) {
449+
if (style) {
450+
style.style();
451+
}
452+
});
453+
427454
return super.update(options);
428455
}
429456

jupyter-js-widgets/src/widget_style.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ class StyleView extends WidgetView {
4141
for (let key of Object.keys(ModelType.styleProperties)) {
4242
this.registerTrait(key)
4343
}
44+
// Set the initial styles
45+
this.style();
4446
}
4547

4648
/**
@@ -54,9 +56,6 @@ class StyleView extends WidgetView {
5456
this.listenTo(this.model, 'change:' + trait, (model, value) => {
5557
this.handleChange(trait, value);
5658
});
57-
58-
// Set the initial value on display.
59-
this.handleChange(trait, this.model.get(trait));
6059
}
6160

6261
/**
@@ -85,6 +84,15 @@ class StyleView extends WidgetView {
8584
}
8685
}
8786

87+
/**
88+
* Apply styles for all registered traits
89+
*/
90+
style() {
91+
for (let trait of this._traitNames) {
92+
this.handleChange(trait, this.model.get(trait));
93+
}
94+
}
95+
8896
/**
8997
* Remove the styling from the parent view.
9098
*/

0 commit comments

Comments
 (0)