Add support for sending a message when the selection list changes

The developer using this may wish to react to UI changes being made, but
they may also want to just know when the collection of selected values has
changed -- this could happen via code so won't get any UI/IO messages. So
this adds a message that is always sent when a change to the collection of
selected values happens.
This commit is contained in:
Dave Pearson
2023-05-22 15:23:11 +01:00
parent a25ef78a7f
commit 189181ba33

View File

@@ -2,6 +2,7 @@
from __future__ import annotations
from dataclasses import dataclass
from typing import ClassVar, Generic, TypeVar, cast
from rich.repr import Result
@@ -162,6 +163,18 @@ class SelectionList(Generic[SelectionType], OptionList):
`SelectionList` or in a parent node in the DOM.
"""
@dataclass
class SelectedChanged(Generic[MessageSelectionType], Message):
"""Message sent when the collection of selected values changes."""
selection_list: SelectionList[MessageSelectionType]
"""The `SelectionList` that sent the message."""
@property
def control(self) -> SelectionList[MessageSelectionType]:
"""An alias for `selection_list`."""
return self.selection_list
def __init__(
self,
*selections: tuple[TextType, SelectionType]
@@ -200,7 +213,9 @@ class SelectionList(Generic[SelectionType], OptionList):
Args:
value: The value to mark as selected.
"""
if value not in self._selected:
self._selected[value] = None
self.post_message(self.SelectedChanged(self))
def select(self, selection: Selection[SelectionType] | SelectionType) -> Self:
"""Mark the given selection as selected.
@@ -224,6 +239,7 @@ class SelectionList(Generic[SelectionType], OptionList):
"""
try:
del self._selected[value]
self.post_message(self.SelectedChanged(self))
except KeyError:
pass