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

Preserve order in mergeChildMappings #6149

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
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
9 changes: 5 additions & 4 deletions src/addons/transitions/ReactTransitionChildMapping.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ var ReactTransitionChildMapping = {

var i;
var childMapping = {};

for (i = 0; i < pendingKeys.length; i++) {
childMapping[pendingKeys[i]] = getValueForKey(pendingKeys[i]);
}

for (var nextKey in next) {
if (nextKeysPending.hasOwnProperty(nextKey)) {
for (i = 0; i < nextKeysPending[nextKey].length; i++) {
Expand All @@ -87,10 +92,6 @@ var ReactTransitionChildMapping = {
childMapping[nextKey] = getValueForKey(nextKey);
}

// Finally, add the keys which didn't appear before any key in `next`
for (i = 0; i < pendingKeys.length; i++) {
childMapping[pendingKeys[i]] = getValueForKey(pendingKeys[i]);
}

return childMapping;
},
Expand Down
12 changes: 6 additions & 6 deletions src/addons/transitions/__tests__/ReactCSSTransitionGroup-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@ describe('ReactCSSTransitionGroup', function() {
container
);
expect(ReactDOM.findDOMNode(a).childNodes.length).toBe(2);
expect(ReactDOM.findDOMNode(a).childNodes[0].id).toBe('two');
expect(ReactDOM.findDOMNode(a).childNodes[1].id).toBe('one');
expect(ReactDOM.findDOMNode(a).childNodes[0].id).toBe('one');
expect(ReactDOM.findDOMNode(a).childNodes[1].id).toBe('two');

// For some reason jst is adding extra setTimeout()s and grunt test isn't,
// so we need to do this disgusting hack.
Expand Down Expand Up @@ -125,8 +125,8 @@ describe('ReactCSSTransitionGroup', function() {
container
);
expect(ReactDOM.findDOMNode(a).childNodes.length).toBe(2);
expect(ReactDOM.findDOMNode(a).childNodes[0].id).toBe('two');
expect(ReactDOM.findDOMNode(a).childNodes[1].id).toBe('one');
expect(ReactDOM.findDOMNode(a).childNodes[0].id).toBe('one');
expect(ReactDOM.findDOMNode(a).childNodes[1].id).toBe('two');
});

it('should switch transitionLeave from false to true', function() {
Expand Down Expand Up @@ -160,8 +160,8 @@ describe('ReactCSSTransitionGroup', function() {
container
);
expect(ReactDOM.findDOMNode(a).childNodes.length).toBe(2);
expect(ReactDOM.findDOMNode(a).childNodes[0].id).toBe('three');
expect(ReactDOM.findDOMNode(a).childNodes[1].id).toBe('two');
expect(ReactDOM.findDOMNode(a).childNodes[0].id).toBe('two');
expect(ReactDOM.findDOMNode(a).childNodes[1].id).toBe('three');
});

it('should work with no children', function() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,51 @@ describe('ReactTransitionChildMapping', function() {
});
});

it('should preserve order when overlapping insertions and deletions are iterated on', function() {
var prev = {
one: true,
two: true,
four: true,
five: true,
};
var next = {
one: true,
two: true,
three: true,
five: true,
};
var childMappingsKeys = Object.keys(ReactTransitionChildMapping.mergeChildMappings(prev, next));
expect(childMappingsKeys).toEqual([
'one',
'two',
'three',
'four',
'five',
]);
});

it('should preserve order when non-overlapping insertions are iterated on', function() {
var prev = {
one: true,
two: true,
three: true,
};
var next = {
four: true,
five: true,
six: true,
};
var childMappingsKeys = Object.keys(ReactTransitionChildMapping.mergeChildMappings(prev, next));
expect(childMappingsKeys).toEqual([
'one',
'two',
'three',
'four',
'five',
'six',
]);
});

it('should support mergeChildMappings with undefined input', function() {
var prev = {
one: true,
Expand Down