# all files / src/ transforms.js

100% Statements 55/55
100% Branches 32/32
90% Functions 18/20
100% Lines 25/25
2 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
    727×

    7×

    5×
    5×
    5×

    5×
    4×

    2×

    1×
    1×

    5×

    5×

    7×
    3×

    4×
    4×
    4×
    4×

    4×
    1×

    4×

    1×

    4×

    4×

     import React from 'react';

    const edit = ({
      getEditId = () => {},
      getEditProperty = () => {},
      onActivate = () => {},
      onValue = () => {}
    } = {}) => editor => {
      const editTransform = (value, extraParameters) => {
        const idx = getEditId(extraParameters);
        const editedCell = getEditProperty();

        if (editedCell === idx) {
          return {
            children: React.createElement(
              editor,
              {
                value,
                onValue: v => onValue(
                  { value: v, ...extraParameters }
                )
              }
            )
          };
        }

        return {
          onClick: () => onActivate(idx)
        };
      };

      editTransform.toFormatter = (value, extraParameters) => React.createElement(
        'div', // This cannot return a span because it can have children
        editTransform(value, extraParameters)
      );

      return editTransform;
    };

    const sort = ({
      getSortingColumns = () => [],
      onSort = () => {}
    } = {}) => property => {
      const sortTransform = () => {
        const columns = getSortingColumns();
        const index = columns.map(c => c.property).indexOf(property);
        let headerClass = 'sort sort-none';

        if (index >= 0) {
          headerClass = `sort sort-${columns[index].sort}`;
        }

        return {
          className: headerClass,
          onClick: () => onSort(property)
        };
      };

      sortTransform.toFormatter = () => React.createElement(
        'span',
        sortTransform()
      );

      return sortTransform;
    };

    export default {
      edit,
      sort
    };