"use strict";

var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard")["default"];

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault")["default"];

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = void 0;

var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));

var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));

var _assertThisInitialized2 = _interopRequireDefault(require("@babel/runtime/helpers/assertThisInitialized"));

var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits"));

var _createSuper2 = _interopRequireDefault(require("@babel/runtime/helpers/createSuper"));

var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));

var _react = _interopRequireWildcard(require("react"));

var _classnames = _interopRequireDefault(require("classnames"));

var _noop = _interopRequireDefault(require("lodash/noop"));

var _util = require("../util");

var ROW = 4;
var COL = 3;

function chooseMonth(month) {
  var next = this.state.value.clone();
  next.month(month);
  this.setAndSelectValue(next);
}

var MonthTable = /*#__PURE__*/function (_Component) {
  (0, _inherits2["default"])(MonthTable, _Component);

  var _super = (0, _createSuper2["default"])(MonthTable);

  function MonthTable() {
    var _this;

    (0, _classCallCheck2["default"])(this, MonthTable);

    for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }

    _this = _super.call.apply(_super, [this].concat(args));
    (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "state", {
      value: _this.props.value
    });
    return _this;
  }

  (0, _createClass2["default"])(MonthTable, [{
    key: "componentWillReceiveProps",
    value: function componentWillReceiveProps(nextProps) {
      if ('value' in nextProps) {
        this.setState({
          value: nextProps.value
        });
      }
    }
  }, {
    key: "setAndSelectValue",
    value: function setAndSelectValue(value) {
      this.setState({
        value: value
      });
      this.props.onSelect(value);
    }
  }, {
    key: "months",
    value: function months() {
      var value = this.state.value;
      var current = value.clone();
      var months = [];
      var index = 0;

      for (var rowIndex = 0; rowIndex < ROW; rowIndex++) {
        months[rowIndex] = [];

        for (var colIndex = 0; colIndex < COL; colIndex++) {
          current.month(index);
          var content = (0, _util.getMonthName)(current);
          months[rowIndex][colIndex] = {
            value: index,
            content: content,
            title: content
          };
          index++;
        }
      }

      return months;
    }
  }, {
    key: "render",
    value: function render() {
      var _this2 = this;

      var props = this.props;
      var value = this.state.value;
      var today = (0, _util.getTodayTime)(value);
      var months = this.months();
      var currentMonth = value.month();
      var prefixCls = props.prefixCls,
          locale = props.locale,
          contentRender = props.contentRender,
          cellRender = props.cellRender;
      var monthsEls = months.map(function (month, index) {
        var tds = month.map(function (monthData) {
          var _classNameMap;

          var disabled = false;

          if (props.disabledDate) {
            var testValue = value.clone();
            testValue.month(monthData.value);
            disabled = props.disabledDate(testValue);
          }

          var classNameMap = (_classNameMap = {}, (0, _defineProperty2["default"])(_classNameMap, "".concat(prefixCls, "-cell"), 1), (0, _defineProperty2["default"])(_classNameMap, "".concat(prefixCls, "-cell-disabled"), disabled), (0, _defineProperty2["default"])(_classNameMap, "".concat(prefixCls, "-selected-cell"), monthData.value === currentMonth), (0, _defineProperty2["default"])(_classNameMap, "".concat(prefixCls, "-current-cell"), today.year() === value.year() && monthData.value === today.month()), _classNameMap);
          var cellEl;

          if (cellRender) {
            var currentValue = value.clone();
            currentValue.month(monthData.value);
            cellEl = cellRender(currentValue, locale);
          } else {
            var content;

            if (contentRender) {
              var _currentValue = value.clone();

              _currentValue.month(monthData.value);

              content = contentRender(_currentValue, locale);
            } else {
              content = monthData.content;
            }

            cellEl = /*#__PURE__*/_react["default"].createElement("a", {
              className: "".concat(prefixCls, "-month")
            }, content);
          }

          return /*#__PURE__*/_react["default"].createElement("td", {
            role: "gridcell",
            key: monthData.value,
            onClick: disabled ? null : chooseMonth.bind(_this2, monthData.value),
            title: monthData.title,
            className: (0, _classnames["default"])(classNameMap)
          }, cellEl);
        });
        return /*#__PURE__*/_react["default"].createElement("tr", {
          key: index,
          role: "row"
        }, tds);
      });
      return /*#__PURE__*/_react["default"].createElement("table", {
        className: "".concat(prefixCls, "-table"),
        cellSpacing: "0",
        role: "grid"
      }, /*#__PURE__*/_react["default"].createElement("tbody", {
        className: "".concat(prefixCls, "-tbody")
      }, monthsEls));
    }
  }]);
  return MonthTable;
}(_react.Component);

exports["default"] = MonthTable;
(0, _defineProperty2["default"])(MonthTable, "defaultProps", {
  onSelect: _noop["default"]
});
//# sourceMappingURL=MonthTable.js.map
