rc-time-picker
Version:
238 lines (194 loc) • 8.5 kB
JavaScript
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 _possibleConstructorReturn(self, call) { if (call && (typeof call === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }
function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
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 _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; }
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import moment from 'moment';
import classNames from 'classnames';
var Header =
/*#__PURE__*/
function (_Component) {
_inherits(Header, _Component);
function Header(props) {
var _this;
_classCallCheck(this, Header);
_this = _possibleConstructorReturn(this, _getPrototypeOf(Header).call(this, props));
_defineProperty(_assertThisInitialized(_this), "onInputChange", function (event) {
var str = event.target.value;
_this.setState({
str: str
});
var _this$props = _this.props,
format = _this$props.format,
hourOptions = _this$props.hourOptions,
minuteOptions = _this$props.minuteOptions,
secondOptions = _this$props.secondOptions,
disabledHours = _this$props.disabledHours,
disabledMinutes = _this$props.disabledMinutes,
disabledSeconds = _this$props.disabledSeconds,
onChange = _this$props.onChange;
if (str) {
var originalValue = _this.props.value;
var value = _this.getProtoValue().clone();
var parsed = moment(str, format, true);
if (!parsed.isValid()) {
_this.setState({
invalid: true
});
return;
}
value.hour(parsed.hour()).minute(parsed.minute()).second(parsed.second()); // if time value not allowed, response warning.
if (hourOptions.indexOf(value.hour()) < 0 || minuteOptions.indexOf(value.minute()) < 0 || secondOptions.indexOf(value.second()) < 0) {
_this.setState({
invalid: true
});
return;
} // if time value is disabled, response warning.
var disabledHourOptions = disabledHours();
var disabledMinuteOptions = disabledMinutes(value.hour());
var disabledSecondOptions = disabledSeconds(value.hour(), value.minute());
if (disabledHourOptions && disabledHourOptions.indexOf(value.hour()) >= 0 || disabledMinuteOptions && disabledMinuteOptions.indexOf(value.minute()) >= 0 || disabledSecondOptions && disabledSecondOptions.indexOf(value.second()) >= 0) {
_this.setState({
invalid: true
});
return;
}
if (originalValue) {
if (originalValue.hour() !== value.hour() || originalValue.minute() !== value.minute() || originalValue.second() !== value.second()) {
// keep other fields for rc-calendar
var changedValue = originalValue.clone();
changedValue.hour(value.hour());
changedValue.minute(value.minute());
changedValue.second(value.second());
onChange(changedValue);
}
} else if (originalValue !== value) {
onChange(value);
}
} else {
onChange(null);
}
_this.setState({
invalid: false
});
});
_defineProperty(_assertThisInitialized(_this), "onKeyDown", function (e) {
var _this$props2 = _this.props,
onEsc = _this$props2.onEsc,
onKeyDown = _this$props2.onKeyDown;
if (e.keyCode === 27) {
onEsc();
}
onKeyDown(e);
});
var _value = props.value,
_format = props.format;
_this.state = {
str: _value && _value.format(_format) || '',
invalid: false
};
return _this;
}
_createClass(Header, [{
key: "componentDidMount",
value: function componentDidMount() {
var _this2 = this;
var focusOnOpen = this.props.focusOnOpen;
if (focusOnOpen) {
// Wait one frame for the panel to be positioned before focusing
var requestAnimationFrame = window.requestAnimationFrame || window.setTimeout;
requestAnimationFrame(function () {
_this2.refInput.focus();
_this2.refInput.select();
});
}
}
}, {
key: "componentDidUpdate",
value: function componentDidUpdate(prevProps) {
var _this$props3 = this.props,
value = _this$props3.value,
format = _this$props3.format;
if (value !== prevProps.value) {
// eslint-disable-next-line react/no-did-update-set-state
this.setState({
str: value && value.format(format) || '',
invalid: false
});
}
}
}, {
key: "getProtoValue",
value: function getProtoValue() {
var _this$props4 = this.props,
value = _this$props4.value,
defaultOpenValue = _this$props4.defaultOpenValue;
return value || defaultOpenValue;
}
}, {
key: "getInput",
value: function getInput() {
var _this3 = this;
var _this$props5 = this.props,
prefixCls = _this$props5.prefixCls,
placeholder = _this$props5.placeholder,
inputReadOnly = _this$props5.inputReadOnly;
var _this$state = this.state,
invalid = _this$state.invalid,
str = _this$state.str;
var invalidClass = invalid ? "".concat(prefixCls, "-input-invalid") : '';
return React.createElement("input", {
className: classNames("".concat(prefixCls, "-input"), invalidClass),
ref: function ref(_ref) {
_this3.refInput = _ref;
},
onKeyDown: this.onKeyDown,
value: str,
placeholder: placeholder,
onChange: this.onInputChange,
readOnly: !!inputReadOnly
});
}
}, {
key: "render",
value: function render() {
var prefixCls = this.props.prefixCls;
return React.createElement("div", {
className: "".concat(prefixCls, "-input-wrap")
}, this.getInput());
}
}]);
return Header;
}(Component);
_defineProperty(Header, "propTypes", {
format: PropTypes.string,
prefixCls: PropTypes.string,
disabledDate: PropTypes.func,
placeholder: PropTypes.string,
clearText: PropTypes.string,
value: PropTypes.object,
inputReadOnly: PropTypes.bool,
hourOptions: PropTypes.array,
minuteOptions: PropTypes.array,
secondOptions: PropTypes.array,
disabledHours: PropTypes.func,
disabledMinutes: PropTypes.func,
disabledSeconds: PropTypes.func,
onChange: PropTypes.func,
onEsc: PropTypes.func,
defaultOpenValue: PropTypes.object,
currentSelectPanel: PropTypes.string,
focusOnOpen: PropTypes.bool,
onKeyDown: PropTypes.func,
clearIcon: PropTypes.node
});
_defineProperty(Header, "defaultProps", {
inputReadOnly: false
});
export default Header;