# all files / src/ table.jsx

100% Statements 136/136
93.51% Branches 72/77
100% Functions 25/25
100% Lines 46/46
8 statements, 15 branches Ignored

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    1657×
    7×
    7×

    9×

    9×

    9×

    7×

    7×

    7×

    6×

    4×
    5×

    6×
    6×
    6×

    6×

    6×

    6×

    7×

    7×

    7×

    7×

    6×
    15×

    14×
    11×
    15×

    15×

    15×
    15×
    15×
    15×

    15×

    15×

    15×

    7×

    7×

    7×

    7×

    1×
    21×
    2×

    19×

    7×
    7×

     import get from 'lodash/get';
    import has from 'lodash/has';
    import React from 'react';

    class Table extends React.Component {
      getChildContext() {
        return {
          columns: this.props.columns,
          data: this.props.data,
          rowKey: this.props.rowKey
        };
      }
      render() {
        const {
          columns, data, children, ...props // eslint-disable-line no-unused-vars
        } = this.props;

        return <table {...props}>{children}</table>;
      }
    }
    Table.propTypes = {
      columns: React.PropTypes.arrayOf(
        React.PropTypes.shape({
          header: React.PropTypes.shape({
            label: React.PropTypes.string,
            transform: React.PropTypes.func,
            format: React.PropTypes.func,
            props: React.PropTypes.object
          }),
          cell: React.PropTypes.shape({
            property: React.PropTypes.string,
            transform: React.PropTypes.func,
            format: React.PropTypes.func,
            resolve: React.PropTypes.func,
            props: React.PropTypes.object
          })
        })
      ).isRequired,
      data: React.PropTypes.array.isRequired,
      rowKey: React.PropTypes.string.isRequired,
      children: React.PropTypes.any
    };
    Table.childContextTypes = {
      columns: React.PropTypes.array,
      data: React.PropTypes.array,
      rowKey: React.PropTypes.string.isRequired
    };

    const Header = ({ children, className, ...props }, { columns }) => (
      <thead {...props}>
        <tr>
          {columns.map((column, i) => {
            const {
              label,
              transform = a => ({}), // eslint-disable-line no-unused-vars
              format = a => a,
              props // eslint-disable-line no-shadow
            } = column.header || {};
            const extraParameters = { cellData: label };
            const key = `${i}-header`;
            const transformed = transform(label, extraParameters);

            Iif (!transformed) {
              console.warn('Table.Header - Failed to receive a transformed result', transformed); // eslint-disable-line max-len, no-console
            }

            const mergedClassName = mergeClassNames(
              className, transformed && transformed.className
            );

            return (
              <th
                key={key}
                {
                  ...{ ...props, ...transformed, ...{ className: mergedClassName } }
                }
              >
                {transformed.children || format(label, extraParameters)}
              </th>
            );
          })}
        </tr>
        {children}
      </thead>
    );
    Header.propTypes = {
      children: React.PropTypes.any,
      className: React.PropTypes.string
    };
    Header.contextTypes = {
      columns: React.PropTypes.array.isRequired
    };
    Header.displayName = 'Table.Header';

    const Body = ({ row, className, ...props }, { columns, data, rowKey }) => (
      <tbody {...props}>{
        data.map((r, i) => <tr key={`${r[rowKey] || i}-row`} {...row(r, i)}>{
          columns.map((column, j) => {
            const {
              property,
              transform = a => ({}), // eslint-disable-line no-unused-vars
              format = a => a,
              resolve = a => a,
              props // eslint-disable-line no-shadow
            } = column.cell;
            Iif (property && !has(r, property)) {
              console.warn(`Table.Body - Failed to find "${property}" property from`, r); // eslint-disable-line max-len, no-console
            }

            const extraParameters = { cellData: data[i], property };
            const value = get(r, property);
            const resolvedValue = resolve(value, extraParameters);
            const transformed = transform(value, extraParameters);

            Iif (!transformed) {
              console.warn('Table.Body - Failed to receive a transformed result', transformed); // eslint-disable-line max-len, no-console
            }

            const mergedClassName = mergeClassNames(
              className, transformed && transformed.className
            );

            return (
              <td
                key={`${j}-cell`}
                {
                  ...{ ...props, ...transformed, ...{ className: mergedClassName } }
                }
              >
                {transformed.children || format(resolvedValue, extraParameters)}
              </td>
            );
          })
        }</tr>)
      }</tbody>
    );
    Body.propTypes = {
      row: React.PropTypes.func,
      className: React.PropTypes.string
    };
    Body.defaultProps = {
      row: () => {}
    };
    Body.contextTypes = {
      columns: React.PropTypes.array.isRequired,
      data: React.PropTypes.array.isRequired,
      rowKey: React.PropTypes.string.isRequired
    };
    Body.displayName = 'Table.Body';

    function mergeClassNames(a, b) {
      if (a && b) {
        return `${a} ${b}`;
      }

      // Either a or b at this point
      return (a || '') + (b || '');
    }

    Table.Header = Header;
    Table.Body = Body;

    export default Table;