UNPKG

yylib-quick-mobile

Version:

yylib-quick-mobile

291 lines (248 loc) 11 kB
'use strict'; var _calendar = require('antd-mobile/lib/calendar'); var _calendar2 = _interopRequireDefault(_calendar); var _list = require('antd-mobile/lib/list'); var _list2 = _interopRequireDefault(_list); var _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; }; var _createClass = 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); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); require('antd-mobile/lib/calendar/style/css'); require('antd-mobile/lib/list/style/css'); var _react = require('react'); var _react2 = _interopRequireDefault(_react); var _FunctionUtil = require('../../utils/FunctionUtil'); var _formatUtils = require('../../utils/formatUtils'); var _YYIcon = require('../icon/YYIcon'); var _YYIcon2 = _interopRequireDefault(_YYIcon); var _classnames = require('classnames'); var _classnames2 = _interopRequireDefault(_classnames); require('./YYCalendar.less'); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } 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; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _possibleConstructorReturn(self, call) { if (!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) { if (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 } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } var Item = _list2.default.Item; var Brief = Item.Brief; var format = 'yyyy/MM/dd hh:mm:ss'; var dateformat = 'yyyy/MM/dd'; var YYCalendar = function (_React$Component) { _inherits(YYCalendar, _React$Component); function YYCalendar(props) { _classCallCheck(this, YYCalendar); var _this = _possibleConstructorReturn(this, (YYCalendar.__proto__ || Object.getPrototypeOf(YYCalendar)).call(this, props)); _this.onCancel = function () { _this.setState({ show: false }); }; _this.onConfirm = function (startDateTime, endDateTime) { var _this$props = _this.props, type = _this$props.type, pickTime = _this$props.pickTime; var formatValue = dateformat; if (pickTime) { formatValue = format; } else { formatValue = dateformat; } var fields = {}; var dateValue = ''; if (startDateTime) { dateValue = (0, _formatUtils.formatDate)(startDateTime, formatValue); } if (type == 'range' && endDateTime) { dateValue = dateValue + "-" + (0, _formatUtils.formatDate)(endDateTime, formatValue); } _this.setState({ show: false, startDateTime: startDateTime, endDateTime: endDateTime, dateValue: dateValue }, function () { var _value; var value = (_value = {}, _defineProperty(_value, _this.props.startField, startDateTime), _defineProperty(_value, _this.props.endField, endDateTime), _value); fields[_this.props.field] = { dirty: true, errors: undefined, name: _this.props.field, touched: true, validating: true, value: value }; _this.props.form.setFields(fields); if ((0, _FunctionUtil.isFunction)(_this.props.onConfirm)) _this.props.onConfirm(startDateTime, endDateTime); }); }; _this.onSelect = function (Date, dates) { if ((0, _FunctionUtil.isFunction)(_this.props.onSelect)) _this.props.onSelect(Date, dates); }; _this.onSelectHasDisableDate = function (dates) { if ((0, _FunctionUtil.isFunction)(_this.props.onSelectHasDisableDate)) _this.props.onSelectHasDisableDate(dates); }; _this.onClick = function () { _this.setState({ show: true }); }; _this.validateCallBack = function (rule, value, callback) { if ((0, _FunctionUtil.isFunction)(_this.props.validateCallBack)) { _this.props.validateCallBack(rule, value, callback); callback(); } else { callback(); } }; _this.componentWillReceiveProps = function (nextProps) { var getFieldValue = _this.props.form.getFieldValue; var _this$props2 = _this.props, field = _this$props2.field, type = _this$props2.type, pickTime = _this$props2.pickTime; var valueField = getFieldValue(field); var formatValue = dateformat; if (pickTime) { formatValue = format; } else { formatValue = dateformat; } var dateValue = ''; if (valueField) { if (valueField[_this.props.startField]) { dateValue = typeof valueField[_this.props.startField] === 'string' ? valueField[_this.props.startField] : (0, _formatUtils.formatDate)(valueField[_this.props.startField], formatValue); } if (type == 'range' && valueField[_this.props.endField]) { dateValue = typeof valueField[_this.props.endField] === 'string' ? dateValue + "-" + valueField[_this.props.endField] : dateValue + "-" + (0, _formatUtils.formatDate)(valueField[_this.props.endField], formatValue); } _this.setState({ dateValue: dateValue, defaultValue: [valueField[_this.props.startField], type == 'range' ? valueField[_this.props.endField] : ''] }); } }; _this.state = { show: false, startDateTime: '', endDateTime: '', dateValue: '', defaultValue: '', defaultTimeValue: '' }; return _this; } _createClass(YYCalendar, [{ key: 'render', value: function render() { var getFieldProps = this.props.form.getFieldProps; var _props = this.props, pickTime = _props.pickTime, showIcon = _props.showIcon, icon = _props.icon, iconColor = _props.iconColor, visible = _props.visible, showShortcut = _props.showShortcut, trigger = _props.trigger, label = _props.label, field = _props.field, type = _props.type, infiniteOpt = _props.infiniteOpt, initalMonths = _props.initalMonths, value = _props.value, required = _props.required, rowSize = _props.rowSize, defaultDate = _props.defaultDate, maxDate = _props.maxDate, minDate = _props.minDate, disabled = _props.disabled, arrow = _props.arrow, extra = _props.extra, enterDirection = _props.enterDirection, wrap = _props.wrap; var wrapClz = (0, _classnames2.default)('yy-calendar-picker', this.props.className); var itemWrapClz = (0, _classnames2.default)('yy-calendar-picker-item', !visible && 'hidden', this.props.className); var requiredMsg = '必填项' + label + '未填写'; var _state = this.state, dateValue = _state.dateValue, show = _state.show, defaultValue = _state.defaultValue, defaultTimeValue = _state.defaultTimeValue; return _react2.default.createElement( _list2.default, null, _react2.default.createElement(_calendar2.default, _extends({ className: wrapClz }, (0, _FunctionUtil.isFunction)(getFieldProps) ? getFieldProps(field, { initialValue: typeof value === 'string' ? new Date(value) : value, rules: [{ required: required, message: requiredMsg }, { validator: this.validateCallBack }], trigger: trigger, valuePropName: 'value' }) : null, { visible: show, onCancel: this.onCancel, onConfirm: this.onConfirm, onSelect: this.onSelect, onSelectHasDisableDate: this.onSelectHasDisableDate, pickTime: pickTime, showShortcut: showShortcut, defaultDate: defaultDate ? new Date(defaultDate) : new Date(), minDate: minDate ? new Date(minDate) : new Date('2000-01-01'), maxDate: maxDate ? new Date(maxDate) : new Date('2030-01-01'), type: type, rowSize: rowSize, enterDirection: enterDirection, infiniteOpt: infiniteOpt, initalMonths: initalMonths, defaultValue: defaultValue, defaultTimeValue: defaultTimeValue })), _react2.default.createElement( Item, { className: itemWrapClz, arrow: disabled ? "empty" : arrow, onClick: !disabled ? this.onClick : this.onCancel, wrap: wrap, extra: dateValue ? _react2.default.createElement( 'span', { title: dateValue }, dateValue ) : extra }, showIcon && _react2.default.createElement(_YYIcon2.default, { type: icon, color: iconColor, style: { float: 'left', paddingRight: '8px', paddingTop: '3px' }, size: 'xs' }), required ? _react2.default.createElement( 'span', { className: 'yy-label-required' }, label ) : label ) ); } }]); return YYCalendar; }(_react2.default.Component); YYCalendar.defaultProps = { startField: "startDateTime", endField: "endDateTime", value: null, defaultDate: '', minDate: '', maxDate: '', field: 'calendar', icon: "Home", iconColor: "red", label: '日历', type: 'one', rowSize: 'normal', enterDirection: 'vertical', extra: '请选择', initalMonths: 6, show: false, pickTime: false, showIcon: false, visible: true, showShortcut: false, infiniteOpt: false, required: false, trigger: 'onChange', disabled: false, arrow: 'horizontal', wrap: false }; module.exports = YYCalendar;