polestar-ui-kit
Version:
## Install
300 lines (226 loc) • 13.3 kB
JavaScript
;
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); }
require("core-js/modules/es.array.is-array");
require("core-js/modules/es.date.to-string");
require("core-js/modules/es.function.name");
require("core-js/modules/es.object.get-prototype-of");
require("core-js/modules/es.object.set-prototype-of");
require("core-js/modules/es.object.to-string");
require("core-js/modules/es.reflect.construct");
require("core-js/modules/es.regexp.to-string");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = void 0;
var _react = _interopRequireWildcard(require("react"));
var _propTypes = _interopRequireDefault(require("prop-types"));
var _classnames = _interopRequireDefault(require("classnames"));
var _moment = _interopRequireDefault(require("moment"));
var _i18n = require("../i18n");
var _ = require(".");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function _getRequireWildcardCache() { return cache; }; return cache; }
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
function _iterableToArrayLimit(arr, i) { if (typeof Symbol === "undefined" || !(Symbol.iterator in Object(arr))) return; var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
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 _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); }
var DateTimeRangePicker = function (_Component) {
_inherits(DateTimeRangePicker, _Component);
var _super = _createSuper(DateTimeRangePicker);
function DateTimeRangePicker(props) {
var _this;
_classCallCheck(this, DateTimeRangePicker);
_this = _super.call(this, props);
_this.onDateChange = function (date) {
console.log('onDateChange', date);
var _this$props = _this.props,
name = _this$props['data-__field'].name,
form = _this$props.form,
onChange = _this$props.onChange;
_this.date = date && new Date(date.year(), date.month(), date.date());
var dates = _this.getDates();
if (form && form.hasOwnProperty('setFieldsValue')) {
form.setFieldsValue(_defineProperty({}, name, dates));
}
if (onChange) {
onChange(dates);
}
};
_this.onStartTimeChange = function (time) {
console.log('onStartTimeChange', time);
var _this$props2 = _this.props,
name = _this$props2['data-__field'].name,
form = _this$props2.form,
onChange = _this$props2.onChange;
_this.startTime = _this.timeToJson(time);
var dates = _this.getDates();
if (form && form.hasOwnProperty('setFieldsValue')) {
form.setFieldsValue(_defineProperty({}, name, dates));
}
if (onChange) {
onChange(dates);
}
};
_this.onEndTimeChange = function (time) {
console.log('onEndTimeChange', time);
var _this$props3 = _this.props,
name = _this$props3['data-__field'].name,
form = _this$props3.form,
onChange = _this$props3.onChange;
_this.endTime = _this.timeToJson(time);
var dates = _this.getDates();
if (form && form.hasOwnProperty('setFieldsValue')) {
form.setFieldsValue(_defineProperty({}, name, dates));
}
if (onChange) {
onChange(dates);
}
};
_this.getDates = function () {
var _assertThisInitialize = _assertThisInitialized(_this),
date = _assertThisInitialize.date,
startTime = _assertThisInitialize.startTime,
endTime = _assertThisInitialize.endTime;
var startDate = (0, _moment["default"])(date);
var endDate = (0, _moment["default"])(date);
if (startTime) {
startDate.hour(startTime.h).minute(startTime.m).second(startTime.s);
}
if (endTime) {
endDate.hour(endTime.h).minute(endTime.m).second(endTime.s);
}
if (_this.props.showEndTime === false) {
return [startDate];
}
return [startDate, endDate];
};
_this.timeToJson = function (time) {
if (!time) return null;
var timeFormat = _this.props.timeFormat;
return timeFormat === 'HH:mm:ss' ? {
h: time.hour(),
m: time.minute(),
s: time.second()
} : {
h: time.hour(),
m: time.minute()
};
};
_this.isReadOnly = function (key) {
var readOnly = _this.props.readOnly;
if (typeof readOnly === 'boolean') {
return readOnly;
}
if (typeof readOnly === 'undefined' || typeof readOnly[key] !== 'boolean') {
return undefined;
}
return readOnly[key];
};
var initialValue = _this.props['data-__meta'].initialValue;
if (initialValue && Array.isArray(initialValue)) {
var _initialValue = _slicedToArray(initialValue, 1),
date = _initialValue[0];
_this.date = date && new Date(date.year(), date.month(), date.date());
_this.startTime = _this.timeToJson(initialValue[0]);
_this.endTime = initialValue[1] && _this.timeToJson(initialValue[1]);
} else {
_this.date = null;
_this.startTime = null;
_this.endTime = null;
}
return _this;
}
_createClass(DateTimeRangePicker, [{
key: "render",
value: function render() {
var _this$props4 = this.props,
className = _this$props4.className,
timeFormat = _this$props4.timeFormat,
initialValue = _this$props4['data-__meta'].initialValue,
required = _this$props4.required,
disabled = _this$props4.disabled,
showEndTime = _this$props4.showEndTime;
return _react["default"].createElement("div", {
className: (0, _classnames["default"])('ps-datetimerange-picker', className)
}, _react["default"].createElement("div", {
className: "ps-datetimerange-date-picker"
}, _react["default"].createElement(_.DatePicker, {
defaultValue: initialValue && initialValue[0],
showTime: false,
required: required,
disabled: disabled,
readOnly: this.isReadOnly('date'),
onChange: this.onDateChange
})), _react["default"].createElement("div", {
className: "ps-datetimerange-time-picker"
}, _react["default"].createElement(_.TimePicker, {
defaultValue: initialValue && initialValue[0],
format: timeFormat,
style: {
width: '100px'
},
required: required,
disabled: disabled,
readOnly: this.isReadOnly('time'),
onChange: this.onStartTimeChange
}), showEndTime === false ? null : _react["default"].createElement(_react["default"].Fragment, null, ' ', "~", ' ', _react["default"].createElement(_.TimePicker, {
defaultValue: initialValue && initialValue[1],
format: timeFormat,
style: {
width: '100px'
},
required: required,
disabled: disabled,
readOnly: this.isReadOnly('time'),
onChange: this.onEndTimeChange
}))));
}
}]);
return DateTimeRangePicker;
}(_react.Component);
DateTimeRangePicker.displayName = 'DateTimeRangePicker';
DateTimeRangePicker.propTypes = {
className: _propTypes["default"].string,
timeFormat: _propTypes["default"].string,
required: _propTypes["default"].bool,
disabled: _propTypes["default"].bool,
readOnly: _propTypes["default"].oneOfType([_propTypes["default"].bool, _propTypes["default"].shape({
date: _propTypes["default"].bool,
time: _propTypes["default"].bool
})]),
showEndTime: _propTypes["default"].bool,
onChange: _propTypes["default"].func
};
DateTimeRangePicker.defaultProps = {
timeFormat: 'HH:mm:ss',
required: false,
showEndTime: true,
'data-__meta': {},
'data-__field': {}
};
DateTimeRangePicker.validator = function (rule, value, callback) {
var _value = _slicedToArray(value, 2),
startDate = _value[0],
endDate = _value[1];
if (startDate && endDate && startDate.isAfter(endDate)) {
callback((0, _i18n.translate)('uikit:message.validate_startTime_before_endTime'));
} else {
callback();
}
};
var _default = DateTimeRangePicker;
exports["default"] = _default;