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.

standardize top k filter components and logic (#9393)

Shahmir Varqha750588444e90
diff --git a/frontend/src/components/data-table/__tests__/column-header.test.ts b/frontend/src/components/data-table/__tests__/column-header.test.ts
new file mode 100644
index 0000000000..9195d5d75a
--- /dev/null
+++ b/frontend/src/components/data-table/__tests__/column-header.test.ts
@@ -0,0 +1,63 @@
+/* Copyright 2026 Marimo. All rights reserved. */
+
+import { describe, expect, it } from "vitest";
+import { seedFromFilter } from "../column-header";
+import { Filter } from "../filters";
+
+describe("seedFromFilter", () => {
+ it("returns empty defaults when there is no filter", () => {
+ expect(seedFromFilter(undefined)).toEqual({
+ values: [],
+ operator: "in",
+ });
+ });
+
+ it("seeds values from an `in` select filter", () => {
+ const filter = Filter.select({
+ options: ["Flying", "Ground"],
+ operator: "in",
+ });
+ expect(seedFromFilter(filter)).toEqual({
+ values: ["Flying", "Ground"],
+ operator: "in",
+ });
+ });
+
+ it("preserves `not_in` so reapplying does not silently flip to `in`", () => {
+ const filter = Filter.select({
+ options: ["Fire"],
+ operator: "not_in",
+ });
+ expect(seedFromFilter(filter)).toEqual({
+ values: ["Fire"],
+ operator: "not_in",
+ });
+ });
+
+ it("returns a fresh array (callers may mutate without affecting the filter)", () => {
+ const options = ["a", "b"];
+ const filter = Filter.select({ options, operator: "in" });
+ const seeded = seedFromFilter(filter);
+ seeded.values.push("c");
+ expect(options).toEqual(["a", "b"]);
+ });
+
+ it("ignores non-select filters and falls back to defaults", () => {
+ expect(
+ seedFromFilter(Filter.text({ text: "abc", operator: "equals" })),
+ ).toEqual({
+ values: [],
+ operator: "in",
+ });
+ expect(seedFromFilter(Filter.number({ min: 0, max: 10 }))).toEqual({
+ values: [],
+ operator: "in",
+ });
+ expect(
+ seedFromFilter(Filter.boolean({ value: true, operator: "is_true" })),
+ ).toEqual({
+ values: [],
+ operator: "in",
+ });
+ });
+});
diff --git a/frontend/src/components/data-table/column-header.tsx b/frontend/src/components/data-table/column-header.tsx
index a5cc5b890e..0b74c1a1c9 100644
--- a/frontend/src/components/data-table/column-header.tsx
+++ b/frontend/src/components/data-table/column-header.tsx
@@ -9,7 +9,7 @@ import {
TextIcon,
XIcon,
} from "lucide-react";
-import { useMemo, useRef, useState } from "react";
+import { useRef, useState } from "react";
import { useLocale } from "react-aria";
import {
DropdownMenu,
@@ -22,24 +22,12 @@ import {
DropdownMenuSubTrigger,
DropdownMenuTrigger,
} from "@/components/ui/dropdown-menu";
-import { useAsyncData } from "@/hooks/useAsyncData";
-import { ErrorBanner } from "@/plugins/impl/common/error-banner";
import type { CalculateTopKRows } from "@/plugins/impl/DataTablePlugin";
import type { OperatorType } from "@/plugins/impl/data-frames/utils/operators";
import { logNever } from "@/utils/assertNever";
import { cn } from "@/utils/cn";
-import { Logger } from "@/utils/Logger";
import { capitalize } from "@/utils/strings";
-import { Spinner } from "../icons/spinner";
import { Button } from "../ui/button";
-import { Checkbox } from "../ui/checkbox";
-import {
- Command,
- CommandEmpty,
- CommandInput,
- CommandItem,
- CommandList,
-} from "../ui/command";
import { DraggablePopover } from "../ui/draggable-popover";
import { Input } from "../ui/input";
import { NumberField } from "../ui/number-field";
@@ -52,8 +40,12 @@ import {
SelectTrigger,
SelectValue,
} from "../ui/select";
-import { type ColumnFilterForType, Filter } from "./filters";
-import { SentinelCell } from "./sentinel-cell";
+import { FilterByValuesList } from "./filter-by-values-picker";
+import {
+ type ColumnFilterForType,
+ type ColumnFilterValue,
+ Filter,
+} from "./filters";
import {
ClearFilterMenuItem,
FilterButtons,
@@ -66,9 +58,6 @@ import {
renderSortFilterIcon,
renderSorts,
} from "./header-items";
-import { detectSentinel, stringifyUnknownValue } from "./utils";
-
-const TOP_K_ROWS = 30;
interface DataTableColumnHeaderProps<
TData,
@@ -534,6 +523,26 @@ const TextFilter = <TData, TValue>({
);
};
+/**
+ * Seed the filter-by-values picker from a column's existing filter value.
+ *
+ * Reopening the picker should reflect what's already applied. Only `select`
+ * filters carry checkbox-style values; other filter shapes (number, text,
+ * etc.) seed an empty list.
+ */
+export function seedFromFilter(value: ColumnFilterValue | undefined): {
+ values: unknown[];
+ operator: Extract<OperatorType, "in" | "not_in">;
+} {
+ if (value && "type" in value && value.type === "select") {
+ return {
+ values: [...value.options],
+ operator: value.operator === "not_in" ? "not_in" : "in",
+ };
+ }
+ return { values: [], operator: "in" };
+}
+
const PopoverFilterByValues = <TData, TValue>({
setIsFilterValueOpen,
calculateTopKRows,
@@ -543,69 +552,13 @@ const PopoverFilterByValues = <TData, TValue>({
calculateTopKRows?: CalculateTopKRows;
column: Column<TData, TValue>;
}) => {
- const [chosenValues, setChosenValues] = useState<Set<unknown>>(new Set());
- const [query, setQuery] = useState<string>("");
-
- const { data, isPending, error } = useAsyncData(async () => {
- if (!calculateTopKRows) {
- return null;
- }
- const res = await calculateTopKRows({ column: column.id, k: TOP_K_ROWS });
- return res.data;
- }, []);
-
- const filteredData = useMemo(() => {
- if (!data) {
- return [];
- }
-
- try {
- return data.filter(([value, _count]) => {
- // Check if value exists and can be converted to string
- // Keep null values for filtering
- return value === undefined
- ? false
- : String(value).toLowerCase().includes(query.toLowerCase());
- });
- } catch (error_) {
- Logger.error("Error filtering data", error_);
- return [];
- }
- }, [data, query]);
-
- let dataTable: React.ReactNode;
-
- if (isPending) {
- dataTable = <Spinner size="medium" className="mx-auto mt-12 mb-10" />;
- }
-
- if (error) {
- dataTable = <ErrorBanner error={error} className="my-10 mx-4" />;
- }
-
- const handleToggle = (value: unknown) => {
- setChosenValues((prev) => {
- const checked = prev.has(value);
- const newSet = new Set(prev);
- if (checked) {
- newSet.delete(value);
- } else {
- newSet.add(value);
- }
- return newSet;
- });
- };
+ const seed = seedFromFilter(
+ column.getFilterValue() as ColumnFilterValue | undefined,
+ );
- const handleToggleAll = (checked: boolean) => {
- if (!data) {
- return;
- }
- if (checked) {
- setChosenValues(new Set(filteredData.map(([value]) => value)));
- } else {
- setChosenValues(new Set());
- }
- };
+ const [chosenValues, setChosenValues] = useState<Set<unknown>>(
+ () => new Set(seed.values),
+ );
const handleApply = () => {
if (chosenValues.size === 0) {
@@ -613,87 +566,13 @@ const PopoverFilterByValues = <TData, TValue>({
return;
}
column.setFilterValue(
- Filter.select({ options: [...chosenValues], operator: "in" }),
+ Filter.select({
+ options: [...chosenValues],
+ operator: seed.operator,
+ }),
);
};
- if (data) {
- const allChecked = chosenValues.size === filteredData.length;
-
- dataTable = (
- <>
- <Command className="text-sm outline-hidden" shouldFilter={false}>
- <CommandInput
- placeholder={`Search among the top ${data.length} values`}
- autoFocus={true}
- onValueChange={(value) => setQuery(value.trim())}
- />
- <CommandEmpty>No results found.</CommandEmpty>
- <CommandList className="border-b">
- {filteredData.length > 0 && (
- <CommandItem
- value="__select-all__"
- className="border-b rounded-none px-3"
- onSelect={() => handleToggleAll(!allChecked)}
- >
- <Checkbox
- checked={chosenValues.size === filteredData.length}
- aria-label="Select all"
- className="mr-3 h-3.5 w-3.5"
- />
- <span className="font-bold flex-1">{column.id}</span>
- <span className="font-bold">Count</span>
- </CommandItem>
- )}
- {filteredData.map(([value, count], rowIndex) => {
- const isSelected = chosenValues.has(value);
- const valueString = stringifyUnknownValue({ value });
- const sentinel = detectSentinel(
- value,
- column.columnDef.meta?.dataType,
- );
-
- return (
- <CommandItem
- key={rowIndex}
- value={valueString}
- className="not-last:border-b rounded-none px-3"
- onSelect={() => handleToggle(value)}
- >
- <Checkbox
- checked={isSelected}
- aria-label="Select row"
- className="mr-3 h-3.5 w-3.5"
- />
- <span className="flex-1 overflow-hidden max-h-20 line-clamp-3">
- {sentinel ? (
- <SentinelCell sentinel={sentinel} />
- ) : (
- valueString
- )}
- </span>
- <span className="ml-3">{count}</span>
- </CommandItem>
- );
- })}
- </CommandList>
- {filteredData.length === TOP_K_ROWS && (
- <span className="text-xs text-muted-foreground mt-1.5 text-center">
- Only showing the top {TOP_K_ROWS} values
- </span>
- )}
- </Command>
- <FilterButtons
- onApply={handleApply}
- onClear={() => {
- setChosenValues(new Set());
- }}
- clearButtonDisabled={chosenValues.size === 0}
- />
- </>
- );
- }
-
return (
<DraggablePopover
open={true}
@@ -709,7 +588,19 @@ const PopoverFilterByValues = <TData, TValue>({
<XIcon className="h-4 w-4" />
</Button>
</PopoverClose>
- <div className="flex flex-col gap-1.5 py-2">{dataTable}</div>
+ <div className="flex flex-col gap-1.5 py-2">
+ <FilterByValuesList
+ column={column}
+ calculateTopKRows={calculateTopKRows}
+ chosenValues={chosenValues}
+ onChange={(values) => setChosenValues(new Set(values))}
+ />
+ <FilterButtons
+ onApply={handleApply}
+ onClear={() => setChosenValues(new Set())}
+ clearButtonDisabled={chosenValues.size === 0}
+ />
+ </div>
</DraggablePopover>
);
};
diff --git a/frontend/src/components/data-table/filter-by-values-picker.tsx b/frontend/src/components/data-table/filter-by-values-picker.tsx
index a6e328f33c..052aaf1ed7 100644
--- a/frontend/src/components/data-table/filter-by-values-picker.tsx
+++ b/frontend/src/components/data-table/filter-by-values-picker.tsx
@@ -74,7 +74,7 @@ export const FilterByValuesPicker = <TData, TValue>({
</Button>
</PopoverTrigger>
<PopoverContent className="w-80 p-0">
- <PickerBody
+ <FilterByValuesList
column={column}
calculateTopKRows={calculateTopKRows}
chosenValues={chosenValuesSet}
@@ -85,19 +85,22 @@ export const FilterByValuesPicker = <TData, TValue>({
);
};
-interface PickerBodyProps<TData, TValue> {
+interface FilterByValuesListProps<TData, TValue> {
column: Column<TData, TValue>;
calculateTopKRows?: CalculateTopKRows;
chosenValues: Set<unknown>;
onChange: (values: unknown[]) => void;
}
-const PickerBody = <TData, TValue>({
+/**
+ * Search + checkbox list that powers the "filter by values" picker.
+ */
+export const FilterByValuesList = <TData, TValue>({
column,
calculateTopKRows,
chosenValues,
onChange,
-}: PickerBodyProps<TData, TValue>) => {
+}: FilterByValuesListProps<TData, TValue>) => {
const [query, setQuery] = useState<string>("");
const { data, isPending, error } = useAsyncData(async () => {