/**
 * @file Tbody component
 * @author liangxiaojun(liangxiaojun@derbysoft.com)
 */

import React, {Component} from 'react';
import PropTypes from 'prop-types';

import TableRow from '../_TableRow';

import SelectMode from '../_statics/SelectMode';

import PureRender from '../_vendors/PureRender';
import Util from '../_vendors/Util';

@PureRender
class Tbody extends Component {

    static SelectMode = SelectMode;

    constructor(props, ...restArgs) {

        super(props, ...restArgs);

        this.isItemChecked = ::this.isItemChecked;

    }

    isItemChecked(rowData) {

        const {selectMode, idProp, value} = this.props;

        if (!rowData || !value) {
            return false;
        }

        switch (selectMode) {
            case SelectMode.MULTI_SELECT:
                return value.findIndex(item => item[idProp] === rowData[idProp]) !== -1;
            case SelectMode.SINGLE_SELECT:
                return value[idProp] === rowData[idProp];
        }

    }

    render() {

        const {
            columns, data, startIndex, idProp, disabled,
            onRowTouchTap, onCellTouchTap
        } = this.props;

        return (
            <tbody className="tbody">
                {
                    data.map((row, rowIndex) => row ?
                        <TableRow key={idProp && idProp in row ? row[idProp] : rowIndex}
                                  rowIndex={startIndex + rowIndex}
                                  columns={columns}
                                  data={row}
                                  isChecked={this.isItemChecked(row)}
                                  disabled={disabled || row.disabled}
                                  onRowTouchTap={onRowTouchTap}
                                  onCellTouchTap={onCellTouchTap}/>
                        :
                        null
                    )
                }
            </tbody>
        );

    }
};

process.env.NODE_ENV !== 'production' && (Tbody.propTypes = {

    columns: PropTypes.array,
    data: PropTypes.array,
    startIndex: PropTypes.number,
    idProp: PropTypes.string,
    selectMode: PropTypes.oneOf(Util.enumerateValue(SelectMode)),
    disabled: PropTypes.bool,

    isItemChecked: PropTypes.func,

    onRowTouchTap: PropTypes.func,
    onCellTouchTap: PropTypes.func

});

Tbody.defaultProps = {
    data: [],
    columns: [],
    startIndex: 0,
    idProp: 'id',
    selectMode: SelectMode.SINGLE_SELECT,
    disabled: false
};

export default Tbody;
@@ -122,44 +122,8 @@
 ct';
-%0Aimport PropTypes from 'prop-types';
 %0A%0Aim
@@ -1895,16 +1895,20 @@
   %7D%0A%7D;%0A%0A
+if (
 process.
@@ -1924,29 +1924,80 @@
 ENV 
-!
+=
 == '
-production' && (
+development') %7B%0A%0A    const PropTypes = require('prop-types');%0A%0A    
 Tbod
@@ -2013,24 +2013,28 @@
 es = %7B%0A%0A    
+  
+  
 columns: Pro
@@ -2043,24 +2043,28 @@
 ypes.array,%0A
+    
     data: Pr
@@ -2078,24 +2078,28 @@
 .array,%0A    
+ 
+   
 startIndex: 
@@ -2116,16 +2116,20 @@
 number,%0A
+    
     idPr
@@ -2150,16 +2150,20 @@
 string,%0A
+    
     sele
@@ -2220,24 +2220,28 @@
 Mode)),%0A    
+   
+ 
 disabled: Pr
@@ -2255,16 +2255,20 @@
 .bool,%0A%0A
+    
     isIt
@@ -2295,24 +2295,28 @@
 .func,%0A%0A    
+  
+  
 onRowTouchTa
@@ -2334,24 +2334,28 @@
 s.func,%0A    
+  
+  
 onCellTouchT
@@ -2378,11 +2378,17 @@
 nc%0A%0A
-%7D);
+    %7D;%0A%0A%7D
 %0A%0ATb

update Tbody