UNPKG

choerodon-ui

Version:

An enterprise-class UI design language and React-based implementation

237 lines (195 loc) 7.6 kB
"use strict"; var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard")["default"]; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault")["default"]; Object.defineProperty(exports, "__esModule", { value: true }); exports["default"] = void 0; var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck")); var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass")); var _assertThisInitialized2 = _interopRequireDefault(require("@babel/runtime/helpers/assertThisInitialized")); var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits")); var _createSuper2 = _interopRequireDefault(require("@babel/runtime/helpers/createSuper")); var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty")); var _react = _interopRequireWildcard(require("react")); var _moment = _interopRequireDefault(require("moment")); var Header = /*#__PURE__*/function (_Component) { (0, _inherits2["default"])(Header, _Component); var _super = (0, _createSuper2["default"])(Header); function Header(props) { var _this; (0, _classCallCheck2["default"])(this, Header); _this = _super.call(this, props); (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_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, allowEmpty = _this$props.allowEmpty; if (str) { var originalValue = _this.props.value; var value = _this.getProtoValue().clone(); var parsed = (0, _moment["default"])(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 if (allowEmpty) { onChange(null); } else { _this.setState({ invalid: true }); return; } _this.setState({ invalid: false }); }); (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "onKeyDown", function (e) { var _this$props2 = _this.props, onEsc = _this$props2.onEsc, onKeyDown = _this$props2.onKeyDown; if (e.keyCode === 27) { onEsc(); } onKeyDown(e); }); (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "onClear", function () { _this.setState({ str: '' }); _this.props.onClear(); }); var _value = props.value, _format = props.format; _this.state = { str: _value && _value.format(_format) || '', invalid: false }; return _this; } (0, _createClass2["default"])(Header, [{ key: "componentDidMount", value: function componentDidMount() { var _this2 = this; if (this.props.focusOnOpen) { // Wait one frame for the panel to be positioned before focusing var requestAnimationFrame = window.requestAnimationFrame || window.setTimeout; requestAnimationFrame(function () { _this2.refs.input.focus(); _this2.refs.input.select(); }); } } }, { key: "componentWillReceiveProps", value: function componentWillReceiveProps(nextProps) { var value = nextProps.value, format = nextProps.format; this.setState({ str: value && value.format(format) || '', invalid: false }); } }, { key: "getClearButton", value: function getClearButton() { var _this$props3 = this.props, prefixCls = _this$props3.prefixCls, allowEmpty = _this$props3.allowEmpty, clearIcon = _this$props3.clearIcon; if (!allowEmpty) { return null; } return /*#__PURE__*/_react["default"].createElement("a", { role: "button", className: "".concat(prefixCls, "-clear-btn"), title: this.props.clearText, onMouseDown: this.onClear }, clearIcon || /*#__PURE__*/_react["default"].createElement("i", { className: "".concat(prefixCls, "-clear-btn-icon") })); } }, { key: "getProtoValue", value: function getProtoValue() { return this.props.value || this.props.defaultOpenValue; } }, { key: "getInput", value: function getInput() { var _this$props4 = this.props, prefixCls = _this$props4.prefixCls, placeholder = _this$props4.placeholder, inputReadOnly = _this$props4.inputReadOnly; var _this$state = this.state, invalid = _this$state.invalid, str = _this$state.str; var invalidClass = invalid ? "".concat(prefixCls, "-input-invalid") : ''; return /*#__PURE__*/_react["default"].createElement("input", { className: "".concat(prefixCls, "-input ").concat(invalidClass), ref: "input", onKeyDown: this.onKeyDown, value: str, placeholder: placeholder, onChange: this.onInputChange, readOnly: !!inputReadOnly }); } }, { key: "render", value: function render() { var prefixCls = this.props.prefixCls; return /*#__PURE__*/_react["default"].createElement("div", { className: "".concat(prefixCls, "-input-wrap") }, this.getInput(), this.getClearButton()); } }]); return Header; }(_react.Component); (0, _defineProperty2["default"])(Header, "defaultProps", { inputReadOnly: false }); var _default = Header; exports["default"] = _default; //# sourceMappingURL=Header.js.map