Skip to content

Commit

Permalink
fix(selection list): update indexes after option removed (#4464)
Browse files Browse the repository at this point in the history
* fix(selection list): update indexes after option removed

* add basic test

* update changelog

* improve test

---------

Co-authored-by: Darren Burns <[email protected]>
  • Loading branch information
TomJGooding and darrenburns authored May 2, 2024
1 parent 5d97439 commit 17e975d
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
4 changes: 1 addition & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,13 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

### Fixed

- Fixed `SelectionList` issues after removing an option https://github.com/Textualize/textual/pull/4464
- Fixed `ListView` bugs with the initial index https://github.com/Textualize/textual/pull/4452

## Unreleased

### Changed

- When displaying a message using `App.exit()`, the console no longer highlights things such as numbers.


## [0.58.1] - 2024-05-01

### Fixed
Expand Down
5 changes: 5 additions & 0 deletions src/textual/widgets/_selection_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -646,6 +646,11 @@ def _remove_option(self, index: int) -> None:
option = self.get_option_at_index(index)
self._deselect(option.value)
del self._values[option.value]
# Decrement index of options after the one we just removed.
self._values = {
option_value: option_index - 1 if option_index > index else option_index
for option_value, option_index in self._values.items()
}
return super()._remove_option(index)

def add_options(
Expand Down
9 changes: 9 additions & 0 deletions tests/selection_list/test_selection_list_create.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,3 +114,12 @@ async def test_options_are_available_soon() -> None:
selection = Selection("", 0, id="some_id")
selection_list = SelectionList[int](selection)
assert selection_list.get_option("some_id") is selection


async def test_removing_option_updates_indexes() -> None:
async with SelectionListApp().run_test() as pilot:
selections = pilot.app.query_one(SelectionList)
assert selections._values == {n: n for n in range(5)}

selections.remove_option_at_index(0)
assert selections._values == {n + 1: n for n in range(4)}

0 comments on commit 17e975d

Please sign in to comment.