|
11 | 11 | from textual.app import App, ComposeResult
|
12 | 12 | from textual.binding import Binding
|
13 | 13 | from textual.command import SimpleCommand
|
14 |
| -from textual.containers import Center, Container, Grid, Middle, Vertical, VerticalScroll |
| 14 | +from textual.containers import ( |
| 15 | + Center, |
| 16 | + Container, |
| 17 | + Grid, |
| 18 | + Middle, |
| 19 | + Vertical, |
| 20 | + VerticalScroll, |
| 21 | + HorizontalGroup, |
| 22 | +) |
15 | 23 | from textual.pilot import Pilot
|
16 | 24 | from textual.renderables.gradient import LinearGradient
|
17 | 25 | from textual.screen import ModalScreen, Screen
|
|
22 | 30 | Header,
|
23 | 31 | Input,
|
24 | 32 | Label,
|
| 33 | + ListItem, |
| 34 | + ListView, |
25 | 35 | Log,
|
26 | 36 | OptionList,
|
27 | 37 | Placeholder,
|
@@ -3139,3 +3149,47 @@ def compose(self) -> ComposeResult:
|
3139 | 3149 | yield Sidebar()
|
3140 | 3150 |
|
3141 | 3151 | snap_compare(FloatSidebarApp())
|
| 3152 | + |
| 3153 | + |
| 3154 | +def test_select_refocus(snap_compare): |
| 3155 | + """Regression test for https://github.com/Textualize/textual/issues/5416 |
| 3156 | +
|
| 3157 | + The original bug was that the call to focus had no apparent effect as the Select |
| 3158 | + was re-focusing itself after the Changed message was processed. |
| 3159 | +
|
| 3160 | + You should see a list view with three items, where the second one is in focus. |
| 3161 | +
|
| 3162 | + """ |
| 3163 | + opts = ["foo", "bar", "zoo"] |
| 3164 | + |
| 3165 | + class MyListItem(ListItem): |
| 3166 | + def __init__(self, opts: list[str]) -> None: |
| 3167 | + self.opts = opts |
| 3168 | + self.lab = Label("Hello!") |
| 3169 | + self.sel = Select(options=[(opt, opt) for opt in self.opts]) |
| 3170 | + super().__init__() |
| 3171 | + |
| 3172 | + def compose(self): |
| 3173 | + with HorizontalGroup(): |
| 3174 | + yield self.lab |
| 3175 | + yield self.sel |
| 3176 | + |
| 3177 | + def on_select_changed(self, event: Select.Changed): |
| 3178 | + self.app.query_one(MyListView).focus() |
| 3179 | + |
| 3180 | + class MyListView(ListView): |
| 3181 | + def compose(self): |
| 3182 | + yield MyListItem(opts) |
| 3183 | + yield MyListItem(opts) |
| 3184 | + yield MyListItem(opts) |
| 3185 | + |
| 3186 | + def on_list_view_selected(self, event: ListView.Selected): |
| 3187 | + event.item.sel.focus() |
| 3188 | + event.item.sel.expanded = True |
| 3189 | + |
| 3190 | + class TUI(App): |
| 3191 | + def compose(self): |
| 3192 | + with Container(): |
| 3193 | + yield MyListView() |
| 3194 | + |
| 3195 | + snap_compare(TUI(), press=["down", "enter", "down", "down", "enter"]) |
0 commit comments