All files / src/components/DatatableCore/Body BodyRow.js

94.29% Statements 33/35
86.36% Branches 19/22
90.91% Functions 10/11
96.97% Lines 32/33

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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                                            29x 5x 5x 4x 4x                   29x                 232x 1044x 232x 232x 232x   232x     232x 29x 29x                       203x       203x 19x                 184x 12x               1x             172x               4x           29x 29x                                 232x                             6x                             6x 26x   2x       6x 26x                                      
import React, { Component, Fragment } from "react";
import { connect } from "react-redux";
import copy from "copy-to-clipboard";
import {
  rowPropType,
  columnsPropType,
  columnsOrderPropType,
  CustomTableBodyCellPropType,
  columnSizeMultiplierPropType,
  keyColumnPropType,
  editingPropType,
  rowsSelectedPropType,
  stylePropType,
  copyToClipboardPropType,
  enqueueSnackbarPropType,
  customPropsPropType
} from "../../../proptypes";
import BodyCell from "./BodyCell";
import BodyActionsCell from "./BodyActionsCell";
import { enqueueSnackbar as enqueueSnackbarAction } from "../../../redux/actions/notifierActions";
 
export class BodyRow extends Component {
  copyToClipboardFunction = val => {
    const { enqueueSnackbar, copyToClipboard } = this.props;
    if (copyToClipboard) {
      copy(val);
      enqueueSnackbar({
        message: "Cell's content has been copied to clipboard.",
        options: {
          key: new Date().getTime() + Math.random(),
          variant: "info"
        }
      });
    }
  };
 
  bodyCellBuilder = (val, columnId, cellIndex, row, editing) => {
    const {
      columns,
      CustomTableBodyCell,
      columnSizeMultiplier,
      rowsSelected,
      keyColumn,
      style,
      customProps
    } = this.props;
    const column = columns.find(col => col.id === columnId);
    const rowId = row[keyColumn];
    const key = `row-${row[keyColumn]}-cell-${columnId}`;
    let isEditing = editing && column.editable;
 
    Iif (row.editableId && !row.editableId.includes(columnId)) {
      isEditing = false;
    }
    if (columnId === "o2xpActions") {
      const checked = !!rowsSelected.find(r => r[keyColumn] === row[keyColumn]);
      return (
        <BodyActionsCell
          style={style}
          key={key}
          column={column}
          row={row}
          editing={editing}
          checked={checked}
        />
      );
    }
 
    const width = `${(
      column.colSize.split("px")[0] * columnSizeMultiplier
    ).toString()}px`;
 
    if ((val === null || val === undefined || val === "") && !editing) {
      return (
        <div className="Table-Cell" key={key}>
          <div style={{ width }}>
            <div className="no-data" />
          </div>
        </div>
      );
    }
 
    if (CustomTableBodyCell !== null && !isEditing) {
      return (
        <div className="Table-Cell" key={key}>
          <div style={{ width }}>
            <CustomTableBodyCell
              customProps={customProps}
              cellVal={val}
              column={column}
              rowId={rowId}
              onClick={() => this.copyToClipboardFunction(val)}
            />
          </div>
        </div>
      );
    }
 
    return (
      <BodyCell
        cellVal={val}
        editing={isEditing}
        width={width}
        column={column}
        rowId={rowId}
        key={key}
        onClick={() => (isEditing ? null : this.copyToClipboardFunction(val))}
      />
    );
  };
 
  render() {
    const { style, row, columnsOrder, editing } = this.props;
    return (
      <Fragment>
        <div
          style={{
            top: style.top,
            height: style.height,
            position: style.position
          }}
        >
          <div
            className="Table-Row"
            style={{
              height: style.height,
              backgroundColor: style.backgroundColor
            }}
          >
            {columnsOrder.map((columnId, cellIndex) => {
              return this.bodyCellBuilder(
                row[columnId],
                columnId,
                cellIndex,
                row,
                editing
              );
            })}
          </div>
        </div>
      </Fragment>
    );
  }
}
 
BodyRow.propTypes = {
  row: rowPropType.isRequired,
  customProps: customPropsPropType,
  columnsOrder: columnsOrderPropType.isRequired,
  columns: columnsPropType.isRequired,
  columnSizeMultiplier: columnSizeMultiplierPropType.isRequired,
  style: stylePropType.isRequired,
  keyColumn: keyColumnPropType.isRequired,
  editing: editingPropType.isRequired,
  enqueueSnackbar: enqueueSnackbarPropType,
  rowsSelected: rowsSelectedPropType.isRequired,
  copyToClipboard: copyToClipboardPropType.isRequired,
  CustomTableBodyCell: CustomTableBodyCellPropType
};
 
const mapDispatchToProps = dispatch => {
  return {
    enqueueSnackbar: ({ message, options }) =>
      dispatch(enqueueSnackbarAction({ message, options }))
  };
};
 
const mapStateToProps = state => {
  return {
    customProps: state.customComponentsReducer.customProps,
    columns: state.datatableReducer.data.columns,
    copyToClipboard:
      state.datatableReducer.features.userConfiguration.copyToClipboard,
    keyColumn: state.datatableReducer.keyColumn,
    rowsSelected: state.datatableReducer.rowsSelected,
    columnsOrder:
      state.datatableReducer.features.userConfiguration.columnsOrder,
    columnSizeMultiplier:
      state.datatableReducer.dimensions.columnSizeMultiplier,
    CustomTableBodyCell: state.customComponentsReducer.CustomTableBodyCell
  };
};
 
export default connect(
  mapStateToProps,
  mapDispatchToProps
)(BodyRow);