fix(views/table): index column ends up in the wrong position

This commit is contained in:
Elian Doran
2025-07-16 09:16:47 +03:00
parent 9d85005255
commit 2244f0368f
2 changed files with 26 additions and 8 deletions

View File

@@ -92,5 +92,20 @@ describe("restoreExistingData", () => {
];
const restored = restoreExistingData(newDefs, oldDefs);
expect(restored.length).toBe(3);
})
});
it("doesn't alter the existing order", () => {
const newDefs: ColumnDefinition[] = [
{ title: "#", headerSort: false, hozAlign: "center", resizable: false, frozen: true, rowHandle: false },
{ field: "noteId", title: "Note ID", visible: false },
{ field: "title", title: "Title", editor: "input", width: 400 }
]
const oldDefs: ColumnDefinition[] = [
{ title: "#", headerSort: false, hozAlign: "center", resizable: false, rowHandle: false },
{ field: "noteId", title: "Note ID", visible: false },
{ field: "title", title: "Title", editor: "input", width: 400 }
];
const restored = restoreExistingData(newDefs, oldDefs);
expect(restored).toStrictEqual(newDefs);
});
});

View File

@@ -99,13 +99,16 @@ export function restoreExistingData(newDefs: ColumnDefinition[], oldDefs: Column
newDefs.map(def => [def.field!, def])
);
const existingColumns = oldDefs
.filter(item => item.field && newItemsByField.has(item.field!))
.map(item => {
return {
...newItemsByField.get(item.field!),
width: item.width,
visible: item.visible,
};
.filter(item => (item.field && newItemsByField.has(item.field!)) || item.title === "#")
.map(oldItem => {
const data = newItemsByField.get(oldItem.field!)!;
if (oldItem.width) {
data.width = oldItem.width;
}
if (oldItem.visible) {
data.visible = oldItem.visible;
}
return data;
}) as ColumnDefinition[];
// 2. Determine new columns.