diff --git a/docs/examples/widgets/data_table.py b/docs/examples/widgets/data_table.py index 9a0550f34..f409bb45c 100644 --- a/docs/examples/widgets/data_table.py +++ b/docs/examples/widgets/data_table.py @@ -26,6 +26,6 @@ class TableApp(App): table.add_rows(rows) +app = TableApp() if __name__ == "__main__": - app = TableApp() app.run() diff --git a/tests/snapshot_tests/__snapshots__/test_snapshots.ambr b/tests/snapshot_tests/__snapshots__/test_snapshots.ambr index 262a3fa8a..388723c00 100644 --- a/tests/snapshot_tests/__snapshots__/test_snapshots.ambr +++ b/tests/snapshot_tests/__snapshots__/test_snapshots.ambr @@ -10624,6 +10624,163 @@ ''' # --- +# name: test_datatable_sort_multikey + ''' + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + TableApp + + + + + + + + + +  lane  swimmer               country        time   +  4     Joseph Schooling      Singapore      50.39  +  2     Michael Phelps        United States  51.14  +  5     Chad le Clos          South Africa   51.14  +  6     László Cseh           Hungary        51.14  +  3     Li Zhuhao             China          51.26  +  8     Mehdy Metella         France         51.58  +  7     Tom Shields           United States  51.73  +  1     Aleksandr Sadovnikov  Russia         51.84  +  10    Darren Burns          Scotland       51.84  + + + + + + + + + + + + + + + + + + + ''' +# --- # name: test_demo ''' diff --git a/tests/snapshot_tests/snapshot_apps/data_table_sort.py b/tests/snapshot_tests/snapshot_apps/data_table_sort.py new file mode 100644 index 000000000..b4866e0ca --- /dev/null +++ b/tests/snapshot_tests/snapshot_apps/data_table_sort.py @@ -0,0 +1,44 @@ +from textual.app import App, ComposeResult +from textual.binding import Binding +from textual.widgets import DataTable + +# Shuffled around a bit to exercise sorting. +ROWS = [ + ("lane", "swimmer", "country", "time"), + (5, "Chad le Clos", "South Africa", 51.14), + (4, "Joseph Schooling", "Singapore", 50.39), + (2, "Michael Phelps", "United States", 51.14), + (6, "László Cseh", "Hungary", 51.14), + (3, "Li Zhuhao", "China", 51.26), + (8, "Mehdy Metella", "France", 51.58), + (7, "Tom Shields", "United States", 51.73), + (10, "Darren Burns", "Scotland", 51.84), + (1, "Aleksandr Sadovnikov", "Russia", 51.84), +] + + +class TableApp(App): + BINDINGS = [ + Binding("s", "sort", "Sort"), + ] + + def compose(self) -> ComposeResult: + yield DataTable() + + def on_mount(self) -> None: + table = self.query_one(DataTable) + table.focus() + rows = iter(ROWS) + column_labels = next(rows) + for column in column_labels: + table.add_column(column, key=column) + table.add_rows(rows) + + def action_sort(self): + table = self.query_one(DataTable) + table.sort("time", "lane") + + +app = TableApp() +if __name__ == "__main__": + app.run() diff --git a/tests/snapshot_tests/test_snapshots.py b/tests/snapshot_tests/test_snapshots.py index 9ce48d171..ef7ab29b6 100644 --- a/tests/snapshot_tests/test_snapshots.py +++ b/tests/snapshot_tests/test_snapshots.py @@ -103,6 +103,11 @@ def test_datatable_column_cursor_render(snap_compare): assert snap_compare(SNAPSHOT_APPS_DIR / "data_table_column_cursor.py", press=press) +def test_datatable_sort_multikey(snap_compare): + press = ["down", "right", "s"] # Also checks that sort doesn't move cursor. + assert snap_compare(SNAPSHOT_APPS_DIR / "data_table_sort.py", press=press) + + def test_footer_render(snap_compare): assert snap_compare(WIDGET_EXAMPLES_DIR / "footer.py")