Skip to content

Commit 4a37fff

Browse files
authored
Added Selection widget to Toga web (#3402)
1 parent e056c0c commit 4a37fff

File tree

6 files changed

+51
-5
lines changed

6 files changed

+51
-5
lines changed

changes/3334.feature.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
The Web backend now provides Selection widgets.

docs/reference/api/widgets/selection.rst

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,11 @@ A widget to select a single option from a list of alternatives.
3535
:align: center
3636
:width: 300px
3737

38-
.. group-tab:: Web |no|
38+
.. group-tab:: Web
3939

40-
Not supported
40+
.. figure:: /reference/images/selection-web.png
41+
:align: center
42+
:width: 300px
4143

4244
.. group-tab:: Textual |no|
4345

docs/reference/data/widgets_by_platform.csv

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ MultilineTextInput,General Widget,:class:`~toga.MultilineTextInput`,Multi-line T
1616
NumberInput,General Widget,:class:`~toga.NumberInput`,A text input that is limited to numeric input,|y|,|y|,|y|,|y|,|y|,,
1717
PasswordInput,General Widget,:class:`~toga.PasswordInput`,A text input that hides its input,|y|,|y|,|y|,|y|,|y|,|b|,
1818
ProgressBar,General Widget,:class:`~toga.ProgressBar`,Progress Bar,|y|,|y|,|y|,|y|,|y|,|b|,
19-
Selection,General Widget,:class:`~toga.Selection`,A widget to select a single option from a list of alternatives.,|y|,|y|,|y|,|y|,|y|,,
19+
Selection,General Widget,:class:`~toga.Selection`,A widget to select a single option from a list of alternatives.,|y|,|y|,|y|,|y|,|y|,|b|,
2020
Slider,General Widget,:class:`~toga.Slider`,Slider,|y|,|y|,|y|,|y|,|y|,,
2121
Switch,General Widget,:class:`~toga.Switch`,Switch,|y|,|y|,|y|,|y|,|y|,|b|,
2222
Table,General Widget,:class:`~toga.Table`,A widget for displaying columns of tabular data.,|y|,|y|,|y|,,|b|,,
8.33 KB
Loading

web/src/toga_web/factory.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@
2626
from .widgets.passwordinput import PasswordInput
2727
from .widgets.progressbar import ProgressBar
2828
from .widgets.scrollcontainer import ScrollContainer
29+
from .widgets.selection import Selection
2930

30-
# from .widgets.selection import Selection
3131
# from .widgets.slider import Slider
3232
# from .widgets.splitcontainer import SplitContainer
3333
from .widgets.switch import Switch
@@ -76,7 +76,7 @@ def not_implemented(feature):
7676
"ProgressBar",
7777
"ActivityIndicator",
7878
"ScrollContainer",
79-
# 'Selection',
79+
"Selection",
8080
# 'Slider',
8181
# 'SplitContainer',
8282
"Switch",

web/src/toga_web/widgets/selection.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
from js import CustomEvent
2+
3+
from toga_web.libs import create_proxy
4+
5+
from .base import Widget
6+
7+
8+
class Selection(Widget):
9+
10+
def create(self):
11+
self.native = self._create_native_widget("sl-select")
12+
self.native.addEventListener("sl-change", create_proxy(self.dom_sl_change))
13+
14+
def dom_sl_change(self, event):
15+
self.interface.on_change()
16+
17+
def clear(self):
18+
while self.native.firstElementChild:
19+
self.native.removeChild(self.native.firstElementChild)
20+
21+
def insert(self, index, item):
22+
display_text = self.interface._title_for_item(item)
23+
option = self._create_native_widget("sl-option")
24+
option.value = str(index)
25+
option.textContent = display_text
26+
if self.native.value == "":
27+
self.native.value = option.value
28+
if index >= len(self.native.children):
29+
self.native.appendChild(option)
30+
else:
31+
self.native.insertBefore(option, self.native.children[index])
32+
33+
def get_selected_index(self):
34+
if self.native.value:
35+
return int(self.native.value)
36+
return None
37+
38+
def select_item(self, index, item):
39+
self.native.value = str(index)
40+
self.native.dispatchEvent(CustomEvent.new("sl-change"))
41+
42+
def rehint(self):
43+
pass

0 commit comments

Comments
 (0)