diff --git a/CHANGELOG.md b/CHANGELOG.md index b82e5c51fc..462d3650d4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/). - Fixed duplicated key displays in the help panel https://github.com/Textualize/textual/issues/5037 - Fixed `TextArea` mouse selection with tab characters https://github.com/Textualize/textual/issues/5212 +- Fixed `Tabs` not updating the highlighting after removing a tab https://github.com/Textualize/textual/issues/5218 ### Added diff --git a/src/textual/widgets/_tabs.py b/src/textual/widgets/_tabs.py index 5adad4895c..c347bf3521 100644 --- a/src/textual/widgets/_tabs.py +++ b/src/textual/widgets/_tabs.py @@ -556,10 +556,12 @@ def remove_tab(self, tab_or_id: Tab | str | None) -> AwaitComplete: async def do_remove() -> None: """Perform the remove after refresh so the underline bar gets new positions.""" await remove_tab.remove() - if next_tab is not None: - self.active = next_tab.id or "" if not self.query("#tabs-list > Tab"): self.active = "" + elif next_tab is not None: + self.active = next_tab.id or "" + else: + self._highlight_active(animate=False) return AwaitComplete(do_remove()) diff --git a/tests/snapshot_tests/__snapshots__/test_snapshots/test_tabs_remove_tab_updates_highlighting.svg b/tests/snapshot_tests/__snapshots__/test_snapshots/test_tabs_remove_tab_updates_highlighting.svg new file mode 100644 index 0000000000..918d739aca --- /dev/null +++ b/tests/snapshot_tests/__snapshots__/test_snapshots/test_tabs_remove_tab_updates_highlighting.svg @@ -0,0 +1,155 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + TabsApp + + + + + + + + + + bar +━━━╺━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ + + + + + + + + + + + + + + + + + + + + + + r Remove foo                                                       ^p palette + + + diff --git a/tests/snapshot_tests/test_snapshots.py b/tests/snapshot_tests/test_snapshots.py index 81803b4f06..1b94057834 100644 --- a/tests/snapshot_tests/test_snapshots.py +++ b/tests/snapshot_tests/test_snapshots.py @@ -30,6 +30,8 @@ SelectionList, Static, Switch, + Tab, + Tabs, TextArea, ) from textual.widgets.text_area import BUILTIN_LANGUAGES, Selection, TextAreaTheme @@ -2547,3 +2549,25 @@ async def run_before(pilot: Pilot): app = HelpPanelApp() assert snap_compare(app, run_before=run_before) + + +def test_tabs_remove_tab_updates_highlighting(snap_compare): + """Regression test for https://github.com/Textualize/textual/issues/5218""" + + class TabsApp(App): + BINDINGS = [("r", "remove_foo", "Remove foo")] + + def compose(self) -> ComposeResult: + yield Tabs( + Tab("foo", id="foo"), + Tab("bar", id="bar"), + active="bar", + ) + yield Footer() + + def action_remove_foo(self) -> None: + tabs = self.query_one(Tabs) + tabs.remove_tab("foo") + + app = TabsApp() + assert snap_compare(app, press="r")