choerodon-ui
Version:
An enterprise-class UI design language and React-based implementation
785 lines (676 loc) • 21.6 kB
JavaScript
import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
import _objectSpread from "@babel/runtime/helpers/objectSpread2";
import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
import _createClass from "@babel/runtime/helpers/createClass";
import _get from "@babel/runtime/helpers/get";
import _inherits from "@babel/runtime/helpers/inherits";
import _possibleConstructorReturn from "@babel/runtime/helpers/possibleConstructorReturn";
import _getPrototypeOf from "@babel/runtime/helpers/getPrototypeOf";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
var _viewComponents;
function _createSuper(Derived) {
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;
}
}
return function () {
var Super = _getPrototypeOf(Derived),
result;
if (isNativeReflectConstruct()) {
var NewTarget = _getPrototypeOf(this).constructor;
result = Reflect.construct(Super, arguments, NewTarget);
} else {
result = Super.apply(this, arguments);
}
return _possibleConstructorReturn(this, result);
};
}
import { __decorate } from "tslib";
import { createElement } from 'react';
import PropTypes from 'prop-types';
import moment, { isMoment } from 'moment';
import isPlainObject from 'lodash/isPlainObject';
import isString from 'lodash/isString';
import isNil from 'lodash/isNil';
import omit from 'lodash/omit';
import noop from 'lodash/noop';
import { observer } from 'mobx-react';
import { action, computed, isArrayLike, observable, runInAction } from 'mobx';
import KeyCode from '../../../es/_util/KeyCode';
import warning from '../../../es/_util/warning';
import TriggerField from '../trigger-field/TriggerField';
import DaysView from './DaysView';
import DateTimesView from './DateTimesView';
import WeeksView from './WeeksView';
import TimesView from './TimesView';
import MonthsView from './MonthsView';
import YearsView from './YearsView';
import DecadeYearsView from './DecadeYearsView';
import autobind from '../_util/autobind';
import { ViewMode } from './enum';
import { stopEvent } from '../_util/EventManager';
import { $l } from '../locale-context';
import isSame from '../_util/isSame';
var viewComponents = (_viewComponents = {}, _defineProperty(_viewComponents, ViewMode.decade, DecadeYearsView), _defineProperty(_viewComponents, ViewMode.year, YearsView), _defineProperty(_viewComponents, ViewMode.month, MonthsView), _defineProperty(_viewComponents, ViewMode.date, DaysView), _defineProperty(_viewComponents, ViewMode.dateTime, DateTimesView), _defineProperty(_viewComponents, ViewMode.week, WeeksView), _defineProperty(_viewComponents, ViewMode.time, TimesView), _viewComponents);
var DatePicker =
/*#__PURE__*/
function (_TriggerField) {
_inherits(DatePicker, _TriggerField);
var _super = _createSuper(DatePicker);
function DatePicker() {
_classCallCheck(this, DatePicker);
return _super.apply(this, arguments);
}
_createClass(DatePicker, [{
key: "getOtherProps",
value: function getOtherProps() {
return omit(_get(_getPrototypeOf(DatePicker.prototype), "getOtherProps", this).call(this), ['mode', 'filter', 'cellRenderer', 'maxLength', 'minLength']);
}
}, {
key: "getDefaultViewMode",
value: function getDefaultViewMode() {
var mode = this.props.mode;
if (mode === ViewMode.decade || mode === undefined) {
return ViewMode.date;
}
return mode;
}
}, {
key: "getPopupContent",
value: function getPopupContent() {
var _this = this;
var mode = this.getViewMode();
return createElement(viewComponents[mode], {
ref: function ref(node) {
return _this.view = node;
},
date: this.getSelectedDate(),
mode: this.getDefaultViewMode(),
disabledNow: !this.isValidNowDate(this.getSelectedDate()),
renderer: this.getCellRenderer(mode),
onSelect: this.handleSelect,
onSelectedDateChange: this.handleSelectedDateChange,
onViewModeChange: this.handelViewModeChange,
isValidDate: this.isValidDate,
format: this.getDateFormat(),
step: this.getProp('step') || {},
renderExtraFooter: this.getProp('renderExtraFooter'),
extraFooterPlacement: this.getProp('extraFooterPlacement') || 'bottom'
});
}
}, {
key: "getCellRenderer",
value: function getCellRenderer(mode) {
var _this$props$cellRende = this.props.cellRenderer,
cellRenderer = _this$props$cellRende === void 0 ? noop : _this$props$cellRende;
return cellRenderer(mode);
}
}, {
key: "getTriggerIconFont",
value: function getTriggerIconFont() {
return 'date_range';
}
}, {
key: "getFieldType",
value: function getFieldType() {
return viewComponents[this.getDefaultViewMode()].type;
}
}, {
key: "getViewMode",
value: function getViewMode() {
var _this$mode = this.mode,
mode = _this$mode === void 0 ? this.getDefaultViewMode() : _this$mode;
return mode;
}
}, {
key: "toMoment",
value: function toMoment(item) {
var field = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : this.field;
var noCheck = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
if (isNil(item)) {
return undefined;
}
if (isMoment(item)) {
return item;
}
warning(noCheck, "DatePicker: The value of DatePicker is not moment.");
var format = this.getDateFormat(field);
if (item instanceof Date) {
item = moment(item).format(format);
}
return moment(item, format);
}
}, {
key: "checkMoment",
value: function checkMoment(item) {
return this.toMoment(item);
}
}, {
key: "compare",
value: function compare(oldValue, newValue) {
return isSame(this.momentToTimestamp(oldValue), this.momentToTimestamp(newValue));
}
}, {
key: "afterSetValue",
value: function afterSetValue() {
_get(_getPrototypeOf(DatePicker.prototype), "afterSetValue", this).call(this);
this.setText(undefined);
}
}, {
key: "momentToTimestamp",
value: function momentToTimestamp(value) {
if (isMoment(value)) {
return moment(value).valueOf();
}
return value;
} // processValue(value: any): ReactNode {
// return super.processValue(this.checkMoment(value));
// }
}, {
key: "getSelectedDate",
value: function getSelectedDate() {
var range = this.range,
multiple = this.multiple,
rangeTarget = this.rangeTarget,
rangeValue = this.rangeValue;
var selectedDate = this.selectedDate || range && !multiple && rangeTarget !== undefined && rangeValue && rangeValue[rangeTarget] || !multiple && this.getValue();
if (isMoment(selectedDate) && selectedDate.isValid()) {
return selectedDate.clone();
}
return this.getValidDate(moment().startOf('d'));
}
}, {
key: "getLimit",
value: function getLimit(minOrMax) {
var limit = this.getProp(minOrMax);
if (!isNil(limit)) {
var record = this.record;
if (record && isString(limit)) {
var field = record.getField(limit);
if (field) {
var value = record.get(limit);
if (value) {
var momentValue = this.toMoment(value, field, true);
if (momentValue) {
return this.getLimitWithType(momentValue.clone(), minOrMax);
}
}
return undefined;
}
}
return this.getLimitWithType(moment(limit), minOrMax);
}
}
}, {
key: "getLimitWithType",
value: function getLimitWithType(limit, minOrMax) {
if (minOrMax === 'min') {
return limit.startOf('d');
}
return limit.endOf('d');
}
}, {
key: "getPopupStyleFromAlign",
value: function getPopupStyleFromAlign() {
return undefined;
}
}, {
key: "handleSelectedDateChange",
value: function handleSelectedDateChange(selectedDate, mode) {
if (this.isUnderRange(selectedDate, mode)) {
this.changeSelectedDate(selectedDate);
}
}
}, {
key: "handelViewModeChange",
value: function handelViewModeChange(mode) {
var _this2 = this;
runInAction(function () {
_this2.mode = mode;
});
}
}, {
key: "handlePopupAnimateAppear",
value: function handlePopupAnimateAppear() {}
}, {
key: "handlePopupAnimateEnd",
value: function handlePopupAnimateEnd(key, exists) {
var _this3 = this;
if (!exists && key === 'align') {
runInAction(function () {
_this3.selectedDate = undefined;
_this3.mode = undefined;
});
}
}
}, {
key: "handleSelect",
value: function handleSelect(date, expand) {
if (this.multiple && this.isSelected(date)) {
this.unChoose(date);
} else {
this.choose(date, expand);
}
}
}, {
key: "handleKeyDown",
value: function handleKeyDown(e) {
if (!this.isDisabled() && !this.isReadOnly()) {
var el = this.popup ? this.view || this : this;
switch (e.keyCode) {
case KeyCode.RIGHT:
el.handleKeyDownRight(e);
break;
case KeyCode.LEFT:
el.handleKeyDownLeft(e);
break;
case KeyCode.DOWN:
el.handleKeyDownDown(e);
break;
case KeyCode.UP:
el.handleKeyDownUp(e);
break;
case KeyCode.END:
el.handleKeyDownEnd(e);
break;
case KeyCode.HOME:
el.handleKeyDownHome(e);
break;
case KeyCode.PAGE_UP:
el.handleKeyDownPageUp(e);
break;
case KeyCode.PAGE_DOWN:
el.handleKeyDownPageDown(e);
break;
case KeyCode.ENTER:
el.handleKeyDownEnter(e);
break;
case KeyCode.TAB:
this.handleKeyDownTab();
break;
case KeyCode.ESC:
this.handleKeyDownEsc(e);
break;
case KeyCode.SPACE:
this.handleKeyDownSpace(e);
break;
default:
}
}
_get(_getPrototypeOf(DatePicker.prototype), "handleKeyDown", this).call(this, e);
}
}, {
key: "handleKeyDownHome",
value: function handleKeyDownHome(e) {
if (!this.multiple && !this.editable) {
stopEvent(e);
this.choose(this.getSelectedDate().startOf('M'));
}
}
}, {
key: "handleKeyDownEnd",
value: function handleKeyDownEnd(e) {
if (!this.multiple && !this.editable) {
stopEvent(e);
this.choose(this.getSelectedDate().endOf('M'));
}
}
}, {
key: "handleKeyDownLeft",
value: function handleKeyDownLeft(e) {
if (!this.multiple && !this.editable) {
stopEvent(e);
this.choose(this.getSelectedDate().subtract(1, 'd'));
}
}
}, {
key: "handleKeyDownRight",
value: function handleKeyDownRight(e) {
if (!this.multiple && !this.editable) {
stopEvent(e);
this.choose(this.getSelectedDate().add(1, 'd'));
}
}
}, {
key: "handleKeyDownUp",
value: function handleKeyDownUp(e) {
if (!this.multiple && !this.editable) {
stopEvent(e);
this.choose(this.getSelectedDate().subtract(1, 'w'));
}
}
}, {
key: "handleKeyDownDown",
value: function handleKeyDownDown(e) {
if (this.multiple) {
this.expand();
} else if (!this.editable) {
stopEvent(e);
this.choose(this.getSelectedDate().add(1, 'w'));
}
}
}, {
key: "handleKeyDownPageUp",
value: function handleKeyDownPageUp(e) {
if (!this.multiple && !this.editable) {
stopEvent(e);
this.choose(this.getSelectedDate().subtract(1, e.altKey ? 'y' : 'M'));
}
}
}, {
key: "handleKeyDownPageDown",
value: function handleKeyDownPageDown(e) {
if (!this.multiple && !this.editable) {
stopEvent(e);
this.choose(this.getSelectedDate().add(1, e.altKey ? 'y' : 'M'));
}
}
}, {
key: "handleKeyDownEnter",
value: function handleKeyDownEnter(_e) {
if (!this.multiple && !this.editable) {
this.choose(this.getSelectedDate());
}
}
}, {
key: "handleKeyDownEsc",
value: function handleKeyDownEsc(e) {
if (this.popup) {
e.preventDefault();
this.collapse();
}
}
}, {
key: "handleKeyDownTab",
value: function handleKeyDownTab() {// this.collapse();
}
}, {
key: "handleKeyDownSpace",
value: function handleKeyDownSpace(e) {
e.preventDefault();
if (!this.popup) {
this.expand();
}
}
}, {
key: "handleEnterDown",
value: function handleEnterDown(e) {
_get(_getPrototypeOf(DatePicker.prototype), "handleEnterDown", this).call(this, e);
if (this.multiple && this.range) {
this.setRangeTarget(0);
this.beginRange();
this.expand();
}
}
}, {
key: "syncValueOnBlur",
value: function syncValueOnBlur(value) {
if (value) {
this.prepareSetValue(this.checkMoment(value));
} else if (!this.multiple) {
this.setValue(this.emptyValue);
}
}
}, {
key: "getValueKey",
value: function getValueKey(v) {
if (isArrayLike(v)) {
return v.map(this.getValueKey, this).join(',');
}
if (isMoment(v)) {
return v.format();
}
return v;
}
}, {
key: "changeSelectedDate",
value: function changeSelectedDate(selectedDate) {
this.selectedDate = this.getValidDate(selectedDate);
}
}, {
key: "isSelected",
value: function isSelected(date) {
return this.getValues().some(function (value) {
return date.isSame(value);
});
}
}, {
key: "unChoose",
value: function unChoose(date) {
this.removeValue(date, -1);
}
/**
*
* @param date 返回的时间
* @param expand 是否保持时间选择器的展开
*/
}, {
key: "choose",
value: function choose(date, expand) {
date = this.getValidDate(date);
this.prepareSetValue(date);
this.changeSelectedDate(date);
if (this.range ? this.rangeTarget === 1 : !this.multiple) {
if (!expand) {
this.collapse();
}
}
if (this.range && this.rangeTarget === 0 && this.popup && !expand) {
this.setRangeTarget(1);
}
}
}, {
key: "setRangeTarget",
value: function setRangeTarget(target) {
if (target !== undefined && target !== this.rangeTarget) {
this.expand();
}
this.selectedDate = undefined;
_get(_getPrototypeOf(DatePicker.prototype), "setRangeTarget", this).call(this, target);
}
}, {
key: "getValidDate",
value: function getValidDate(date) {
var min = this.min,
max = this.max;
if (min && date.isSameOrBefore(min)) {
date = min;
} else if (max && date.isSameOrAfter(max)) {
date = max;
}
return date;
}
}, {
key: "isLowerRange",
value: function isLowerRange(m1, m2) {
return m1.isBefore(m2);
}
}, {
key: "isUnderRange",
value: function isUnderRange(date, mode) {
var min = this.min,
max = this.max;
if (min || max) {
var start = (min || date).clone();
var end = (max || date).clone();
switch (mode || this.getViewMode()) {
case ViewMode.month:
start = start.startOf('M');
end = end.endOf('M');
break;
case ViewMode.year:
start = start.startOf('y');
end = end.endOf('y');
break;
case ViewMode.decade:
start = start.startOf('y').subtract(start.year() % 10, 'y').startOf('d');
end = end.endOf('y').add(9 - end.year() % 10, 'y').endOf('d');
break;
case ViewMode.dateTime:
start = start.startOf('d');
end = end.endOf('d');
break;
default:
}
return date.isBetween(start, end, undefined, '[]');
}
return true;
}
}, {
key: "isValidDate",
value: function isValidDate(currentDate, selected) {
var filter = this.props.filter;
var isValid = this.isUnderRange(currentDate);
if (isValid && filter) {
return filter(currentDate, selected, this.getViewMode());
}
return isValid;
}
}, {
key: "isValidNowDate",
value: function isValidNowDate(selected) {
var filter = this.props.filter;
var isValid = this.isUnderRange(moment());
if (isValid && filter) {
return filter(moment(), selected);
}
return isValid;
}
}, {
key: "getValidatorProps",
value: function getValidatorProps() {
var min = this.min,
max = this.max;
return omit(_objectSpread({}, _get(_getPrototypeOf(DatePicker.prototype), "getValidatorProps", this).call(this), {
min: min,
max: max,
format: this.getDateFormat()
}), ['maxLength', 'minLength']);
}
}, {
key: "value",
get: function get() {
var _this4 = this;
var value = this.observableProps.value;
var range = this.range;
if (isArrayLike(value)) {
return value.map(function (item) {
if (isArrayLike(item)) {
return item.map(_this4.checkMoment, _this4);
}
return _this4.checkMoment(item);
});
}
if (isArrayLike(range)) {
if (isPlainObject(value)) {
var _ref;
var _range = _slicedToArray(range, 2),
start = _range[0],
end = _range[1];
return _ref = {}, _defineProperty(_ref, start, this.checkMoment(value[start])), _defineProperty(_ref, end, this.checkMoment(value[end])), _ref;
}
}
return this.checkMoment(value);
},
set: function set(value) {
var _this5 = this;
runInAction(function () {
_this5.observableProps.value = value;
});
}
}, {
key: "defaultValidationMessages",
get: function get() {
var label = this.getProp('label');
return {
valueMissing: $l('DatePicker', label ? 'value_missing' : 'value_missing_no_label', {
label: label
})
};
}
}, {
key: "editable",
get: function get() {
var mode = this.getViewMode();
return _get(_getPrototypeOf(DatePicker.prototype), "editable", this) && mode !== ViewMode.week;
}
}, {
key: "min",
get: function get() {
return this.getLimit('min');
}
}, {
key: "max",
get: function get() {
return this.getLimit('max');
}
}]);
return DatePicker;
}(TriggerField);
DatePicker.displayName = 'DatePicker';
DatePicker.propTypes = _objectSpread({
/**
* 日期格式,如 `YYYY-MM-DD HH:mm:ss`
*/
format: PropTypes.string,
/**
* 显示模式date|dateTime|time|year|month|week
*/
mode: PropTypes.string,
/**
* 单元格渲染
*/
cellRenderer: PropTypes.func,
/**
* 日期过滤
*/
filter: PropTypes.func,
/**
* 最小日期
*/
min: PropTypes.any,
/**
* 最大日期
*/
max: PropTypes.any,
/**
* 时间步距
*/
step: PropTypes.shape({
hour: PropTypes.number,
minute: PropTypes.number,
second: PropTypes.number
})
}, TriggerField.propTypes);
DatePicker.defaultProps = _objectSpread({}, TriggerField.defaultProps, {
suffixCls: 'calendar-picker',
mode: ViewMode.date
});
__decorate([computed], DatePicker.prototype, "value", null);
__decorate([computed], DatePicker.prototype, "defaultValidationMessages", null);
__decorate([computed], DatePicker.prototype, "editable", null);
__decorate([computed], DatePicker.prototype, "min", null);
__decorate([computed], DatePicker.prototype, "max", null);
__decorate([observable], DatePicker.prototype, "selectedDate", void 0);
__decorate([observable], DatePicker.prototype, "mode", void 0);
__decorate([autobind], DatePicker.prototype, "handleSelectedDateChange", null);
__decorate([autobind], DatePicker.prototype, "handelViewModeChange", null);
__decorate([autobind], DatePicker.prototype, "handlePopupAnimateEnd", null);
__decorate([autobind], DatePicker.prototype, "handleSelect", null);
__decorate([autobind], DatePicker.prototype, "handleKeyDown", null);
__decorate([action], DatePicker.prototype, "handleEnterDown", null);
__decorate([action], DatePicker.prototype, "changeSelectedDate", null);
__decorate([action], DatePicker.prototype, "setRangeTarget", null);
__decorate([autobind], DatePicker.prototype, "isUnderRange", null);
__decorate([autobind], DatePicker.prototype, "isValidDate", null);
__decorate([autobind], DatePicker.prototype, "isValidNowDate", null);
DatePicker = __decorate([observer], DatePicker);
export default DatePicker;
//# sourceMappingURL=DatePicker.js.map