All files / src/components/DatatableCore/Header HeaderCell.js

100% Statements 38/38
100% Branches 19/19
100% Functions 13/13
100% Lines 38/38

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                                                  40x 40x         40x 3x     40x 8x 8x 8x   8x 4x   8x             2x 1x 1x   1x 1x                                     40x 50x 50x 50x   6x   10x   6x   2x   2x   6x   18x         43x 43x 43x                   6x 36x                         6x                 6x 29x 1x       6x 29x                    
import React, { Component, Fragment } from "react";
import { sortableElement } from "react-sortable-hoc";
import { Tooltip, Zoom } from "@material-ui/core";
import { ArrowUpward as ArrowIcon } from "@material-ui/icons";
import { connect } from "react-redux";
import {
  NumberWrapper,
  TextWrapper,
  BooleanWrapper,
  DateWrapper,
  TimeWrapper,
  DateTimeWrapper
} from "../CellTypes";
import {
  columnPropType,
  widthPropType,
  indexPropType,
  orderByColumnsPropType,
  orderByPropType,
  canOrderColumnsPropType
} from "../../../proptypes";
import { orderByColumns as orderByColumnsAction } from "../../../redux/actions/datatableActions";
 
export class HeaderCell extends Component {
  constructor(props) {
    super(props);
    this.state = {
      childButtonHovered: false
    };
  }
 
  setHover = bool => {
    this.setState({ childButtonHovered: bool });
  };
 
  buildButton = column => {
    const { orderByColumns, orderBy } = this.props;
    const { keys, order } = orderBy;
    const index = keys.indexOf(column.id);
    let orderElement;
    if (index !== -1) {
      orderElement = order[index];
    }
    return (
      <div className="cell-header">
        <Tooltip TransitionComponent={Zoom} title="Order by">
          <span>
            <button
              type="button"
              className="button-header"
              onMouseOver={() => this.setHover(true)}
              onMouseLeave={() => this.setHover(false)}
              onFocus={() => null}
              onClick={e => {
                e.stopPropagation();
                orderByColumns(column.id);
              }}
            >
              {column.label}
            </button>
          </span>
        </Tooltip>
        {orderElement && (
          <Fragment>
            <ArrowIcon
              className={orderElement === "asc" ? "ascIcon" : "descIcon"}
            />
            {index + 1}
          </Fragment>
        )}
      </div>
    );
  };
 
  buildHeaderCell = () => {
    const { width, column, canOrderColumns } = this.props;
    const content = canOrderColumns ? this.buildButton(column) : column.label;
    switch (column.dataType) {
      case "number":
        return <NumberWrapper style={{ width }}>{content}</NumberWrapper>;
      case "text":
        return <TextWrapper style={{ width }}>{content}</TextWrapper>;
      case "boolean":
        return <BooleanWrapper style={{ width }}>{content}</BooleanWrapper>;
      case "date":
        return <DateWrapper style={{ width }}>{content}</DateWrapper>;
      case "time":
        return <TimeWrapper style={{ width }}>{content}</TimeWrapper>;
      case "dateTime":
        return <DateTimeWrapper style={{ width }}>{content}</DateTimeWrapper>;
      default:
        return <TextWrapper style={{ width }}>{content}</TextWrapper>;
    }
  };
 
  render() {
    const { index } = this.props;
    const { childButtonHovered } = this.state;
    return (
      <SortableItem
        index={index}
        value={this.buildHeaderCell()}
        childButtonHovered={childButtonHovered}
      />
    );
  }
}
 
const SortableItem = sortableElement(({ value, childButtonHovered }) => (
  <Tooltip TransitionComponent={Zoom} title={childButtonHovered ? "" : "Drag"}>
    <div
      className={
        childButtonHovered
          ? "Table-Header-Cell-Child-Hovered"
          : "Table-Header-Cell"
      }
    >
      {value}
    </div>
  </Tooltip>
));
 
HeaderCell.propTypes = {
  column: columnPropType.isRequired,
  width: widthPropType.isRequired,
  index: indexPropType.isRequired,
  orderBy: orderByPropType.isRequired,
  canOrderColumns: canOrderColumnsPropType.isRequired,
  orderByColumns: orderByColumnsPropType
};
 
const mapDispatchToProps = dispatch => {
  return {
    orderByColumns: col => dispatch(orderByColumnsAction(col))
  };
};
 
const mapStateToProps = state => {
  return {
    canOrderColumns: state.datatableReducer.features.canOrderColumns,
    orderBy: state.datatableReducer.orderBy
  };
};
 
export default connect(
  mapStateToProps,
  mapDispatchToProps
)(HeaderCell);