import React, { Component, PropTypes } from 'react';
import { columnsConfig as columnsConfigPropType, globalConfig as globalConfigPropType } from './proptypes';

const GridBody = ({config, objects, idColumn, columns, columnKeys, children})=>{
    const {row: Row, cell: Cell, cellRendererBase: CellRenderer} = config;
    const rows = objects.map((current) => {
        const cells = columnKeys.map((key) => {
            let configForColumn = columns[key];
            let value = current[key];
            let content = configForColumn && configForColumn.content ? configForColumn.content : undefined;
            const args = {columns:columns, columnName:key, config:config, value:value, object:current};
            if (content && typeof content === "function"){
                if (content.prototype  instanceof Component){
                    content = React.createElement(content, args);
                }else {
                    content = content(args);
                }
            } else {
                content = <CellRenderer {...args} />;
            }
            return (
                <Cell key={key} content={content} />
            );
        });

        return(
            <Row key={current[idColumn]} columns={columns} config={config} object={current}>
                {cells}
            </Row>
        );
    });
    return(
        <tbody>
            {rows}
            {children}
        </tbody>
    );
};

if (process.env.NODE_ENV !== 'production'){
    GridBody.propTypes = {
        config : globalConfigPropType,
        objects: PropTypes.arrayOf(PropTypes.object),
        columnKeys: PropTypes.arrayOf(PropTypes.string).isRequired,
        columns: columnsConfigPropType,
        children: PropTypes.any,
        idColumn: PropTypes.string.isRequired
    };
}

export default GridBody;

@@ -597,16 +597,57 @@
 efined;%0A
+            let contentToRender = value;%0A
         
@@ -770,25 +770,89 @@
 ent 
-&& typeof content
+!== undefined)%7B%0A              const type = typeof content;%0A              if (type
  ===
@@ -903,17 +903,16 @@
 ototype 
- 
 instance
@@ -944,25 +944,31 @@
         
-  
 content
+ToRender
  = React
@@ -1015,16 +1015,17 @@
        %7D
+ 
 else %7B%0A 
@@ -1041,25 +1041,31 @@
         
-  
 content
+ToRender
  = conte
@@ -1096,32 +1096,34 @@
   %7D%0A            
+  
 %7D else %7B%0A       
@@ -1138,16 +1138,469 @@
  content
+ToRender = content;%0A              %7D%0A            %7D%0A            if (contentToRender !== undefined && !React.isValidElement(contentToRender))%7B%0A              switch (typeof contentToRender)%7B%0A                case 'string':%0A                  break;%0A                case 'number':%0A                case 'boolean':%0A                  contentToRender = contentToRender.toString();%0A                  break;%0A                default:%0A                  contentToRender
  = %3CCell
@@ -1622,16 +1622,32 @@
 gs%7D /%3E;%0A
+              %7D%0A
         
@@ -1721,16 +1721,24 @@
 %7Bcontent
+ToRender
 %7D /%3E%0A   

improve content handling