From 7e7579e1bafcbee07751562abc7ae64386978cff Mon Sep 17 00:00:00 2001 From: CommitSyncScript Date: Fri, 7 Jun 2013 22:04:53 -0700 Subject: [PATCH] Assign the same keys if it's a single nested array or not If you specify a single array, we didn't prefix the keys with 0. If you later add children, the first array won't have the same key. --- src/core/ReactComponent.js | 2 +- src/core/__tests__/ReactIdentity-test.js | 19 +++++++++++-------- src/utils/__tests__/mapChildren-test.js | 8 +++++--- 3 files changed, 17 insertions(+), 12 deletions(-) diff --git a/src/core/ReactComponent.js b/src/core/ReactComponent.js index 85bf82115f..4b0fad589b 100644 --- a/src/core/ReactComponent.js +++ b/src/core/ReactComponent.js @@ -87,7 +87,7 @@ function tryToReuseArray(children) { for (var i = 0; i < children.length; i++) { var child = children[i]; if (isEmptyChild(child)) return false; - assignKey('', child, i); + assignKey('0', child, i); } return true; } diff --git a/src/core/__tests__/ReactIdentity-test.js b/src/core/__tests__/ReactIdentity-test.js index 71614082b9..36dffd8391 100644 --- a/src/core/__tests__/ReactIdentity-test.js +++ b/src/core/__tests__/ReactIdentity-test.js @@ -67,8 +67,8 @@ describe('ReactIdentity', function() { React.renderComponent(instance, document.createElement('div')); var node = instance.getDOMNode(); reactComponentExpect(instance).toBeDOMComponentWithChildCount(2); - checkId(node.childNodes[0], '.reactRoot[0].[apple]'); - checkId(node.childNodes[1], '.reactRoot[0].[banana]'); + checkId(node.childNodes[0], '.reactRoot[0].[0:apple]'); + checkId(node.childNodes[1], '.reactRoot[0].[0:banana]'); }); it('should use instance identity', function() { @@ -89,12 +89,15 @@ describe('ReactIdentity', function() { React.renderComponent(instance, document.createElement('div')); var node = instance.getDOMNode(); reactComponentExpect(instance).toBeDOMComponentWithChildCount(3); - checkId(node.childNodes[0], '.reactRoot[0].[wrap1]'); - checkId(node.childNodes[0].firstChild, '.reactRoot[0].[wrap1].[squirrel]'); - checkId(node.childNodes[1], '.reactRoot[0].[wrap2]'); - checkId(node.childNodes[1].firstChild, '.reactRoot[0].[wrap2].[bunny]'); - checkId(node.childNodes[2], '.reactRoot[0].[2]'); - checkId(node.childNodes[2].firstChild, '.reactRoot[0].[2].[chipmunk]'); + checkId(node.childNodes[0], '.reactRoot[0].[0:wrap1]'); + checkId( + node.childNodes[0].firstChild, + '.reactRoot[0].[0:wrap1].[0:squirrel]' + ); + checkId(node.childNodes[1], '.reactRoot[0].[0:wrap2]'); + checkId(node.childNodes[1].firstChild, '.reactRoot[0].[0:wrap2].[0:bunny]'); + checkId(node.childNodes[2], '.reactRoot[0].[0:2]'); + checkId(node.childNodes[2].firstChild, '.reactRoot[0].[0:2].[0:chipmunk]'); }); it('should let restructured components retain their uniqueness', function() { diff --git a/src/utils/__tests__/mapChildren-test.js b/src/utils/__tests__/mapChildren-test.js index 32ac55bdb6..21abf64110 100644 --- a/src/utils/__tests__/mapChildren-test.js +++ b/src/utils/__tests__/mapChildren-test.js @@ -75,7 +75,7 @@ describe('mapChildren', function() { expect(mapFn).toHaveBeenCalledWith(simpleKid, 'simple', 0); expect(rendered.props.children[0]).toBe(simpleKid); - expect(rendered.props.children).toHaveKeys(['simple']); + expect(rendered.props.children).toHaveKeys(['0:simple']); }); it('should pass key to returned component', function() { @@ -94,7 +94,7 @@ describe('mapChildren', function() { expect(rendered.props.children[0]).not.toBe(simpleKid); expect(rendered.props.children[0].props.children[0]).toBe(simpleKid); - expect(rendered.props.children).toHaveKeys(['simple']); + expect(rendered.props.children).toHaveKeys(['0:simple']); expect(rendered.props.children[0].props.children).toHaveKeys(['simple']); }); @@ -126,6 +126,8 @@ describe('mapChildren', function() { expect(mapFn).toHaveBeenCalledWith(kidTwo, '0:two', 1); expect(mapFn).toHaveBeenCalledWith(kidThree, '0:three', 2); expect(rendered.props.children).not.toEqual(instance.props.children); - expect(rendered.props.children).toHaveKeys(['0:one', '0:two', '0:three']); + expect(rendered.props.children).toHaveKeys([ + '0:0:one', '0:0:two', '0:0:three' + ]); }); });