fix(grid): preserve null text during editing

This commit is contained in:
zhangsan 2026-07-20 01:00:01 +08:00 committed by GitHub
parent 834ad772c6
commit f8baedc6e6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 37 additions and 1 deletions

View File

@ -34,6 +34,26 @@ describe("dataGridCellDisplayText", () => {
});
describe("coerceDataGridCellValue", () => {
it.each(["null", "NULL", "Null", "nUlL"])("preserves literal %s input as text", (value) => {
expect(
coerceDataGridCellValue({
value,
oldValue: null,
databaseType: "mysql",
columnInfo: { data_type: "varchar(255)" },
}),
).toBe(value);
expect(
coerceDataGridCellValue({
value,
oldValue: "previous",
databaseType: "postgres",
columnInfo: { data_type: "text" },
}),
).toBe(value);
});
it("preserves an explicitly generated empty string for a null cell", () => {
const options = {
value: "",

View File

@ -11,7 +11,6 @@ export interface CoerceDataGridCellValueOptions {
export function coerceDataGridCellValue(options: CoerceDataGridCellValueOptions): GridCellValue {
const { value, oldValue } = options;
if (value.toUpperCase() === "NULL") return null;
if (value === "" && oldValue === null && !options.preserveEmptyString) return null;
const postgresArrayValue = coercePostgresArrayValue(options);
if (postgresArrayValue !== undefined) return postgresArrayValue;

View File

@ -618,6 +618,23 @@ test("undo and redo restore pending cell edits before save", () => {
assert.deepEqual(editor.rowDataWithChanges(result.value.rows[0], 0), [1, "Ada Lovelace"]);
});
test("typing NULL preserves the literal string value", async () => {
setActivePinia(createPinia());
installBrowserTestGlobals();
const result = computed(() => ({
columns: ["id", "name"],
rows: [[1, "Ada"] as CellValue[]],
}));
const editor = createPeopleGridEditor(result);
editor.applyCellValue(0, 1, "NULL");
assert.equal(editor.dirtyRows.value.get(0)?.get(1), "NULL");
assert.deepEqual(editor.rowDataWithChanges(result.value.rows[0], 0), [1, "NULL"]);
assert.deepEqual(await editor.previewChanges(), [`UPDATE "people" SET "name" = 'NULL' WHERE "id" = 1;`]);
});
test("setting a cell to NULL records pending SQL and supports undo and redo", async () => {
setActivePinia(createPinia());
installBrowserTestGlobals();