Skip to content

Commit 9d42eb2

Browse files
authored
Merge pull request #5314 from Textualize/select-highlight
fix select highlight
2 parents 901fee4 + 3488b2f commit 9d42eb2

File tree

5 files changed

+96
-67
lines changed

5 files changed

+96
-67
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](http://keepachangelog.com/)
66
and this project adheres to [Semantic Versioning](http://semver.org/).
77

8+
## Unreleased
9+
10+
## Changed
11+
12+
- Fixed Select not scrolling highlight in to view when clicked https://github.com/Textualize/textual/issues/5255
13+
814
## [0.88.1] - 2024-11-30
915

1016
### Fixed

src/textual/widgets/_option_list.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ class OptionList(ScrollView, can_focus=True):
195195
| `option-list--separator` | Target the separators. |
196196
"""
197197

198-
highlighted: reactive[int | None] = reactive["int | None"](None)
198+
highlighted: reactive[int | None] = reactive(None)
199199
"""The index of the currently-highlighted option, or `None` if no option is highlighted."""
200200

201201
class OptionMessage(Message):
@@ -880,6 +880,7 @@ def scroll_to_highlight(self, top: bool = False) -> None:
880880
top: Scroll highlight to top of the list.
881881
"""
882882
highlighted = self.highlighted
883+
883884
if highlighted is None or not self.is_mounted:
884885
return
885886

@@ -901,6 +902,10 @@ def scroll_to_highlight(self, top: bool = False) -> None:
901902
immediate=True,
902903
)
903904

905+
def on_show(self) -> None:
906+
if self.highlighted is not None:
907+
self.scroll_to_highlight()
908+
904909
def validate_highlighted(self, highlighted: int | None) -> int | None:
905910
"""Validate the `highlighted` property value on access."""
906911
if highlighted is None or not self._options:

src/textual/widgets/_select.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,7 @@ def _setup_variables_for_options(
414414

415415
def _setup_options_renderables(self) -> None:
416416
"""Sets up the `Option` renderables associated with the `Select` options."""
417-
self._select_options: list[Option] = [
417+
options: list[Option] = [
418418
(
419419
Option(Text(self.prompt, style="dim"))
420420
if value == self.BLANK
@@ -425,7 +425,7 @@ def _setup_options_renderables(self) -> None:
425425

426426
option_list = self.query_one(SelectOverlay)
427427
option_list.clear_options()
428-
option_list.add_options(self._select_options)
428+
option_list.add_options(options)
429429

430430
def _init_selected_option(self, hint: SelectType | NoSelection = BLANK) -> None:
431431
"""Initialises the selected option for the `Select`."""

tests/snapshot_tests/__snapshots__/test_snapshots/test_select_width_auto.svg

Lines changed: 64 additions & 63 deletions
Loading

tests/snapshot_tests/test_snapshots.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2744,7 +2744,7 @@ def compose(self) -> ComposeResult:
27442744
yield Select(
27452745
[("Extra long option here", 100)]
27462746
+ [(f"Option {idx + 1}", idx) for idx in range(100)],
2747-
value=25,
2747+
value=100,
27482748
)
27492749

27502750
async def run_before(pilot: Pilot) -> None:
@@ -2841,3 +2841,20 @@ def action_add_pane(self) -> None:
28412841
tabbed_content.add_pane(new_pane)
28422842

28432843
snap_compare(ExampleApp(), press=["a", "r", "a"])
2844+
2845+
2846+
def test_click_expand(snap_compare):
2847+
"""Should show an expanded select with 15 highlighted."""
2848+
2849+
class SelectApp(App):
2850+
def compose(self) -> ComposeResult:
2851+
yield Select.from_values(
2852+
range(20),
2853+
value=15,
2854+
)
2855+
2856+
async def run_before(pilot: Pilot) -> None:
2857+
await pilot.pause()
2858+
await pilot.click(Select)
2859+
2860+
snap_compare(SelectApp(), run_before=run_before)

0 commit comments

Comments
 (0)