Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(selection list): update indexes after option removed #4464

Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).

## Unreleased

### Fixed

- Fixed `SelectionList` issues after removing an option https://github.com/Textualize/textual/pull/4464

## [0.58.1] - 2024-05-01

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.
Copy link
Member

@darrenburns darrenburns May 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe a separate issue (or not an issue at all if I'm missing something), but this makes me wonder why the highlighted index wouldn't be decremented if it appears below the removed option.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually from a skim, I'm guessing it might be that the highlight is applied based on the ID of the option rather than the index. It's just converted to an ID via a validator.

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)}
Loading