UNPKG

8.35 kBJavaScriptView Raw
1function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
2
3function _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); } }
4
5function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
6
7function _possibleConstructorReturn(self, call) { if (call && (typeof call === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }
8
9function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
10
11function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
12
13function _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); }
14
15function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
16
17function _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; }
18
19import React, { Component } from 'react';
20import PropTypes from 'prop-types';
21import moment from 'moment';
22import classNames from 'classnames';
23
24var Header =
25/*#__PURE__*/
26function (_Component) {
27 _inherits(Header, _Component);
28
29 function Header(props) {
30 var _this;
31
32 _classCallCheck(this, Header);
33
34 _this = _possibleConstructorReturn(this, _getPrototypeOf(Header).call(this, props));
35
36 _defineProperty(_assertThisInitialized(_this), "onInputChange", function (event) {
37 var str = event.target.value;
38
39 _this.setState({
40 str: str
41 });
42
43 var _this$props = _this.props,
44 format = _this$props.format,
45 hourOptions = _this$props.hourOptions,
46 minuteOptions = _this$props.minuteOptions,
47 secondOptions = _this$props.secondOptions,
48 disabledHours = _this$props.disabledHours,
49 disabledMinutes = _this$props.disabledMinutes,
50 disabledSeconds = _this$props.disabledSeconds,
51 onChange = _this$props.onChange;
52
53 if (str) {
54 var originalValue = _this.props.value;
55
56 var value = _this.getProtoValue().clone();
57
58 var parsed = moment(str, format, true);
59
60 if (!parsed.isValid()) {
61 _this.setState({
62 invalid: true
63 });
64
65 return;
66 }
67
68 value.hour(parsed.hour()).minute(parsed.minute()).second(parsed.second()); // if time value not allowed, response warning.
69
70 if (hourOptions.indexOf(value.hour()) < 0 || minuteOptions.indexOf(value.minute()) < 0 || secondOptions.indexOf(value.second()) < 0) {
71 _this.setState({
72 invalid: true
73 });
74
75 return;
76 } // if time value is disabled, response warning.
77
78
79 var disabledHourOptions = disabledHours();
80 var disabledMinuteOptions = disabledMinutes(value.hour());
81 var disabledSecondOptions = disabledSeconds(value.hour(), value.minute());
82
83 if (disabledHourOptions && disabledHourOptions.indexOf(value.hour()) >= 0 || disabledMinuteOptions && disabledMinuteOptions.indexOf(value.minute()) >= 0 || disabledSecondOptions && disabledSecondOptions.indexOf(value.second()) >= 0) {
84 _this.setState({
85 invalid: true
86 });
87
88 return;
89 }
90
91 if (originalValue) {
92 if (originalValue.hour() !== value.hour() || originalValue.minute() !== value.minute() || originalValue.second() !== value.second()) {
93 // keep other fields for rc-calendar
94 var changedValue = originalValue.clone();
95 changedValue.hour(value.hour());
96 changedValue.minute(value.minute());
97 changedValue.second(value.second());
98 onChange(changedValue);
99 }
100 } else if (originalValue !== value) {
101 onChange(value);
102 }
103 } else {
104 onChange(null);
105 }
106
107 _this.setState({
108 invalid: false
109 });
110 });
111
112 _defineProperty(_assertThisInitialized(_this), "onKeyDown", function (e) {
113 var _this$props2 = _this.props,
114 onEsc = _this$props2.onEsc,
115 onKeyDown = _this$props2.onKeyDown;
116
117 if (e.keyCode === 27) {
118 onEsc();
119 }
120
121 onKeyDown(e);
122 });
123
124 var _value = props.value,
125 _format = props.format;
126 _this.state = {
127 str: _value && _value.format(_format) || '',
128 invalid: false
129 };
130 return _this;
131 }
132
133 _createClass(Header, [{
134 key: "componentDidMount",
135 value: function componentDidMount() {
136 var _this2 = this;
137
138 var focusOnOpen = this.props.focusOnOpen;
139
140 if (focusOnOpen) {
141 // Wait one frame for the panel to be positioned before focusing
142 var requestAnimationFrame = window.requestAnimationFrame || window.setTimeout;
143 requestAnimationFrame(function () {
144 _this2.refInput.focus();
145
146 _this2.refInput.select();
147 });
148 }
149 }
150 }, {
151 key: "componentWillReceiveProps",
152 value: function componentWillReceiveProps(nextProps) {
153 var value = nextProps.value,
154 format = nextProps.format;
155 this.setState({
156 str: value && value.format(format) || '',
157 invalid: false
158 });
159 }
160 }, {
161 key: "getProtoValue",
162 value: function getProtoValue() {
163 var _this$props3 = this.props,
164 value = _this$props3.value,
165 defaultOpenValue = _this$props3.defaultOpenValue;
166 return value || defaultOpenValue;
167 }
168 }, {
169 key: "getInput",
170 value: function getInput() {
171 var _this3 = this;
172
173 var _this$props4 = this.props,
174 prefixCls = _this$props4.prefixCls,
175 placeholder = _this$props4.placeholder,
176 inputReadOnly = _this$props4.inputReadOnly;
177 var _this$state = this.state,
178 invalid = _this$state.invalid,
179 str = _this$state.str;
180 var invalidClass = invalid ? "".concat(prefixCls, "-input-invalid") : '';
181 return React.createElement("input", {
182 className: classNames("".concat(prefixCls, "-input"), invalidClass),
183 ref: function ref(_ref) {
184 _this3.refInput = _ref;
185 },
186 onKeyDown: this.onKeyDown,
187 value: str,
188 placeholder: placeholder,
189 onChange: this.onInputChange,
190 readOnly: !!inputReadOnly
191 });
192 }
193 }, {
194 key: "render",
195 value: function render() {
196 var prefixCls = this.props.prefixCls;
197 return React.createElement("div", {
198 className: "".concat(prefixCls, "-input-wrap")
199 }, this.getInput());
200 }
201 }]);
202
203 return Header;
204}(Component);
205
206_defineProperty(Header, "propTypes", {
207 format: PropTypes.string,
208 prefixCls: PropTypes.string,
209 disabledDate: PropTypes.func,
210 placeholder: PropTypes.string,
211 clearText: PropTypes.string,
212 value: PropTypes.object,
213 inputReadOnly: PropTypes.bool,
214 hourOptions: PropTypes.array,
215 minuteOptions: PropTypes.array,
216 secondOptions: PropTypes.array,
217 disabledHours: PropTypes.func,
218 disabledMinutes: PropTypes.func,
219 disabledSeconds: PropTypes.func,
220 onChange: PropTypes.func,
221 onEsc: PropTypes.func,
222 defaultOpenValue: PropTypes.object,
223 currentSelectPanel: PropTypes.string,
224 focusOnOpen: PropTypes.bool,
225 onKeyDown: PropTypes.func,
226 clearIcon: PropTypes.node
227});
228
229_defineProperty(Header, "defaultProps", {
230 inputReadOnly: false
231});
232
233export default Header;
\No newline at end of file