feat(data-grid): add configurable default sorting

This commit is contained in:
二丫讲梵 authored and GitHub committed 2026-07-29 04:46:46 +08:00
1 parent fdaef62518
commit 2e5b439c8a
24 files changed
+720 -17

No files matched your search

@@ -0,0 +1,46 @@
import { describe, expect, it } from "vitest";
import { resolveDataGridHeaderSortAction } from "@/lib/dataGrid/dataGridSort";
describe("resolveDataGridHeaderSortAction", () => {
it("keeps the existing menu action when quick sorting is disabled", () => {
expect(
resolveDataGridHeaderSortAction({
enabled: false,
configuredDirection: "desc",
configuredMode: "database",
currentColumnSorted: false,
currentDirection: "asc",
currentMode: "local",
}),
).toEqual({ kind: "menu" });
});
it.each([
["local", "asc"],
["database", "desc"],
] as const)("uses the explicit %s/%s preference for an unsorted column", (configuredMode, configuredDirection) => {
expect(
resolveDataGridHeaderSortAction({
enabled: true,
configuredDirection,
configuredMode,
currentColumnSorted: false,
currentDirection: "asc",
currentMode: "database",
}),
).toEqual({ kind: "sort", direction: configuredDirection, mode: configuredMode });
});
it("toggles an existing sort while preserving its scope", () => {
expect(
resolveDataGridHeaderSortAction({
enabled: true,
configuredDirection: "asc",
configuredMode: "database",
currentColumnSorted: true,
currentDirection: "asc",
currentMode: "local",
}),
).toEqual({ kind: "sort", direction: "desc", mode: "local" });
});
});
@@ -0,0 +1,48 @@
import { describe, expect, it } from "vitest";
import { buildOpenTableDefaultSort, buildPrimaryKeyOrderBy } from "@/lib/table/tableDefaultSort";
describe("buildPrimaryKeyOrderBy", () => {
it.each([
["postgres", '"Tenant" ASC, "Order" ASC'],
["mysql", "`Tenant` ASC, `Order` ASC"],
["sqlserver", "[Tenant] ASC, [Order] ASC"],
["oracle", '"Tenant" ASC, "Order" ASC'],
["neo4j", "n.`Tenant` ASC, n.`Order` ASC"],
] as const)("quotes composite keys for %s", (databaseType, expected) => {
expect(
buildPrimaryKeyOrderBy({
databaseType,
primaryKeys: ["Tenant", "Order"],
direction: "asc",
}),
).toBe(expected);
});
it("quotes every mixed-case or reserved identifier and applies descending to every key", () => {
expect(
buildPrimaryKeyOrderBy({
databaseType: "postgres",
primaryKeys: ['Tenant"Id', "select"],
direction: "desc",
}),
).toBe('"Tenant""Id" DESC, "select" DESC');
});
});
describe("buildOpenTableDefaultSort", () => {
it("keeps metadata order for all primary-key columns while using the first key as the grid indicator", () => {
expect(
buildOpenTableDefaultSort({
mode: "primary-key-desc",
databaseType: "postgres",
primaryKeys: ["tenant_id", "record_id"],
columns: ["record_id", "tenant_id", "value"],
}),
).toEqual({
column: "tenant_id",
columnIndex: 1,
direction: "desc",
orderBy: '"tenant_id" DESC, "record_id" DESC',
});
});
});