Skip to content

Commit

Permalink
Update concatChildren to accomodate multiple children
Browse files Browse the repository at this point in the history
  • Loading branch information
youknowriad committed Apr 27, 2017
1 parent 129ffd7 commit 1b360dc
Showing 1 changed file with 15 additions and 26 deletions.
41 changes: 15 additions & 26 deletions element/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import { createElement, Component, cloneElement, Children } from 'react';
import { render } from 'react-dom';
import { renderToStaticMarkup } from 'react-dom/server';
import { isString } from 'lodash';

/**
* Returns a new element of given type. Type can be either a string tag name or
Expand Down Expand Up @@ -68,33 +67,23 @@ export function renderToString( element ) {
}

/**
* Concat two React children objects
* Concatenate two or more React children objects
*
* @param {?Object} children1 First children value
* @param {?Object} children2 Second children value
*
* @return {Array} The concatenation
* @param {...?Object} childrens Set of children to concatenate
* @return {Array} The concatenated value
*/
export function concatChildren( children1, children2 ) {
const toArray = ( children ) => {
if ( ! children ) {
return [];
}

return Array.isArray( children ) ? Children.toArray( children ) : [ children ];
};
export function concatChildren( ...childrens ) {
return childrens.reduce( ( memo, children, i ) => {
Children.forEach( children, ( child, j ) => {
if ( 'string' !== typeof child ) {
child = cloneElement( child, {
key: [ i, j ].join()
} );
}

return [
...toArray( children1 ),
...toArray( children2 )
].map( ( elt, index ) => {
if ( isString( elt ) ) {
return elt;
}
memo.push( child );
} );

return {
...elt,
key: index
};
} );
return memo;
}, [] );
}

0 comments on commit 1b360dc

Please sign in to comment.