# all files / src/ table.jsx

100% Statements 146/146
91.36% Branches 74/81
100% Functions 27/27
100% Lines 51/51
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
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    1987×
    7×
    7×
    7×

    17×

    17×

    17×

    7×

    7×

    7×

    9×

    4×
    8×

    9×

    9×
    9×

    9×

    9×

    9×

    7×

    7×

    7×

    9×

    12×
    23×

    18×
    19×
    23×

    23×

    23×

    23×
    23×
    23×

    23×

    23×

    23×

    7×

    7×

    7×

    7×

    1×
    32×

    38×

    38×

    1×
    70×
    4×

    66×

    7×
    7×

     import get from 'lodash/get';
    import has from 'lodash/has';
    import merge from 'lodash/merge';
    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,
            transforms: React.PropTypes.arrayOf(React.PropTypes.func),
            format: React.PropTypes.func,
            component: React.PropTypes.any, // XXX: too loose? createElement first param
            props: React.PropTypes.object
          }),
          cell: React.PropTypes.shape({
            property: React.PropTypes.oneOfType([
              React.PropTypes.number,
              React.PropTypes.string
            ]),
            transforms: React.PropTypes.arrayOf(React.PropTypes.func),
            format: React.PropTypes.func,
            resolve: React.PropTypes.func,
            component: React.PropTypes.any, // XXX: too loose? createElement first param
            props: React.PropTypes.object
          })
        })
      ).isRequired,
      data: React.PropTypes.array.isRequired,
      // TODO: if data is an array of arrays instead of an array of objects,
      // then rowKey isn't required
      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,
              transforms = [() => ({})],
              format = a => a,
              component = 'th',
              props // eslint-disable-line no-shadow
            } = column.header || {};
            const extraParameters = {
              cellData: label,
              columnIndex: i,
              column
            };
            const key = `${i}-header`;
            const transformed = evaluateTransforms(transforms, label, extraParameters);

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

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

            return React.createElement(
              component,
              {
                key,
                ...props,
                ...transformed,
                ...{ className: mergedClassName }
              },
              transformed.children || format(label, extraParameters)
            );
          })}
        </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,
              transforms = [() => ({})],
              format = a => a,
              resolve = a => a,
              component = 'td',
              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],
              columnIndex: j,
              column,
              rowIndex: i,
              property
            };
            const value = get(r, property);
            const resolvedValue = resolve(value, extraParameters);
            const transformed = evaluateTransforms(transforms, value, extraParameters);

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

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

            return React.createElement(
              component,
              {
                key: `${j}-cell`,
                ...props,
                ...transformed,
                ...{ className: mergedClassName }
              },
              transformed.children || format(resolvedValue, extraParameters)
            );
          })
        }</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 evaluateTransforms(transforms, value, extraParameters) {
      return transforms.reduceRight(
        (a, t) => {
          const result = t(value, extraParameters);

          return merge({}, a, result, {
            className: mergeClassNames(a.className, result.className)
          });
        },
        {}
      );
    }

    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;