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

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

import Td from '../_Td';

class Tr extends Component {

    constructor(props, ...restArgs) {
        super(props, ...restArgs);
    }

    calSpan = (col, colIndex) => {
        return col.span && typeof col.span === 'function' ?
            col.span(this.props.rowIndex, colIndex)
            :
            null;
    };

    calColumns = () => {

        const {columns} = this.props,
            result = [];
        let spanFlag = 0;

        for (let i = 0, len = columns.length; i < len; i++) {

            if (spanFlag > 1) {
                spanFlag--;
                continue;
            }

            const col = columns[i],
                span = this.calSpan(columns[i], i);

            if (span && span > 1) {
                spanFlag = span;
            }

            result.push({
                col,
                span
            });

        }

        return result;

    };

    handleClick = e => {
        const {data, rowIndex, disabled, onRowClick} = this.props;
        !disabled && onRowClick && onRowClick(data, rowIndex, e);
    };

    render() {

        const {
                className, rowIndex, data, isChecked, disabled, isExpanded, expandedIconCls,
                onCellClick
            } = this.props,
            columns = this.calColumns();

        return (
            <tr className={classNames({
                [className]: className,
                activated: isChecked,
                [data.rowClassName]: data.rowClassName
            })}
                style={data.rowStyle}
                disabled={disabled}
                onClick={this.handleClick}>
                {
                    columns && columns.map((column, colIndex) =>
                        <Td key={colIndex}
                            rowIndex={rowIndex}
                            colIndex={colIndex}
                            column={column}
                            data={data}
                            isExpanded={isExpanded}
                            disabled={disabled}
                            expandedIconCls={expandedIconCls}
                            onCellClick={onCellClick}/>
                    )
                }
            </tr>
        );

    }
}

Tr.propTypes = {

    rowIndex: PropTypes.number,
    columns: PropTypes.array,
    data: PropTypes.object,
    isChecked: PropTypes.bool,
    disabled: PropTypes.bool,
    expandedIconCls: PropTypes.string,
    className: PropTypes.string,

    onRowClick: PropTypes.func,
    onCellClick: PropTypes.func

};

Tr.defaultProps = {
    className: '',
    rowIndex: 0,
    expandedIconCls: 'fas fa-angle-right',
    columns: [],
    data: {},
    isChecked: false,
    disabled: false
};

export default Tr;

@@ -330,776 +330,8 @@
  %7D%0A%0A
-    calSpan = (col, colIndex) =%3E %7B%0A        return col.span && typeof col.span === 'function' ?%0A            col.span(this.props.rowIndex, colIndex)%0A            :%0A            null;%0A    %7D;%0A%0A    calColumns = () =%3E %7B%0A%0A        const %7Bcolumns%7D = this.props,%0A            result = %5B%5D;%0A        let spanFlag = 0;%0A%0A        for (let i = 0, len = columns.length; i %3C len; i++) %7B%0A%0A            if (spanFlag %3E 1) %7B%0A                spanFlag--;%0A                continue;%0A            %7D%0A%0A            const col = columns%5Bi%5D,%0A                span = this.calSpan(columns%5Bi%5D, i);%0A%0A            if (span && span %3E 1) %7B%0A                spanFlag = span;%0A            %7D%0A%0A            result.push(%7B%0A                col,%0A                span%0A            %7D);%0A%0A        %7D%0A%0A        return result;%0A%0A    %7D;%0A%0A
     
@@ -536,20 +536,16 @@
         
-    
 classNam
@@ -546,16 +546,33 @@
 assName,
+ columnsWithSpan,
  rowInde
@@ -634,36 +634,32 @@
 ls,%0A            
-    
 onCellClick%0A    
@@ -662,20 +662,16 @@
         
-  
-  
 %7D = this
@@ -680,49 +680,8 @@
 rops
-,%0A            columns = this.calColumns()
 ;%0A%0A 
@@ -1048,16 +1048,24 @@
  columns
+WithSpan
  && colu
@@ -1067,16 +1067,24 @@
  columns
+WithSpan
 .map((co
@@ -1702,16 +1702,54 @@
 .array,%0A
+    columnsWithSpan: PropTypes.array,%0A
     data

update Tr