feat(grid): add suggested values to filtering criteria

This commit is contained in:
HE WENJUN authored and GitHub committed 2026-09-28 13:14:19 +08:00
1 parent f76cb4e46b
commit ebae26ef34
16 files changed
+1349 -173

No files matched your search

@@ -0,0 +1,43 @@
import { describe, expect, it } from "vitest";
import { formatFilterRawValue, formatFilterRawValues, parseFilterValue, parseFilterValues } from "@/lib/dataGrid/dataGridColumnFilter";
import { dataGridNullSuggestionFilterMode, toggleAllDataGridDistinctValueOptions } from "@/lib/dataGrid/dataGridDistinctValueSuggestions";
describe("data grid filter candidate value serialization", () => {
it("keeps simple single values readable and quotes surrounding whitespace", () => {
expect(formatFilterRawValue("active")).toBe("active");
const raw = formatFilterRawValue(" active ");
expect(raw).toBe("' active '");
expect(parseFilterValue(raw)).toBe(" active ");
expect(parseFilterValue(formatFilterRawValue("'quoted'"))).toBe("'quoted'");
});
it("round-trips commas, newlines, quotes, and the literal string null in lists", () => {
const raw = formatFilterRawValues(["north,east", "line 1\nline 2", "O'Brien", "null"]);
expect(parseFilterValues(raw)).toEqual(["north,east", "line 1\nline 2", "O'Brien", "null"]);
});
it("maps a selected NULL candidate to a null-aware single-value operator", () => {
expect(dataGridNullSuggestionFilterMode("equals")).toBe("is-null");
expect(dataGridNullSuggestionFilterMode("like")).toBe("is-null");
expect(dataGridNullSuggestionFilterMode("not-equals")).toBe("is-not-null");
expect(dataGridNullSuggestionFilterMode("not-like")).toBe("is-not-null");
});
it("toggles all currently displayed candidates without removing hidden selections", () => {
const options = [
{ key: "str:a", label: "a", value: "a", count: 2 },
{ key: "str:b", label: "b", value: "b", count: 1 },
];
const hiddenSelection = new Map<string, string>([["str:manual", "manual"]]);
const selected = toggleAllDataGridDistinctValueOptions(hiddenSelection, options);
expect([...selected.entries()]).toEqual([
["str:manual", "manual"],
["str:a", "a"],
["str:b", "b"],
]);
const cleared = toggleAllDataGridDistinctValueOptions(selected, options);
expect([...cleared.entries()]).toEqual([["str:manual", "manual"]]);
});
});
@@ -230,6 +230,21 @@ export function parseFilterValues(rawValue: string, columnInfo?: Pick<DataGridCo
});
}
export function formatFilterRawValue(value: GridCellValue, quoteStrings = false): string {
if (typeof value === "string") {
const wrappedInMatchingQuotes = value.length >= 2 && ((value.startsWith("'") && value.endsWith("'")) || (value.startsWith('"') && value.endsWith('"')));
if (!quoteStrings && value.trim() === value && !wrappedInMatchingQuotes) return value;
return `'${value.replaceAll("'", "''")}'`;
}
if (value === null) return "null";
if (typeof value === "object") return `'${JSON.stringify(value).replaceAll("'", "''")}'`;
return String(value);
}
export function formatFilterRawValues(values: readonly GridCellValue[]): string {
return values.map((value) => formatFilterRawValue(value, true)).join(", ");
}
type ParsedFilterValue = {
value: string;
quoted: boolean;
@@ -295,7 +310,8 @@ function unwrapMatchingQuotes(text: string): string {
const first = text[0];
const last = text[text.length - 1];
if ((first === "'" && last === "'") || (first === '"' && last === '"')) {
return text.slice(1, -1);
const quote = first as "'" | '"';
return text.slice(1, -1).replaceAll(`${quote}${quote}`, quote);
}
}
return text;
@@ -0,0 +1,41 @@
import type { DataGridLocalFilterOption } from "@/lib/dataGrid/dataGridLocalColumnFilterState";
import { dataGridLocalFilterKey } from "@/lib/dataGrid/dataGridLocalColumnFilterState";
import type { CellValue } from "@/lib/dataGrid/cellValue";
import type { DataGridContextFilterMode } from "@/lib/dataGrid/dataGridSql";
import type { ColumnInfo } from "@/types/database";
export type DataGridDistinctValueSuggestionTarget = "value" | "end";
export interface DataGridDistinctValueSuggestionState {
ruleId?: string;
target?: DataGridDistinctValueSuggestionTarget;
search: string;
options: DataGridLocalFilterOption[];
loading: boolean;
error: string;
limited: boolean;
limit: number;
selectedKeys: Set<string>;
}
export function dataGridDistinctValueKey(value: CellValue, columnInfo?: Pick<ColumnInfo, "data_type">): string {
const typeParts = (columnInfo?.data_type ?? "").toLowerCase().split(/[^a-z0-9]+/);
if (typeParts.some((part) => ["int", "integer", "bigint", "smallint", "tinyint", "mediumint", "serial", "number", "numeric", "decimal", "float", "double", "real", "money"].includes(part))) return `num:${String(value)}`;
return dataGridLocalFilterKey(value);
}
export function dataGridNullSuggestionFilterMode(mode: DataGridContextFilterMode): "is-null" | "is-not-null" {
return mode === "not-equals" || mode === "not-like" || mode === "not-between" ? "is-not-null" : "is-null";
}
export function toggleAllDataGridDistinctValueOptions(selectedValues: ReadonlyMap<string, CellValue>, options: readonly DataGridLocalFilterOption[]): Map<string, CellValue> {
const next = new Map(selectedValues);
if (!options.length) return next;
const allSelected = options.every((option) => next.has(option.key));
for (const option of options) {
if (allSelected) next.delete(option.key);
else next.set(option.key, option.value);
}
return next;
}
@@ -105,6 +105,7 @@ export interface DataGridColumnDistinctValuesSqlOptions {
searchValue?: string;
limit?: number;
includeCounts?: boolean;
excludeNulls?: boolean;
}
export interface DataGridCountSqlOptions {