Skip to content

Commit

Permalink
fix: fix sync element (#551)
Browse files Browse the repository at this point in the history
* fix: fix sync element

* chore: bump @egjs/grid
  • Loading branch information
daybrush committed Aug 4, 2023
1 parent e63f1e8 commit f663673
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 12 deletions.
2 changes: 1 addition & 1 deletion packages/infinitegrid/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@
"@cfcs/core": "^0.0.5",
"@egjs/children-differ": "^1.0.1",
"@egjs/component": "^3.0.0",
"@egjs/grid": "^1.14.1",
"@egjs/grid": "^1.14.2",
"@egjs/list-differ": "^1.0.0"
}
}
6 changes: 6 additions & 0 deletions packages/infinitegrid/src/GroupManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -644,6 +644,9 @@ export class GroupManager extends Grid<GroupManagerOptions> {
if (info.groupKey != null) {
prevItem.groupKey = info.groupKey!;
}
if (info.element) {
prevItem.element = info.element;
}
nextItemKeys[key] = prevItem;
}
});
Expand All @@ -664,6 +667,9 @@ export class GroupManager extends Grid<GroupManagerOptions> {
if (info.data) {
item.data = info.data;
}
if (info.element) {
item.element = info.element;
}
} else {
item = new InfiniteGridItem(horizontal, {
...info,
Expand Down
16 changes: 9 additions & 7 deletions packages/infinitegrid/src/InfiniteGrid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -693,12 +693,20 @@ class InfiniteGrid<Options extends InfiniteGridOptions = InfiniteGridOptions> ex
this.setCursors(e.nextStartCursor, e.nextEndCursor);
}
private _onRendererUpdated = (e: OnRendererUpdated<GridRendererItem>): void => {
const renderedItems = e.items;

renderedItems.forEach((item) => {
// set grid element
const gridItem = item.orgItem;

gridItem.element = item.element as HTMLElement;
});

if (!e.isChanged) {
this._checkEndLoading();
this._scroll();
return;
}
const renderedItems = e.items;

const {
added,
Expand All @@ -715,12 +723,6 @@ class InfiniteGrid<Options extends InfiniteGridOptions = InfiniteGridOptions> ex
}
});

renderedItems.forEach((item) => {
// set grid element
const gridItem = item.orgItem;

gridItem.element = item.element as HTMLElement;
});

const horizontal = this.options.horizontal;
const addedItems = added.map((index) => {
Expand Down
49 changes: 49 additions & 0 deletions packages/infinitegrid/test/unit/cfcs.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,55 @@ describe("test cfcs", () => {
expect(item.element).to.be.equals(item2.element);
});
});
it("should check if the element is updated even if the key is the same", async () => {
// Given
const firstItems = [0, 1, 2, 3, 4, 5].map((i) => {
return {
key: i,
element: document.createElement("div"),
};
});
container!.innerHTML = `
<div class="wrapper" style="width: 100%; height: 500px;"></div>
`;
const wrapper = container!.querySelector<HTMLElement>(".wrapper")!;
ig = new InfiniteGrid<InfiniteGridOptions>(wrapper, {
gridConstructor: SampleGrid,
container: true,
});

// first render
ig.syncItems(firstItems);

await waitEvent(ig!, "renderComplete");

// When
// second render with same key
const nextItems = [0, 1, 2, 3, 4, 5].map((i) => {
return {
key: i,
element: document.createElement("li"),
};
});
ig.getContainerElement().innerHTML = "";

nextItems.forEach((item) => {
ig!.getContainerElement().appendChild(item.element);
});
ig.syncItems(nextItems);
ig.renderItems();

await waitEvent(ig!, "renderComplete");

const renderingItems2 = ig.getRenderingItems();

// Then
nextItems.forEach((item, i) => {
const item2 = renderingItems2[i];

expect(item.element).to.be.equals(item2.element);
});
});
it("should check if rendering items exists with placeholders", async () => {
// Given
container!.innerHTML = `
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2495,10 +2495,10 @@
"@egjs/imready" "^1.3.1"
"@egjs/list-differ" "^1.0.1"

"@egjs/grid@^1.14.1":
version "1.14.1"
resolved "https://registry.npmjs.org/@egjs/grid/-/grid-1.14.1.tgz#83ca3f16b86b6f89ec33dd424d8341831e210792"
integrity sha512-xICx7COEqVdcfsEYnh22otobqOBqb/69NooDF6W4Tg35SS3Tans9yrgv0a4J6/dEykVcH6kdDEy53gPxl9wIUQ==
"@egjs/grid@^1.14.2":
version "1.14.2"
resolved "https://registry.npmjs.org/@egjs/grid/-/grid-1.14.2.tgz#b92c91298a808c7549219904ed2396361c4cc1b8"
integrity sha512-MOIL6zdFmEFYVmlsrPr8eNPybDZqRKzJo1sceFiamAvxWJ+5yG17tVRP9zMSeXogjsShWhNUKqoPOUC4rClFAQ==
dependencies:
"@egjs/children-differ" "^1.0.1"
"@egjs/component" "^3.0.0"
Expand Down

0 comments on commit f663673

Please sign in to comment.