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.
Python artificial-intelligencedagdata-sciencedata-visualizationdataflowdeveloper-toolsmachine-learningnotebookspipelinepythonreactivesqlweb-app
fixes: range focus improvements (#5353)
Shahmir Varqha8404dcce69ee
diff --git a/frontend/src/components/data-table/columns.tsx b/frontend/src/components/data-table/columns.tsxindex 40a318f8c3..6adbfd318e 100644--- a/frontend/src/components/data-table/columns.tsx+++ b/frontend/src/components/data-table/columns.tsx@@ -31,6 +31,7 @@ import { parseContent, UrlDetector } from "./url-detector";// Artificial limit to display long stringsconst MAX_STRING_LENGTH = 50;+const SELECT_ID = "__select__";function inferDataType(value: unknown): [type: DataType, displayType: string] {if (typeof value === "string") {@@ -245,7 +246,7 @@ export function generateColumns<T>({if (selection === "single" || selection === "multi") {columns.unshift({- id: "__select__",+ id: SELECT_ID,maxSize: 40,header: ({ table }) =>selection === "multi" ? (@@ -266,6 +267,10 @@ export function generateColumns<T>({onCheckedChange={(value) => row.toggleSelected(!!value)}aria-label="Select row"className="mx-2"+ onMouseDown={(e) => {+ // Prevent cell underneath from being selected+ e.stopPropagation();+ }}/>),enableSorting: false,@@ -294,7 +299,14 @@ const PopoutColumn = ({return (<EmotionCacheProvider container={null}><Popover>- <PopoverTrigger className={cellStyles} onClick={selectCell}>+ <PopoverTrigger+ className={cn(cellStyles, "w-fit outline-none")}+ onClick={selectCell}+ onMouseDown={(e) => {+ // Prevent cell underneath from being selected+ e.stopPropagation();+ }}+ ><spanclassName="cursor-pointer hover:text-link"title={rawStringValue}diff --git a/frontend/src/components/data-table/range-focus/__tests__/atoms.test.ts b/frontend/src/components/data-table/range-focus/__tests__/atoms.test.tsindex 04c2fbbc82..1c98fb7c3e 100644--- a/frontend/src/components/data-table/range-focus/__tests__/atoms.test.ts+++ b/frontend/src/components/data-table/range-focus/__tests__/atoms.test.ts@@ -56,6 +56,19 @@ function createMockCell(}function createMockTable(): Table<T> {+ const mockToggleSelected = vi.fn().mockImplementation(function (this: {+ isSelected: boolean;+ }) {+ this.isSelected = !this.isSelected;+ return { isSelected: this.isSelected };+ });++ const mockGetIsSelected = vi.fn().mockImplementation(function (this: {+ isSelected: boolean;+ }) {+ return this.isSelected;+ });+const rows = [{id: "row1",@@ -65,6 +78,9 @@ function createMockTable(): Table<T> {createMockCell("row1", "col2", 0, 1),createMockCell("row1", "col3", 0, 2),],+ toggleSelected: mockToggleSelected,+ getIsSelected: mockGetIsSelected,+ isSelected: false,},{id: "row2",@@ -74,6 +90,9 @@ function createMockTable(): Table<T> {createMockCell("row2", "col2", 1, 1),createMockCell("row2", "col3", 1, 2),],+ toggleSelected: mockToggleSelected,+ getIsSelected: mockGetIsSelected,+ isSelected: false,},{id: "row3",@@ -83,6 +102,9 @@ function createMockTable(): Table<T> {createMockCell("row3", "col2", 2, 1),createMockCell("row3", "col3", 2, 2),],+ toggleSelected: mockToggleSelected,+ getIsSelected: mockGetIsSelected,+ isSelected: false,},];@@ -190,6 +212,74 @@ describe("cell selection atoms", () => {expect(state.selectedStartCell).toBeNull();expect(state.focusedCell).toBeNull();});++ it("can select all cells", () => {+ actions.selectAllCells(mockTable);+ const allCells = mockTable+ .getRowModel()+ .rows.flatMap((row) => row.getAllCells().map((cell) => cell.id));+ expect(state.selectedCells).toEqual(new Set(allCells));+ });+ });++ describe("toggleSelectionOfCurrentRow", () => {+ it("can toggle row selection on current cell", () => {+ const focusedCell: SelectedCell = {+ rowId: "row1",+ columnId: "col1",+ cellId: "row1_col1",+ };+ actions.setFocusedCell(focusedCell);++ // Initial state should be unselected+ const row = mockTable.getRow("row1");+ expect(row.getIsSelected()).toBe(false);++ // First toggle should select the row+ actions.toggleCurrentRowSelection(mockTable);+ expect(row.toggleSelected).toHaveBeenCalled();+ expect(row.getIsSelected()).toBe(true);++ // Second toggle should unselect the row+ actions.toggleCurrentRowSelection(mockTable);+ expect(row.getIsSelected()).toBe(false);+ });++ it("should not toggle row selection without focused cell", () => {+ // Clear focused cell+ actions.setFocusedCell(null);++ const row = mockTable.getRow("row1");+ const initialState = row.getIsSelected();++ actions.toggleCurrentRowSelection(mockTable);++ // Selection state should not change+ expect(row.getIsSelected()).toBe(initialState);+ expect(row.toggleSelected).not.toHaveBeenCalled();+ });++ it("should toggle selection of different rows", () => {+ // Toggle row1+ actions.setFocusedCell({+ rowId: "row1",+ columnId: "col1",+ cellId: "row1_col1",+ });+ actions.toggleCurrentRowSelection(mockTable);+ expect(mockTable.getRow("row1").getIsSelected()).toBe(true);++ // Toggle row2+ actions.setFocusedCell({+ rowId: "row2",+ columnId: "col1",+ cellId: "row2_col1",+ });+ actions.toggleCurrentRowSelection(mockTable);+ expect(mockTable.getRow("row2").getIsSelected()).toBe(true);+ // row1 should still be selected+ expect(mockTable.getRow("row1").getIsSelected()).toBe(true);+ });});describe("updateSelection", () => {diff --git a/frontend/src/components/data-table/range-focus/__tests__/utils.test.ts b/frontend/src/components/data-table/range-focus/__tests__/utils.test.tsindex 524a52b2b4..fccee8443d 100644--- a/frontend/src/components/data-table/range-focus/__tests__/utils.test.ts+++ b/frontend/src/components/data-table/range-focus/__tests__/utils.test.ts@@ -2,6 +2,7 @@import type { Cell, Column, Row, Table } from "@tanstack/react-table";import { describe, expect, it, vi } from "vitest";+import { SELECT_COLUMN_ID } from "../../types";import type { SelectedCell } from "../atoms";import { getCellsBetween, getCellValues } from "../utils";@@ -68,6 +69,15 @@ describe("getCellValues", () => {expect(result).toBe("");});+ it("should ignore select checkbox in tables", () => {+ const cell = createMockCell(`row_${SELECT_COLUMN_ID}`, "test");+ const row = createMockRow("0", [cell]);+ const table = createMockTable([row], []);++ const result = getCellValues(table, new Set());+ expect(result).toBe("");+ });+it("should return single cell value", () => {const cell = createMockCell("0_0", "test");const row = createMockRow("0", [cell]);diff --git a/frontend/src/components/data-table/range-focus/atoms.ts b/frontend/src/components/data-table/range-focus/atoms.tsindex 3ba6de0153..c2a3a76cc9 100644--- a/frontend/src/components/data-table/range-focus/atoms.ts+++ b/frontend/src/components/data-table/range-focus/atoms.ts@@ -66,6 +66,23 @@ const {focusedCell: null,};},+ selectAllCells: (state, table: AnyTable) => {+ const allCells = table+ .getRowModel()+ .rows.flatMap((row) => row.getAllCells().map((cell) => cell.id));+ return {+ ...state,+ selectedCells: new Set(allCells),+ };+ },+ toggleCurrentRowSelection: (state, table: AnyTable) => {+ const currentCell = state.focusedCell;+ if (currentCell?.rowId) {+ const row = table.getRow(currentCell?.rowId);+ row?.toggleSelected?.();+ }+ return state;+ },updateSelection: (state,{diff --git a/frontend/src/components/data-table/range-focus/use-cell-range-selection.ts b/frontend/src/components/data-table/range-focus/use-cell-range-selection.tsindex 3f6517ca31..b6626e6a46 100644--- a/frontend/src/components/data-table/range-focus/use-cell-range-selection.ts+++ b/frontend/src/components/data-table/range-focus/use-cell-range-selection.ts@@ -68,6 +68,19 @@ export const useCellRangeSelection = <TData>({e.preventDefault();navigate(e, "right");break;+ case "Enter":+ e.preventDefault();+ actions.toggleCurrentRowSelection(table);+ break;+ case "Escape":+ actions.clearSelection();+ break;+ case "a":+ if (e.metaKey || e.ctrlKey) {+ e.preventDefault();+ actions.selectAllCells(table);+ }+ break;}});diff --git a/frontend/src/components/data-table/range-focus/utils.ts b/frontend/src/components/data-table/range-focus/utils.tsindex a28863726f..61d4105ee7 100644--- a/frontend/src/components/data-table/range-focus/utils.ts+++ b/frontend/src/components/data-table/range-focus/utils.ts@@ -2,6 +2,7 @@import type { Table } from "@tanstack/react-table";import { renderUnknownValue } from "../renderers";+import { SELECT_COLUMN_ID } from "../types";import type { SelectedCell } from "./atoms";/**@@ -14,6 +15,11 @@ export function getCellValues<TData>(const rowValues = new Map<string, string[]>();for (const cellId of selectedCellIds) {+ if (cellId.includes(SELECT_COLUMN_ID)) {+ // Ignore select checkbox in tables+ continue;+ }+const [rowId] = cellId.split("_"); // CellId is rowId_columnIdconst row = table.getRow(rowId);if (!row) {diff --git a/frontend/src/components/data-table/renderers.tsx b/frontend/src/components/data-table/renderers.tsxindex 9256f9f97e..5c9e205200 100644--- a/frontend/src/components/data-table/renderers.tsx+++ b/frontend/src/components/data-table/renderers.tsx@@ -122,8 +122,10 @@ export const DataTableBody = <TData,>({};const handleRowClick = (row: Row<TData>) => {- const rowIndex = getRowIndex?.(row.original, row.index) ?? row.index;- row.focusRow?.(rowIndex);+ if (rowViewerPanelOpen) {+ const rowIndex = getRowIndex?.(row.original, row.index) ?? row.index;+ row.focusRow?.(rowIndex);+ }};return (diff --git a/marimo/_smoke_tests/tables/rich_elements.py b/marimo/_smoke_tests/tables/rich_elements.pyindex c57b8449db..86bead80bf 100644--- a/marimo/_smoke_tests/tables/rich_elements.py+++ b/marimo/_smoke_tests/tables/rich_elements.py@@ -93,6 +93,10 @@ def _(alt, mo, pd):data = {"buttons": [mo.ui.button(kind="warn"), mo.ui.button(kind="invalid")],+ "checkbox": [+ mo.ui.checkbox(value=True, label="True"),+ mo.ui.checkbox(value=False, label="False"),+ ],"mixed": [mo.ui.button(kind="info"), "apples"],"arrays": mo.ui.array([text] * 2),"dictionary": [dictionary, dictionary],