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

91.43% Statements 32/35
88.89% Branches 16/18
75% Functions 9/12
100% Lines 32/32

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 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218                                                5x       10x 10x 10x 10x       1x 1x 1x       10x 3x 3x 3x 2x       10x                   43x 43x 43x 43x 43x   43x 43x           43x 20x                                           23x                         10x   10x                                                                                                                     5x                                 5x 10x 2x       5x 10x                                                
import React, { Component, Fragment } from "react";
import { findDOMNode } from "react-dom";
import { connect } from "react-redux";
import { ScrollSyncPane } from "react-scroll-sync";
import { FixedSizeList } from "react-window";
import { throttle } from "lodash";
import BodyRow from "./BodyRow";
import { setIsScrolling as setIsScrollingAction } from "../../../redux/actions/datatableActions";
import {
  rowsPropType,
  CustomTableBodyRowPropType,
  columnsOrderPropType,
  dimensionsPropType,
  strippedPropType,
  setIsScrollingPropType,
  isScrollingPropType,
  keyColumnPropType,
  rowsEditedPropType,
  heightNumberPropType,
  widthNumberPropType,
  columnSizeMultiplierPropType,
  customPropsPropType
} from "../../../proptypes";
 
export const tableRef = React.createRef();
 
export class Body extends Component {
  componentDidMount() {
    const virtualizedContainer = findDOMNode(tableRef.current);
    const callBack = () =>
      throttle(() => this.handleScroll(virtualizedContainer.scrollLeft), 500);
    virtualizedContainer.addEventListener("scroll", callBack());
  }
 
  componentWillUnmount() {
    const { rows } = this.props;
    Eif (rows.length > 0) {
      findDOMNode(tableRef.current).removeEventListener("scroll", null);
    }
  }
 
  handleScroll = scrollLeft => {
    const { setIsScrolling, isScrolling } = this.props;
    const bool = scrollLeft > 0;
    if (bool !== isScrolling) {
      setIsScrolling(bool);
    }
  };
 
  rowBuilder = ({ index, style }) => {
    const {
      CustomTableBodyRow,
      rows,
      dimensions,
      columnsOrder,
      keyColumn,
      stripped,
      rowsEdited,
      customProps
    } = this.props;
    const key = `row-${index}`;
    const { columnSizeMultiplier } = dimensions;
    let row = rows[index];
    row = rowsEdited.find(r => r[keyColumn] === row[keyColumn]) || row;
    const editing =
      rowsEdited.find(r => r[keyColumn] === row[keyColumn]) !== undefined;
    const newStyle = {
      ...style,
      backgroundColor:
        stripped && index % 2 === 0 ? "rgba(228, 228, 228, 1)" : "white"
    };
 
    if (CustomTableBodyRow !== null) {
      return (
        <div
          style={{
            top: newStyle.top,
            height: newStyle.height,
            position: newStyle.position,
            borderBottom: "1px solid rgba(224, 224, 244, 1)"
          }}
        >
          <CustomTableBodyRow
            customProps={customProps}
            row={row}
            columnsOrder={columnsOrder}
            rowIndex={index}
            height={newStyle.height}
            columnSizeMultiplier={columnSizeMultiplier}
            key={key}
          />
        </div>
      );
    }
 
    return <BodyRow row={row} editing={editing} style={newStyle} key={key} />;
  };
 
  render() {
    const {
      rows,
      dimensions,
      columnsOrder,
      rowsEdited,
      height,
      width,
      totalWidthNumber,
      columnSizeMultiplier
    } = this.props;
 
    return (
      <Fragment>
        {rows.length > 0 && (
          <div className="Table-Body">
            <ScrollSyncPane>
              <FixedSizeList
                ref={tableRef}
                className="virtualized-container"
                height={dimensions.body.heightNumber}
                itemCount={rows.length}
                itemSize={dimensions.row.heightNumber}
                width={dimensions.datatable.widthNumber}
                columnsOrder={columnsOrder}
                rowsEdited={rowsEdited}
                rows={rows}
                style={{ overflowX: "auto", overflowY: "scroll" }}
              >
                {this.rowBuilder}
              </FixedSizeList>
            </ScrollSyncPane>
          </div>
        )}
 
        {rows.length === 0 && (
          <Fragment>
            <div
              id="no-rows-filtered"
              style={{
                height: height - 15,
                width: width - 15
              }}
            >
              There is no result for your search
            </div>
            <ScrollSyncPane>
              <div
                style={{
                  overflowX: columnSizeMultiplier === 1 ? "scroll" : "hidden",
                  overflowY: "hidden",
                  height: "15px",
                  width: width - 15
                }}
              >
                <div
                  style={{
                    width: totalWidthNumber
                  }}
                >
                  .
                </div>
              </div>
            </ScrollSyncPane>
          </Fragment>
        )}
      </Fragment>
    );
  }
}
 
Body.propTypes = {
  rows: rowsPropType.isRequired,
  customProps: customPropsPropType,
  columnsOrder: columnsOrderPropType.isRequired,
  dimensions: dimensionsPropType.isRequired,
  CustomTableBodyRow: CustomTableBodyRowPropType,
  setIsScrolling: setIsScrollingPropType,
  isScrolling: isScrollingPropType.isRequired,
  stripped: strippedPropType.isRequired,
  keyColumn: keyColumnPropType.isRequired,
  rowsEdited: rowsEditedPropType.isRequired,
  height: heightNumberPropType.isRequired,
  width: widthNumberPropType.isRequired,
  totalWidthNumber: widthNumberPropType,
  columnSizeMultiplier: columnSizeMultiplierPropType
};
 
const mapDispatchToProps = dispatch => {
  return {
    setIsScrolling: bool => dispatch(setIsScrollingAction(bool))
  };
};
 
const mapStateToProps = state => {
  return {
    customProps: state.customComponentsReducer.customProps,
    rows: state.datatableReducer.pagination.rowsCurrentPage,
    dimensions: state.datatableReducer.dimensions,
    columnsOrder:
      state.datatableReducer.features.userConfiguration.columnsOrder,
    rowsEdited: state.datatableReducer.rowsEdited,
    height: state.datatableReducer.dimensions.body.heightNumber,
    width: state.datatableReducer.dimensions.datatable.widthNumber,
    keyColumn: state.datatableReducer.keyColumn,
    isScrolling: state.datatableReducer.dimensions.isScrolling,
    stripped: state.datatableReducer.stripped,
    totalWidthNumber:
      state.datatableReducer.dimensions.datatable.totalWidthNumber,
    columnSizeMultiplier:
      state.datatableReducer.dimensions.columnSizeMultiplier,
    CustomTableBodyRow: state.customComponentsReducer.CustomTableBodyRow
  };
};
 
export default connect(
  mapStateToProps,
  mapDispatchToProps
)(Body);