all files / src/ CalendarFullHeader.jsx

70.37% Statements 76/108
44.44% Branches 24/54
65% Functions 13/20
75.58% Lines 65/86
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 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249                                        165×   12×   12×                           12×   12×                     12×         12×       12×       12×                                                                                             12×                 12× 12×                                             12×   12×           12× 12×       33×   33×                         12×   12×                                                          
'use strict';
 
Object.defineProperty(exports, "__esModule", {
  value: true
});
 
var _react = require('react');
 
var _react2 = _interopRequireDefault(_react);
 
var _propTypes = require('prop-types');
 
var _propTypes2 = _interopRequireDefault(_propTypes);
 
var _uxcoreButton = require('uxcore-button');
 
var _uxcoreButton2 = _interopRequireDefault(_uxcoreButton);
 
var _classnames2 = require('classnames');
 
var _classnames3 = _interopRequireDefault(_classnames2);
 
var _moment = require('moment');
 
var _moment2 = _interopRequireDefault(_moment);
 
var _index = require('rc-calendar/lib/util/index');
 
var _Calendar = require('./Calendar');
 
var _Calendar2 = _interopRequireDefault(_Calendar);
 
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
 
function _defaults(obj, defaults) { var keys = Object.getOwnPropertyNames(defaults); for (var i = 0; i < keys.length; i++) { var key = keys[i]; var value = Object.getOwnPropertyDescriptor(defaults, key); if (value && value.configurable && obj[key] === undefined) { Object.defineProperty(obj, key, value); } } return obj; }
 
