Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

graciously handle array shuffling #4413

Merged
merged 4 commits into from
Jun 25, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 19 additions & 5 deletions src/diff/children.js
Original file line number Diff line number Diff line change
Expand Up @@ -302,20 +302,34 @@ function constructNewChildrenArray(newParentVNode, renderResult, oldChildren) {
childVNode._flags |= INSERT_VNODE;
}
} else if (matchingIndex !== skewedIndex) {
if (matchingIndex === skewedIndex + 1) {
if (matchingIndex == skewedIndex - 1) {
skew = matchingIndex - skewedIndex;
} else if (matchingIndex == skewedIndex + 1) {
skew++;
} else if (matchingIndex > skewedIndex) {
// Our matched DOM-node is further in the list of children than
// where it's at now.

// When the remaining old children is bigger than the new-children
// minus our skewed index we know we are dealing with a shrinking list
// we have to increase our skew with the matchedIndex - the skewed index
if (remainingOldChildren > newChildrenLength - skewedIndex) {
skew += matchingIndex - skewedIndex;
} else {
// If we have matched all the children just decrease the skew
skew--;
}
} else if (matchingIndex < skewedIndex) {
if (matchingIndex == skewedIndex - 1) {
skew = matchingIndex - skewedIndex;
// Our matched DOM-node is further in the negative way in the list of children
// than where it's at now.

// When the remaining old chiildren is less than the new children
// plus our skewed index we know we are dealing with a growing list
if (remainingOldChildren < newChildrenLength + skewedIndex) {
skew += matchingIndex + skewedIndex;
} else {
skew = 0;
JoviDeCroock marked this conversation as resolved.
Show resolved Hide resolved
}
} else {
JoviDeCroock marked this conversation as resolved.
Show resolved Hide resolved
skew = 0;
}

// Move this VNode's DOM if the original index (matchingIndex) doesn't
Expand Down
33 changes: 30 additions & 3 deletions test/browser/render.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -474,17 +474,17 @@ describe('render()', () => {

it('should support popover auto', () => {
render(<div popover="auto" />, scratch);
expect(scratch.innerHTML).to.equal("<div popover=\"auto\"></div>");
expect(scratch.innerHTML).to.equal('<div popover="auto"></div>');
});

it('should support popover true boolean', () => {
render(<div popover />, scratch);
expect(scratch.innerHTML).to.equal("<div popover=\"\"></div>");
expect(scratch.innerHTML).to.equal('<div popover=""></div>');
});

it('should support popover false boolean', () => {
render(<div popover={false} />, scratch);
expect(scratch.innerHTML).to.equal("<div></div>");
expect(scratch.innerHTML).to.equal('<div></div>');
JoviDeCroock marked this conversation as resolved.
Show resolved Hide resolved
});

// Test for preactjs/preact#4340
Expand Down Expand Up @@ -1625,4 +1625,31 @@ describe('render()', () => {
'<div><div>One</div><div>Six</div><div>Seven</div></div>'
);
});

it('shuffle', function () {
JoviDeCroock marked this conversation as resolved.
Show resolved Hide resolved
const App = ({ items }) => (
<div>
{items.map(key => (
<div key={key}>{key}</div>
))}
</div>
);
const a = ['0', '1', '2', '3', '4', '5', '6'];
const b = ['1', '3', '5', '2', '6', '4', '0'];
render(<App items={a} />, scratch);
expect(scratch.innerHTML).to.equal(
`<div>${a.map(n => `<div>${n}</div>`).join('')}</div>`
);

render(<App items={b} />, scratch);

JoviDeCroock marked this conversation as resolved.
Show resolved Hide resolved
expect(scratch.innerHTML).to.equal(
`<div>${b.map(n => `<div>${n}</div>`).join('')}</div>`
);

render(<App items={a} />, scratch);
expect(scratch.innerHTML).to.equal(
`<div>${a.map(n => `<div>${n}</div>`).join('')}</div>`
);
});
});
Loading