react-cohort-chart
Version:
Cohort Analysis Graph with ReactJS
346 lines (298 loc) • 17.1 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = void 0;
var _react = _interopRequireDefault(require("react"));
var _propTypes = _interopRequireDefault(require("prop-types"));
var _styles = require("./styles");
var _DataStore = _interopRequireDefault(require("./DataStore"));
var _components = require("./components");
var _constants = require("./constants");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }
function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }
function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Date.prototype.toString.call(Reflect.construct(Date, [], function () {})); return true; } catch (e) { return false; } }
function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
var ReactCohortGraph = /*#__PURE__*/function (_React$Component) {
_inherits(ReactCohortGraph, _React$Component);
var _super = _createSuper(ReactCohortGraph);
function ReactCohortGraph(_props) {
var _this;
_classCallCheck(this, ReactCohortGraph);
_this = _super.call(this, _props);
_defineProperty(_assertThisInitialized(_this), "_getStore", function (props) {
var _props$data = props.data,
data = _props$data === void 0 ? {} : _props$data,
_props$shadeColor = props.shadeColor,
shadeColor = _props$shadeColor === void 0 ? _styles.DEFAULT_SHADE_COLOR : _props$shadeColor,
_props$headerCellColo = props.headerCellColor,
headerCellColor = _props$headerCellColo === void 0 ? _styles.DEFAULT_HEADER_CELL_COLOR : _props$headerCellColo,
_props$bodyCellColor = props.bodyCellColor,
bodyCellColor = _props$bodyCellColor === void 0 ? _styles.DEFAULT_BODY_CELL_COLOR : _props$bodyCellColor,
_props$keyCellColor = props.keyCellColor,
keyCellColor = _props$keyCellColor === void 0 ? _styles.DEFAULT_KEY_CELL_COLOR : _props$keyCellColor;
return new _DataStore["default"](data, {
shadeColor: shadeColor,
headerCellColor: headerCellColor,
bodyCellColor: bodyCellColor,
keyCellColor: keyCellColor
});
});
_defineProperty(_assertThisInitialized(_this), "isFixed", function (index) {
return index < 2;
});
_defineProperty(_assertThisInitialized(_this), "renderChildren", function (props) {
return _react["default"].Children.map(props.children, function (child) {
return /*#__PURE__*/_react["default"].cloneElement(child, props);
});
});
var _props$data2 = _props.data,
_data = _props$data2 === void 0 ? {} : _props$data2,
_props$defaultValueTy = _props.defaultValueType,
defaultValueType = _props$defaultValueTy === void 0 ? _constants.VALUE_KEYS.PERCENT : _props$defaultValueTy,
_shadeColor = _props.shadeColor;
_this.state = {
dataStore: _this._getStore(_props),
currentType: "",
valueType: defaultValueType
};
return _this;
}
_createClass(ReactCohortGraph, [{
key: "componentWillMount",
value: function componentWillMount() {
var _this$props = this.props,
data = _this$props.data,
onStoreUpdate = _this$props.onStoreUpdate;
var keys = Object.keys(data);
if (keys.length > 0) {
var store = this._getStore(this.props);
var currentType = keys[0];
if (typeof onStoreUpdate === 'function') {
onStoreUpdate(store, currentType, this.state.valueType);
}
this.setState({
currentType: currentType,
dataStore: store
});
}
}
}, {
key: "componentWillUpdate",
value: function componentWillUpdate(nextProps) {
var data = nextProps.data,
dataType = nextProps.dataType,
valueType = nextProps.valueType,
onStoreUpdate = nextProps.onStoreUpdate;
var currentType = this.state.currentType;
var keys = Object.keys(data);
if (keys.length > 0) {
var store = this._getStore(this.props);
var currentDataType = dataType || Object.keys(data)[0];
if (currentType === "" || valueType === this.state.valueType && dataType === currentType) {
this.setState({
dataStore: store
});
} else {
if (valueType) {
this.setState({
currentType: currentDataType,
valueType: valueType
});
} else {
this.setState({
currentType: currentDataType
});
}
}
if (typeof onStoreUpdate === 'function') {
onStoreUpdate(store, currentDataType, valueType);
}
}
}
}, {
key: "componentDidMount",
value: function componentDidMount() {}
}, {
key: "render",
value: function render() {
var _this2 = this;
var _this$props2 = this.props,
_this$props2$showEmpt = _this$props2.showEmptyDataMessage,
showEmptyDataMessage = _this$props2$showEmpt === void 0 ? true : _this$props2$showEmpt,
customEmptyDataMessage = _this$props2.customEmptyDataMessage,
showHeaderValues = _this$props2.showHeaderValues,
cellFormatter = _this$props2.cellFormatter,
headerFormatter = _this$props2.headerFormatter,
_this$props2$bodyCell = _this$props2.bodyCellStyles,
bodyCellStyles = _this$props2$bodyCell === void 0 ? {} : _this$props2$bodyCell,
_this$props2$headerCe = _this$props2.headerCellStyles,
headerCellStyles = _this$props2$headerCe === void 0 ? {} : _this$props2$headerCe,
tableStyles = _this$props2.tableStyles,
tableRowStyles = _this$props2.tableRowStyles,
tableHeadingStyles = _this$props2.tableHeadingStyles,
tableBodyStyles = _this$props2.tableBodyStyles,
fixedTablePartStyles = _this$props2.fixedTablePartStyles,
wrapperStyles = _this$props2.wrapperStyles,
scrollableTablePartStyles = _this$props2.scrollableTablePartStyles,
scrollableTableContentStyles = _this$props2.scrollableTableContentStyles,
headerLabelStyles = _this$props2.headerLabelStyles,
tableCellStyles = _this$props2.tableCellStyles;
var _this$state = this.state,
dataStore = _this$state.dataStore,
currentType = _this$state.currentType,
valueType = _this$state.valueType;
var header = dataStore.getHeader(currentType);
var rows = dataStore.getRows(currentType);
var TableStyles = (0, _styles.table)(tableStyles);
var TableRowStyles = (0, _styles.tableRow)(tableRowStyles);
var TableHeadingStyles = (0, _styles.tableHeading)(tableHeadingStyles);
var TableBodyStyles = (0, _styles.tableBody)(tableBodyStyles);
var FixedTablePartStyles = (0, _styles.fixedTablePart)(fixedTablePartStyles);
var WrapperStyles = (0, _styles.wrapper)(wrapperStyles);
var ScrollableTablePartStyles = (0, _styles.scrollableTablePart)(scrollableTablePartStyles);
var ScrollableTableContentStyles = (0, _styles.scrollableTableContent)(scrollableTableContentStyles);
if (header && header.length > 0) {
return /*#__PURE__*/_react["default"].createElement("div", {
style: WrapperStyles
}, this.renderChildren(_objectSpread(_objectSpread({}, this.props), this.state)), /*#__PURE__*/_react["default"].createElement("div", {
style: TableStyles
}, /*#__PURE__*/_react["default"].createElement("div", {
style: TableBodyStyles
}, /*#__PURE__*/_react["default"].createElement("div", {
style: TableRowStyles
}, /*#__PURE__*/_react["default"].createElement("div", {
style: FixedTablePartStyles
}, /*#__PURE__*/_react["default"].createElement("div", {
style: TableStyles
}, /*#__PURE__*/_react["default"].createElement("div", {
style: TableHeadingStyles
}, header.map(function (headerCell, i) {
return _this2.isFixed(i) && /*#__PURE__*/_react["default"].createElement(_components.HeaderCell, _extends({
tableCellStyles: tableCellStyles,
headerLabelStyles: headerLabelStyles,
style: headerCellStyles,
key: "header" + i
}, headerCell, {
formatter: typeof headerFormatter === "function" ? headerFormatter : cellFormatter,
showHeaderValues: showHeaderValues,
valueType: valueType,
isFixed: true
}));
})), /*#__PURE__*/_react["default"].createElement("div", {
style: TableBodyStyles
}, rows.map(function (row, j) {
return /*#__PURE__*/_react["default"].createElement("div", {
style: TableRowStyles,
key: "row" + j
}, row.map(function (cell, k) {
return _this2.isFixed(k) && /*#__PURE__*/_react["default"].createElement(_components.BodyCell, _extends({
tableCellStyles: tableCellStyles,
style: bodyCellStyles,
key: "cell" + k
}, cell, {
valueType: valueType,
formatter: cellFormatter,
isFixed: true
}));
}));
})))), /*#__PURE__*/_react["default"].createElement("div", {
style: ScrollableTablePartStyles
}, /*#__PURE__*/_react["default"].createElement(_components.ScrollableContent, {
scrollableTableContentStyles: scrollableTableContentStyles
}, /*#__PURE__*/_react["default"].createElement("div", {
style: TableStyles
}, /*#__PURE__*/_react["default"].createElement("div", {
style: TableHeadingStyles
}, header.map(function (headerCell, i) {
return !_this2.isFixed(i) && /*#__PURE__*/_react["default"].createElement(_components.HeaderCell, _extends({
tableCellStyles: tableCellStyles,
style: headerCellStyles,
key: "header" + i
}, headerCell, {
formatter: typeof headerFormatter === "function" ? headerFormatter : cellFormatter,
showHeaderValues: showHeaderValues,
valueType: valueType,
isFixed: false
}));
})), /*#__PURE__*/_react["default"].createElement("div", {
style: TableBodyStyles
}, rows.map(function (row, j) {
return /*#__PURE__*/_react["default"].createElement("div", {
style: TableRowStyles,
key: "row" + j
}, row.map(function (cell, k) {
return !_this2.isFixed(k) && /*#__PURE__*/_react["default"].createElement(_components.BodyCell, _extends({
tableCellStyles: tableCellStyles,
style: bodyCellStyles,
key: "cell" + k
}, cell, {
valueType: valueType,
formatter: cellFormatter,
isFixed: false
}));
}));
})))))))));
}
if (showEmptyDataMessage) {
return customEmptyDataMessage || /*#__PURE__*/_react["default"].createElement("h3", null, "No Data..!");
}
}
}]);
return ReactCohortGraph;
}(_react["default"].Component);
ReactCohortGraph.propTypes = {
data: _propTypes["default"].object.isRequired,
dataType: _propTypes["default"].string,
//keys of data
defaultValueType: _propTypes["default"].string,
//["value", "percent"]
cellClickEvent: _propTypes["default"].func,
showEmptyDataMessage: _propTypes["default"].bool,
customEmptyDataMessage: _propTypes["default"].any,
columnClickEvent: _propTypes["default"].func,
shadeColor: _propTypes["default"].string,
//#3f83a3
headerCellColor: _propTypes["default"].string,
bodyCellColor: _propTypes["default"].string,
keyCellColor: _propTypes["default"].string,
headerFormatter: _propTypes["default"].func,
cellFormatter: _propTypes["default"].func,
/*maxDays : PropTypes.number,
maxWeeks : PropTypes.number, //TODO:
maxMonths : PropTypes.number,*/
//enableTooltip : PropTypes.bool, TODO
showAbsolute: _propTypes["default"].bool,
toggleValues: _propTypes["default"].bool,
showHeaderValues: _propTypes["default"].bool,
onStoreUpdate: _propTypes["default"].func,
//function(store, currentType, valueType)
//Styles
headerCellStyles: _propTypes["default"].object,
bodyCellStyles: _propTypes["default"].object,
tableCellStyles: _propTypes["default"].object,
tableStyles: _propTypes["default"].object,
tableRowStyles: _propTypes["default"].object,
tableHeadingStyles: _propTypes["default"].object,
tableBodyStyles: _propTypes["default"].object,
fixedTablePartStyles: _propTypes["default"].object,
wrapperStyles: _propTypes["default"].object,
scrollableTablePartStyles: _propTypes["default"].object,
scrollableTableContentStyles: _propTypes["default"].object,
headerValueStyles: _propTypes["default"].object,
headerLabelStyles: _propTypes["default"].object
};
var _default = ReactCohortGraph;
exports["default"] = _default;