fix(grid): stop filter autocomplete inserting column names
This commit is contained in:
3 files changed
+193
-44
No files matched your search
+106
-27
@@ -21,8 +21,8 @@ describe("useDataGridConditionEditor", () => {
|
||||
vi.useRealTimers();
|
||||
});
|
||||
|
||||
it("replaces the active token and supports clamped keyboard navigation", async () => {
|
||||
const value = ref("status = cus");
|
||||
it("replaces the active field token and supports clamped keyboard navigation", async () => {
|
||||
const value = ref("status = 1 AND cus");
|
||||
const editor = useDataGridConditionEditor({
|
||||
kind: "where",
|
||||
value,
|
||||
@@ -30,16 +30,94 @@ describe("useDataGridConditionEditor", () => {
|
||||
historyScope: {},
|
||||
});
|
||||
|
||||
value.value = "status = cust";
|
||||
value.value = "status = 1 AND cust";
|
||||
await nextTick();
|
||||
await vi.waitFor(() => expect(editor.suggestions.value.map((item) => item.value)).toEqual(["customer_id", "customer_name"]));
|
||||
// 不再默认高亮第一条建议:必须显式导航/接受,避免回车应用筛选时改写输入(issue #10595)
|
||||
expect(editor.highlightedIndex.value).toBe(-1);
|
||||
expect(editor.navigate(1)).toBe(true);
|
||||
expect(editor.highlightedIndex.value).toBe(0);
|
||||
expect(editor.navigate(1)).toBe(true);
|
||||
expect(editor.highlightedIndex.value).toBe(1);
|
||||
expect(editor.navigate(1)).toBe(true);
|
||||
expect(editor.highlightedIndex.value).toBe(1);
|
||||
expect(editor.accept()).toBe(true);
|
||||
expect(value.value).toBe("status = customer_name");
|
||||
expect(value.value).toBe("status = 1 AND customer_name");
|
||||
});
|
||||
|
||||
it("does not suggest column names while typing a value after a comparison operator", async () => {
|
||||
const value = ref("");
|
||||
const editor = useDataGridConditionEditor({
|
||||
kind: "where",
|
||||
value,
|
||||
columns: ["v_id", "v_name"],
|
||||
historyScope: {},
|
||||
suggestionDebounceMs: 1,
|
||||
});
|
||||
|
||||
// 用户场景:`v_id = v` 时正在输入值,不应该看到列名补全(issue #10595)
|
||||
value.value = "v_id = v";
|
||||
await nextTick();
|
||||
await new Promise((resolve) => setTimeout(resolve, 5));
|
||||
expect(editor.suggestions.value).toEqual([]);
|
||||
|
||||
value.value = "v_id <> v";
|
||||
await nextTick();
|
||||
await new Promise((resolve) => setTimeout(resolve, 5));
|
||||
expect(editor.suggestions.value).toEqual([]);
|
||||
|
||||
// AND 之后是字段位置,仍然提示列名
|
||||
value.value = "v_id = 'x' AND v";
|
||||
await nextTick();
|
||||
await vi.waitFor(() => expect(editor.suggestions.value.map((item) => item.value)).toEqual(["v_id", "v_name"]));
|
||||
});
|
||||
|
||||
it("does not suggest column names inside an IN value list", async () => {
|
||||
const value = ref("");
|
||||
const editor = useDataGridConditionEditor({
|
||||
kind: "where",
|
||||
value,
|
||||
columns: ["mi_id", "model_name"],
|
||||
historyScope: {},
|
||||
suggestionDebounceMs: 1,
|
||||
});
|
||||
|
||||
// 用户场景:`v_id = v AND mi_id in (v` —— 正在输入 IN 列表里的值(issue #10595)
|
||||
value.value = "v_id = v AND mi_id in (v";
|
||||
await nextTick();
|
||||
await new Promise((resolve) => setTimeout(resolve, 5));
|
||||
expect(editor.suggestions.value).toEqual([]);
|
||||
|
||||
// 列表里的第二个值同样不应提示列名
|
||||
value.value = "mi_id in ('a', m";
|
||||
await nextTick();
|
||||
await new Promise((resolve) => setTimeout(resolve, 5));
|
||||
expect(editor.suggestions.value).toEqual([]);
|
||||
|
||||
// 括号闭合后的新条件仍应提示列名
|
||||
value.value = "mi_id in (1, 2) AND m";
|
||||
await nextTick();
|
||||
await vi.waitFor(() => expect(editor.suggestions.value.map((item) => item.value)).toEqual(["mi_id", "model_name"]));
|
||||
|
||||
// 裸括号(表达式开头)仍属于字段位置
|
||||
value.value = "(m";
|
||||
await nextTick();
|
||||
await vi.waitFor(() => expect(editor.suggestions.value.map((item) => item.value)).toEqual(["mi_id", "model_name"]));
|
||||
});
|
||||
|
||||
it("only suggests sort direction after an ORDER BY column", async () => {
|
||||
const value = ref("");
|
||||
const editor = useDataGridConditionEditor({ kind: "orderBy", value, columns: ["create_time", "create_by"], historyScope: {}, suggestionDebounceMs: 1 });
|
||||
|
||||
// 输入 `asc` 时不应提示列名(否则按 Tab 会把列名写进输入框)
|
||||
value.value = "create_time a";
|
||||
await nextTick();
|
||||
await vi.waitFor(() => expect(editor.suggestions.value).toEqual([{ value: "ASC", kind: "keyword" }]));
|
||||
|
||||
// 逗号之后是新的排序列位置,提示列名
|
||||
value.value = "create_time ASC, c";
|
||||
await nextTick();
|
||||
await vi.waitFor(() => expect(editor.suggestions.value.map((item) => item.value)).toEqual(["create_time", "create_by"]));
|
||||
});
|
||||
|
||||
it.each(["where", "orderBy"] as const)("searches %s fields by camel-case initials and any-position text", async (kind) => {
|
||||
@@ -108,16 +186,16 @@ describe("useDataGridConditionEditor", () => {
|
||||
historyScope: {},
|
||||
});
|
||||
|
||||
value.value = "status = cus AND enabled = 1";
|
||||
selectionStart.value = 12;
|
||||
selectionEnd.value = 12;
|
||||
value.value = "status = 1 AND cus AND enabled = 2";
|
||||
selectionStart.value = 18;
|
||||
selectionEnd.value = 18;
|
||||
await nextTick();
|
||||
await vi.waitFor(() => expect(editor.suggestions.value.map((item) => item.value)).toEqual(["customer_id", "customer_name"]));
|
||||
|
||||
expect(editor.accept(0)).toBe(true);
|
||||
expect(value.value).toBe("status = customer_id AND enabled = 1");
|
||||
expect(selectionStart.value).toBe(20);
|
||||
expect(selectionEnd.value).toBe(20);
|
||||
expect(value.value).toBe("status = 1 AND customer_id AND enabled = 2");
|
||||
expect(selectionStart.value).toBe(26);
|
||||
expect(selectionEnd.value).toBe(26);
|
||||
});
|
||||
|
||||
it("uses the current selection as an explicit replacement range", async () => {
|
||||
@@ -126,14 +204,14 @@ describe("useDataGridConditionEditor", () => {
|
||||
const selectionEnd = ref(0);
|
||||
const editor = useDataGridConditionEditor({ kind: "where", value, selectionStart, selectionEnd, columns: ["old_value"], historyScope: {} });
|
||||
|
||||
value.value = "status = old AND enabled = 1";
|
||||
selectionStart.value = 9;
|
||||
selectionEnd.value = 12;
|
||||
value.value = "old = 1";
|
||||
selectionStart.value = 0;
|
||||
selectionEnd.value = 3;
|
||||
await nextTick();
|
||||
await vi.waitFor(() => expect(editor.suggestions.value.map((item) => item.value)).toEqual(["old_value"]));
|
||||
|
||||
expect(editor.accept()).toBe(true);
|
||||
expect(value.value).toBe("status = old_value AND enabled = 1");
|
||||
expect(value.value).toBe("old_value = 1");
|
||||
});
|
||||
|
||||
it("suggests WHERE connectors after a completed expression", async () => {
|
||||
@@ -168,7 +246,7 @@ describe("useDataGridConditionEditor", () => {
|
||||
expect(value.value).toBe("status = 'active' AND owner_id");
|
||||
});
|
||||
|
||||
it("does not offer connectors inside quoted values or in ORDER BY", async () => {
|
||||
it("does not offer connectors inside quoted values, and offers only sort direction after an ORDER BY column", async () => {
|
||||
const whereValue = ref("");
|
||||
const whereEditor = useDataGridConditionEditor({ kind: "where", value: whereValue, columns: ["name"], historyScope: {} });
|
||||
whereValue.value = "name = 'Alice a";
|
||||
@@ -179,7 +257,8 @@ describe("useDataGridConditionEditor", () => {
|
||||
const orderByEditor = useDataGridConditionEditor({ kind: "orderBy", value: orderByValue, columns: ["amount"], historyScope: {} });
|
||||
orderByValue.value = "created_at a";
|
||||
await nextTick();
|
||||
await vi.waitFor(() => expect(orderByEditor.suggestions.value).toEqual([]));
|
||||
// ORDER BY 的排序列之后是排序方向位置:只提示 ASC/DESC,绝不插入列名(issue #10595)
|
||||
await vi.waitFor(() => expect(orderByEditor.suggestions.value).toEqual([{ value: "ASC", kind: "keyword" }]));
|
||||
});
|
||||
|
||||
it.each(["deleted_at IS ", "deleted_at IS a", "deleted_at IS NOT o", "name LIKE o", "id IN a", "score BETWEEN o"])("does not offer connectors while the keyword operator is incomplete: %s", async (condition) => {
|
||||
@@ -282,12 +361,12 @@ describe("useDataGridConditionEditor", () => {
|
||||
historyScope: {},
|
||||
});
|
||||
|
||||
value.value = kind === "where" ? "status = Order" : "created_at DESC, Order";
|
||||
value.value = kind === "where" ? "status = 1 AND Order" : "created_at DESC, Order";
|
||||
await nextTick();
|
||||
await vi.waitFor(() => expect(editor.suggestions.value).toEqual([{ value: "OrderId", insertText: '"OrderId"', kind: "column", comment: "Mixed-case identifier" }]));
|
||||
|
||||
expect(editor.accept()).toBe(true);
|
||||
expect(value.value).toBe(kind === "where" ? 'status = "OrderId"' : 'created_at DESC, "OrderId"');
|
||||
expect(value.value).toBe(kind === "where" ? 'status = 1 AND "OrderId"' : 'created_at DESC, "OrderId"');
|
||||
});
|
||||
|
||||
it("restores quoted history verbatim instead of quoting it again", () => {
|
||||
@@ -371,21 +450,21 @@ describe("useDataGridConditionEditor", () => {
|
||||
const suggestionProvider = vi.fn(() => ["customer_id"]);
|
||||
useDataGridConditionEditor({ kind: "where", value, selectionStart, selectionEnd, historyScope: {}, suggestionProvider });
|
||||
|
||||
value.value = "status = cus AND enabled = 1";
|
||||
selectionStart.value = 12;
|
||||
selectionEnd.value = 12;
|
||||
value.value = "status = 1 AND cus";
|
||||
selectionStart.value = 18;
|
||||
selectionEnd.value = 18;
|
||||
await nextTick();
|
||||
await vi.waitFor(() => expect(suggestionProvider).toHaveBeenCalledOnce());
|
||||
|
||||
expect(suggestionProvider).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
value: "status = cus AND enabled = 1",
|
||||
valueBeforeCursor: "status = cus",
|
||||
value: "status = 1 AND cus",
|
||||
valueBeforeCursor: "status = 1 AND cus",
|
||||
token: "cus",
|
||||
from: 9,
|
||||
to: 12,
|
||||
selectionStart: 12,
|
||||
selectionEnd: 12,
|
||||
from: 15,
|
||||
to: 18,
|
||||
selectionStart: 18,
|
||||
selectionEnd: 18,
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
Reference in new issue
Block a user