Skip to content

Commit

Permalink
fix: fix issue with reordering list inside lists
Browse files Browse the repository at this point in the history
The issue was that moveBefore was not implemented for lists, which means
that list of lists did not behave as expected.

closes ged-odoo#3
  • Loading branch information
ged-odoo committed Aug 29, 2021
1 parent 22e8325 commit f4907bb
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,15 @@ class VList {
}

moveBefore(other: VList | null, afterNode: Node | null) {
// todo
if (other) {
const next = other!.children[0];
afterNode = (next ? next.firstNode() : other!.anchor) || null;
}
const children = this.children;
for (let i = 0, l = children.length; i < l; i++) {
children[i].moveBefore(null, afterNode);
}
this.parentEl!.insertBefore(this.anchor!, afterNode);
}

patch(other: VList, withBeforeRemove: boolean) {
Expand Down
29 changes: 29 additions & 0 deletions tests/list.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,35 @@ describe("list node: misc", () => {
patch(tree, block([], [list([1, 2, 3].map(n))]));
expect(fixture.innerHTML).toBe("<div>123</div>");
});

test("list of lists", async () => {
const tree = list([
withKey(list([kText("a1", "1"), kText("a2", "2")]), "a"),
withKey(list([kText("b1", "1"), kText("b2", "2")]), "b"),
]);
mount(tree, fixture);
expect(fixture.innerHTML).toBe("a1a2b1b2");

patch(
tree,
list([
withKey(list([kText("b1", "1"), kText("b2", "2")]), "b"),
withKey(list([kText("a1", "1"), kText("a2", "2")]), "a"),
])
);

expect(fixture.innerHTML).toBe("b1b2a1a2");

patch(
tree,
list([
withKey(list([kText("a2", "2"), kText("a1", "1")]), "a"),
withKey(list([kText("b2", "2"), kText("b1", "1")]), "b"),
])
);

expect(fixture.innerHTML).toBe("a2a1b2b1");
});
});

describe("adding/removing elements", () => {
Expand Down

0 comments on commit f4907bb

Please sign in to comment.