function _defineProperty(obj, key, value) { Iif (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
 
function _classCallCheck(instance, Constructor) { Iif (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
 
function _possibleConstructorReturn(self, call) { Iif (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
 
function _inherits(subClass, superClass) { Iif (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); Eif (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : _defaults(subClass, superClass); }
 
var SWITCHERS = [{
  label: 'time',
  value: 'time'
}, {
  label: 'week',
  value: 'week'
}, {
  label: 'month',
  value: 'month'
}];
function noop() {}
 
var CalendarHeader = function (_Component) {
  _inherits(CalendarHeader, _Component);
 
  function CalendarHeader() {
    _classCallCheck(this, CalendarHeader);
 
    return _possibleConstructorReturn(this, _Component.apply(this, arguments));
  }
 
  CalendarHeader.prototype.setToday = function setToday() {
    var _props = this.props,
        onValueChange = _props.onValueChange,
        value = _props.value;
 
    var now = (0, _index.getTodayTime)(value);
    onValueChange(now);
  };
 
  CalendarHeader.prototype.getShowValue = function getShowValue() {
    var _props2 = this.props,
        value = _props2.value,
        type = _props2.type,
        format = _props2.format;
 
    if (type === 'time') {
      return (0, _moment2['default'])(value).format(format);
    }if (type === 'week') {
      var day = (0, _moment2['default'])(value).day();
      day = day === 0 ? 7 : day;
      var firstDate = (0, _moment2['default'])(value).subtract(day - 1, 'd').format(format);
      var lastDate = (0, _moment2['default'])(value).add(7 - day, 'd').format(format);
      return firstDate + ' ~ ' + lastDate;
    }
    var newFormat = format.replace(/([-/])?dd/gi, '');
    return (0, _moment2['default'])(value).format(newFormat);
  };
 
  CalendarHeader.prototype.todayElement = function todayElement() {
    var _props3 = this.props,
        showToday = _props3.showToday,
        locale = _props3.locale;
 
    return showToday ? _react2['default'].createElement(
      _uxcoreButton2['default'],
      { type: 'secondary', onClick: this.setToday.bind(this), className: 'today-btn' },
      locale.today
    ) : null;
  };
 
  CalendarHeader.prototype.handlePrev = function handlePrev() {
    var _props4 = this.props,
        type = _props4.type,
        onValueChange = _props4.onValueChange,
        value = _props4.value;
 
    var newValue = value || (0, _moment2['default'])();
    if (type === 'time') {
      newValue = newValue.subtract(1, 'd');
    } else if (type === 'week') {
      newValue = newValue.subtract(1, 'w');
    } else {
      newValue = newValue.subtract(1, 'M');
    }
    onValueChange(newValue);
  };
 
  CalendarHeader.prototype.changeType = function changeType(value) {
    var typeChange = this.props.typeChange;
 
    typeChange(value);
  };
 
  CalendarHeader.prototype.handleNext = function handleNext() {
    var _props5 = this.props,
        type = _props5.type,
        onValueChange = _props5.onValueChange,
        value = _props5.value;
 
    var newValue = value || (0, _moment2['default'])();
    if (type === 'time') {
      newValue = (0, _moment2['default'])(newValue).add(1, 'd');
    } else if (type === 'week') {
      newValue = (0, _moment2['default'])(newValue).add(1, 'w');
    } else {
      newValue = (0, _moment2['default'])(newValue).add(1, 'M');
    }
    onValueChange(newValue);
  };
 
  // 日历选择
 
 
  CalendarHeader.prototype.initCalendar = function initCalendar() {
    var _props6 = this.props,
        headerSize = _props6.headerSize,
        yearSelectOffset = _props6.yearSelectOffset,
        yearSelectTotal = _props6.yearSelectTotal,
        prefixCls = _props6.prefixCls,
        value = _props6.value,
        onValueChange = _props6.onValueChange,
        locale = _props6.locale;
 
    var showValue = this.getShowValue();
    return _react2['default'].createElement(
      'div',
      { className: prefixCls + '-date-select' },
      _react2['default'].createElement('span', { className: prefixCls + '-prev-btn', onClick: this.handlePrev.bind(this) }),
      _react2['default'].createElement('input', { value: showValue, readOnly: true, className: prefixCls + '-show-input kuma-input' }),
      _react2['default'].createElement(_Calendar2['default'], {
        showToday: false,
        showTime: false,
        allowClear: false,
        showDateInput: false,
        showSecond: false,
        yearSelectOffset: yearSelectOffset,
        yearSelectTotal: yearSelectTotal,
        size: headerSize,
        value: value,
        onSelect: onValueChange,
        hasTrigger: false,
        style: { top: '40px' }
      }),
      _react2['default'].createElement('span', { className: prefixCls + '-next-btn', onClick: this.handleNext.bind(this) })
    );
  };
 
  CalendarHeader.prototype.renderSwitcher = function renderSwitcher() {
    var _this2 = this;
 
    var _props7 = this.props,
        prefixCls = _props7.prefixCls,
        showTypeSwitch = _props7.showTypeSwitch,
        type = _props7.type,
        locale = _props7.locale;
 
    var switchCls = prefixCls + '-header-switcher';
    return showTypeSwitch ? _react2['default'].createElement(
      'span',
      { className: switchCls },
      SWITCHERS.map(function (s) {
        var _classnames;
 
        return _react2['default'].createElement(
          'span',
          {
            onClick: _this2.changeType.bind(_this2, s.value),
            key: s.label,
            className: (0, _classnames3['default'])((_classnames = {}, _defineProperty(_classnames, switchCls + '-normal', true), _defineProperty(_classnames, switchCls + '-time', s.value === 'time'), _defineProperty(_classnames, switchCls + '-week', s.value === 'week'), _defineProperty(_classnames, switchCls + '-date', s.value === 'month'), _defineProperty(_classnames, switchCls + '-focus', type === s.value), _classnames))
          },
          locale[s.label] || '日'
        );
      })
    ) : null;
  };
 
  CalendarHeader.prototype.render = function render() {
    var prefixCls = this.props.prefixCls;
 
    return _react2['default'].createElement(
      'div',
      { className: prefixCls + '-header' },
      this.todayElement(),
      this.initCalendar(),
      this.renderSwitcher()
    );
  };
 
  return CalendarHeader;
}(_react.Component);
 
CalendarHeader.propTypes = {
  yearSelectOffset: _propTypes2['default'].number,
  yearSelectTotal: _propTypes2['default'].number,
  onValueChange: _propTypes2['default'].func,
  typeChange: _propTypes2['default'].func,
  prefixCls: _propTypes2['default'].string,
  type: _propTypes2['default'].string,
  showTypeSwitch: _propTypes2['default'].bool,
  showToday: _propTypes2['default'].bool
};
CalendarHeader.defaultProps = {
  yearSelectOffset: 10,
  yearSelectTotal: 20,
  onValueChange: noop,
  showToday: true,
  type: 'time',
  showTypeSwitch: true,
  typeChange: function typeChange() {}
};
 
exports['default'] = CalendarHeader;
module.exports = exports['default'];