UNPKG

choerodon-ui

Version:

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

225 lines (187 loc) 6.79 kB
import _classCallCheck from "@babel/runtime/helpers/classCallCheck"; import _createClass from "@babel/runtime/helpers/createClass"; import _assertThisInitialized from "@babel/runtime/helpers/assertThisInitialized"; import _inherits from "@babel/runtime/helpers/inherits"; import _createSuper from "@babel/runtime/helpers/createSuper"; import _defineProperty from "@babel/runtime/helpers/defineProperty"; import React, { Component } from 'react'; import moment from 'moment'; var Header = /*#__PURE__*/function (_Component) { _inherits(Header, _Component); var _super = _createSuper(Header); function Header(props) { var _this; _classCallCheck(this, Header); _this = _super.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, allowEmpty = _this$props.allowEmpty; 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 if (allowEmpty) { onChange(null); } else { _this.setState({ invalid: true }); return; } _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); }); _defineProperty(_assertThisInitialized(_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; } _createClass(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.createElement("a", { role: "button", className: "".concat(prefixCls, "-clear-btn"), title: this.props.clearText, onMouseDown: this.onClear }, clearIcon || /*#__PURE__*/React.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.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.createElement("div", { className: "".concat(prefixCls, "-input-wrap") }, this.getInput(), this.getClearButton()); } }]); return Header; }(Component); _defineProperty(Header, "defaultProps", { inputReadOnly: false }); export default Header; //# sourceMappingURL=Header.js.map