Skip to content
This repository has been archived by the owner on Nov 3, 2021. It is now read-only.

Commit

Permalink
Fix loop to avoid duplicated keys
Browse files Browse the repository at this point in the history
  • Loading branch information
ockham committed Nov 10, 2017
1 parent a8fd8e2 commit fda5d2d
Showing 1 changed file with 10 additions and 17 deletions.
27 changes: 10 additions & 17 deletions src/index.es6
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ function getCloseIndex( openIndex, tokens ) {

function buildChildren( tokens, components ) {
var children = [],
openComponent, clonedOpenComponent, openIndex, closeIndex, token, i, grandChildTokens, grandChildren, siblingTokens, siblings;
clonedOpenComponent, openIndex, closeIndex, token, i, grandChildTokens, grandChildren, siblingTokens, siblings;

for ( i = 0; i < tokens.length; i++ ) {
token = tokens[ i ];
Expand All @@ -56,9 +56,16 @@ function buildChildren( tokens, components ) {
throw new Error( 'Missing opening component token: `' + token.value + '`' );
}
if ( token.type === 'componentOpen' ) {
openComponent = components[ token.value ];
openIndex = i;
break;

closeIndex = getCloseIndex( openIndex, tokens );
grandChildTokens = tokens.slice( ( openIndex + 1 ), closeIndex );
grandChildren = buildChildren( grandChildTokens, components );

clonedOpenComponent = React.cloneElement( components[ token.value ], { key: openIndex }, grandChildren );
children.push( clonedOpenComponent );
i = closeIndex;
continue;
}
// componentSelfClosing token
if ( components[ token.value ] ) {
Expand All @@ -67,20 +74,6 @@ function buildChildren( tokens, components ) {
continue;
}

if ( openComponent ) {
closeIndex = getCloseIndex( openIndex, tokens );
grandChildTokens = tokens.slice( ( openIndex + 1 ), closeIndex );
grandChildren = buildChildren( grandChildTokens, components );
clonedOpenComponent = React.cloneElement( openComponent, { key: openIndex }, grandChildren );
children.push( clonedOpenComponent );

if ( closeIndex < tokens.length - 1 ) {
siblingTokens = tokens.slice( closeIndex + 1 );
siblings = buildChildren( siblingTokens, components );
children = children.concat( siblings );
}
}

if ( children.length === 1 ) {
return children[ 0 ];
}
Expand Down

0 comments on commit fda5d2d

Please sign in to comment.