Skip to content

A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.

fix: preserve table selection on accordion toggle (#3148)

devin-ai-integration[bot]a75baf6b872c
diff --git a/frontend/src/components/data-table/__tests__/data-table.test.tsx b/frontend/src/components/data-table/__tests__/data-table.test.tsx
new file mode 100644
index 0000000000..6e78d53db0
--- /dev/null
+++ b/frontend/src/components/data-table/__tests__/data-table.test.tsx
@@ -0,0 +1,52 @@
+import { render } from "@testing-library/react";
+import { DataTable } from "../data-table";
+import { describe, expect, it } from "vitest";
+import { vi } from "vitest";
+import type { ColumnDef } from "@tanstack/react-table";
+import type { RowSelectionState } from "@tanstack/react-table";
+
+interface TestData {
+ id: number;
+ name: string;
+}
+
+describe("DataTable", () => {
+ it("should maintain selection state when remounted", () => {
+ const mockOnRowSelectionChange = vi.fn();
+ const testData: TestData[] = [
+ { id: 1, name: "Test 1" },
+ { id: 2, name: "Test 2" },
+ ];
+
+ const columns: Array<ColumnDef<TestData>> = [
+ { accessorKey: "name", header: "Name" },
+ ];
+
+ const initialRowSelection: RowSelectionState = { "0": true };
+
+ const commonProps = {
+ data: testData,
+ columns,
+ selection: "single" as const,
+ totalRows: 2,
+ totalColumns: 1,
+ pagination: false,
+ rowSelection: initialRowSelection,
+ onRowSelectionChange: mockOnRowSelectionChange,
+ };
+
+ const { rerender } = render(<DataTable {...commonProps} />);
+
+ // Verify initial selection is not cleared
+ expect(mockOnRowSelectionChange).not.toHaveBeenCalledWith({});
+
+ // Simulate remount (as would happen in accordion toggle)
+ rerender(<DataTable {...commonProps} />);
+
+ // Verify selection is still not cleared after remount
+ expect(mockOnRowSelectionChange).not.toHaveBeenCalledWith({});
+
+ // Verify the rowSelection prop is maintained
+ expect(commonProps.rowSelection).toEqual(initialRowSelection);
+ });
+});
diff --git a/frontend/src/hooks/__tests__/useEffectSkipFirstRender.test.tsx b/frontend/src/hooks/__tests__/useEffectSkipFirstRender.test.tsx
new file mode 100644
index 0000000000..e6c876d797
--- /dev/null
+++ b/frontend/src/hooks/__tests__/useEffectSkipFirstRender.test.tsx
@@ -0,0 +1,41 @@
+import { renderHook } from "@testing-library/react";
+import { describe, expect, it, vi } from "vitest";
+import { useEffectSkipFirstRender } from "../useEffectSkipFirstRender";
+
+describe("useEffectSkipFirstRender", () => {
+ it("should skip callback on first render", () => {
+ const callback = vi.fn();
+ const { rerender } = renderHook(
+ ({ deps }) => useEffectSkipFirstRender(callback, deps),
+ { initialProps: { deps: [1] } },
+ );
+
+ expect(callback).not.toHaveBeenCalled();
+
+ rerender({ deps: [2] });
+ expect(callback).toHaveBeenCalledTimes(1);
+ });
+
+ it("should call callback on subsequent dependency changes", () => {
+ const callback = vi.fn();
+ const { rerender } = renderHook(
+ ({ deps }) => useEffectSkipFirstRender(callback, deps),
+ { initialProps: { deps: [1] } },
+ );
+
+ rerender({ deps: [2] });
+ rerender({ deps: [3] });
+ expect(callback).toHaveBeenCalledTimes(2);
+ });
+
+ it("should not call callback if dependencies haven't changed", () => {
+ const callback = vi.fn();
+ const { rerender } = renderHook(
+ ({ deps }) => useEffectSkipFirstRender(callback, deps),
+ { initialProps: { deps: [1] } },
+ );
+
+ rerender({ deps: [1] });
+ expect(callback).not.toHaveBeenCalled();
+ });
+});
diff --git a/frontend/src/hooks/useEffectSkipFirstRender.ts b/frontend/src/hooks/useEffectSkipFirstRender.ts
new file mode 100644
index 0000000000..4286bf67c9
--- /dev/null
+++ b/frontend/src/hooks/useEffectSkipFirstRender.ts
@@ -0,0 +1,23 @@
+import { useEffect, useRef } from "react";
+
+/**
+ * A hook that skips running a callback on the first render,
+ * then executes it on subsequent renders when dependencies change.
+ *
+ * @param callback - Function to execute after first render
+ * @param deps - Dependencies that trigger the callback when changed
+ */
+export function useEffectSkipFirstRender(
+ callback: () => void,
+ deps: React.DependencyList,
+) {
+ const hasRendered = useRef(false);
+
+ useEffect(() => {
+ if (!hasRendered.current) {
+ hasRendered.current = true;
+ return;
+ }
+ callback();
+ }, deps);
+}
diff --git a/frontend/src/plugins/impl/DataTablePlugin.tsx b/frontend/src/plugins/impl/DataTablePlugin.tsx
index 0fe2115afc..884eda4c1c 100644
--- a/frontend/src/plugins/impl/DataTablePlugin.tsx
+++ b/frontend/src/plugins/impl/DataTablePlugin.tsx
@@ -44,6 +44,7 @@ import { useAsyncData } from "@/hooks/useAsyncData";
import { useDeepCompareMemoize } from "@/hooks/useDeepCompareMemoize";
import { DelayMount } from "@/components/utils/delay-mount";
import { DATA_TYPES } from "@/core/kernel/messages";
+import { useEffectSkipFirstRender } from "../../hooks/useEffectSkipFirstRender";
type CsvURL = string;
type TableData<T> = T[] | CsvURL;
@@ -232,9 +233,9 @@ export const LoadingDataTableComponent = memo(
// We need to clear the selection when sort, query, or filters change
// Currently, our selection is index-based,
- // so we can't rely on the data to be the same
+ // so we can't rely on the data to be the same when these change
// We can remove this when we have a stable key for each row
- useEffect(() => {
+ useEffectSkipFirstRender(() => {
setValue([]);
}, [setValue, filters, searchQuery, sorting]);
diff --git a/marimo/_smoke_tests/issues/3142-table-in-accordion.py b/marimo/_smoke_tests/issues/3142-table-in-accordion.py
new file mode 100644
index 0000000000..07b292e79e
--- /dev/null
+++ b/marimo/_smoke_tests/issues/3142-table-in-accordion.py
@@ -0,0 +1,38 @@
+
+import marimo
+
+__generated_with = "0.9.34"
+app = marimo.App(width="medium")
+
+
+def __():
+ import marimo as mo
+ return (mo,)
+
+
+def __(mo):
+ table=mo.ui.table([{"col1": "hello"},{"col1": "world"}], selection="single")
+ return (table,)
+
+
+def __(mo, table):
+ if table.value:
+ val = table.value[0]["col1"]
+ else:
+ val = ""
+ text=mo.ui.text(label="Select from table", value=val)
+
+ return text, val
+
+
+def __(mo, table, text):
+ mo.accordion({"fold down": mo.vstack([table, text])})
+ return
+
+
+if __name__ == "__main__":
+ app.run()