diff --git a/docs/examples/widgets/selection_list_selections.py b/docs/examples/widgets/selection_list_selections.py index d959a689f..4a5e582a0 100644 --- a/docs/examples/widgets/selection_list_selections.py +++ b/docs/examples/widgets/selection_list_selections.py @@ -8,7 +8,7 @@ class SelectionListApp(App[None]): def compose(self) -> ComposeResult: yield Header() - yield SelectionList[int]( + yield SelectionList[int]( # (1)! Selection("Falken's Maze", 0, True), Selection("Black Jack", 1), Selection("Gin Rummy", 2), diff --git a/docs/examples/widgets/selection_list_tuples.py b/docs/examples/widgets/selection_list_tuples.py index 07b7d85bc..bff54e69c 100644 --- a/docs/examples/widgets/selection_list_tuples.py +++ b/docs/examples/widgets/selection_list_tuples.py @@ -7,7 +7,7 @@ class SelectionListApp(App[None]): def compose(self) -> ComposeResult: yield Header() - yield SelectionList[int]( + yield SelectionList[int]( # (1)! ("Falken's Maze", 0, True), ("Black Jack", 1), ("Gin Rummy", 2), diff --git a/docs/widgets/selection_list.md b/docs/widgets/selection_list.md index 232fceaa7..498d886fb 100644 --- a/docs/widgets/selection_list.md +++ b/docs/widgets/selection_list.md @@ -7,10 +7,31 @@ A widget for showing a vertical list check boxes. - [x] Focusable - [ ] Container +## Typing + +The `SelectionList` control is a +[`Generic`](https://docs.python.org/3/library/typing.html#typing.Generic), +which allows you to set the type of the +[selection values][textual.widgets.selection_list.Selection.value]. For instance, if +the data type for your values is an integer, you would type the widget as +follows: + +```python +selections = [("First", 1), ("Second", 2)] +my_selection_list: SelectionList[int] = SelectionList[int](selections) +``` + +!!! note + + Typing is entirely optional. + + If you aren't familiar with typing or don't want to worry about it right now, feel free to ignore it. + ## Examples A selection list is designed to be built up of single-line prompts (which -can be Rich renderables) and an associated unique value. +can be [Rich renderables](/guide/widgets/#rich-renderables)) and an +associated unique value. ### Selections as tuples @@ -29,6 +50,8 @@ optionally contain a flag for the initial selected state of the option. --8<-- "docs/examples/widgets/selection_list_tuples.py" ~~~ + 1. Note that the `SelectionList` is typed as `int`, for the type of the vlaues. + === "selection_list.css" ~~~python @@ -51,6 +74,8 @@ Alternatively, selections can be passed in as --8<-- "docs/examples/widgets/selection_list_selections.py" ~~~ + 1. Note that the `SelectionList` is typed as `int`, for the type of the vlaues. + === "selection_list.css" ~~~python