UNPKG

10.2 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';
23import Header from './Header';
24import Combobox from './Combobox';
25
26function noop() {}
27
28function generateOptions(length, disabledOptions, hideDisabledOptions) {
29 var step = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : 1;
30 var arr = [];
31
32 for (var value = 0; value < length; value += step) {
33 if (!disabledOptions || disabledOptions.indexOf(value) < 0 || !hideDisabledOptions) {
34 arr.push(value);
35 }
36 }
37
38 return arr;
39}
40
41function toNearestValidTime(time, hourOptions, minuteOptions, secondOptions) {
42 var hour = hourOptions.slice().sort(function (a, b) {
43 return Math.abs(time.hour() - a) - Math.abs(time.hour() - b);
44 })[0];
45 var minute = minuteOptions.slice().sort(function (a, b) {
46 return Math.abs(time.minute() - a) - Math.abs(time.minute() - b);
47 })[0];
48 var second = secondOptions.slice().sort(function (a, b) {
49 return Math.abs(time.second() - a) - Math.abs(time.second() - b);
50 })[0];
51 return moment("".concat(hour, ":").concat(minute, ":").concat(second), 'HH:mm:ss');
52}
53
54var Panel =
55/*#__PURE__*/
56function (_Component) {
57 _inherits(Panel, _Component);
58
59 function Panel(props) {
60 var _this;
61
62 _classCallCheck(this, Panel);
63
64 _this = _possibleConstructorReturn(this, _getPrototypeOf(Panel).call(this, props));
65
66 _defineProperty(_assertThisInitialized(_this), "onChange", function (newValue) {
67 var onChange = _this.props.onChange;
68
69 _this.setState({
70 value: newValue
71 });
72
73 onChange(newValue);
74 });
75
76 _defineProperty(_assertThisInitialized(_this), "onAmPmChange", function (ampm) {
77 var onAmPmChange = _this.props.onAmPmChange;
78 onAmPmChange(ampm);
79 });
80
81 _defineProperty(_assertThisInitialized(_this), "onCurrentSelectPanelChange", function (currentSelectPanel) {
82 _this.setState({
83 currentSelectPanel: currentSelectPanel
84 });
85 });
86
87 _defineProperty(_assertThisInitialized(_this), "disabledHours", function () {
88 var _this$props = _this.props,
89 use12Hours = _this$props.use12Hours,
90 disabledHours = _this$props.disabledHours;
91 var disabledOptions = disabledHours();
92
93 if (use12Hours && Array.isArray(disabledOptions)) {
94 if (_this.isAM()) {
95 disabledOptions = disabledOptions.filter(function (h) {
96 return h < 12;
97 }).map(function (h) {
98 return h === 0 ? 12 : h;
99 });
100 } else {
101 disabledOptions = disabledOptions.map(function (h) {
102 return h === 12 ? 12 : h - 12;
103 });
104 }
105 }
106
107 return disabledOptions;
108 });
109
110 _this.state = {
111 value: props.value
112 };
113 return _this;
114 }
115
116 _createClass(Panel, [{
117 key: "componentWillReceiveProps",
118 value: function componentWillReceiveProps(nextProps) {
119 var value = nextProps.value;
120
121 if (value) {
122 this.setState({
123 value: value
124 });
125 }
126 }
127 }, {
128 key: "close",
129 // https://github.com/ant-design/ant-design/issues/5829
130 value: function close() {
131 var onEsc = this.props.onEsc;
132 onEsc();
133 }
134 }, {
135 key: "isAM",
136 value: function isAM() {
137 var defaultOpenValue = this.props.defaultOpenValue;
138 var value = this.state.value;
139 var realValue = value || defaultOpenValue;
140 return realValue.hour() >= 0 && realValue.hour() < 12;
141 }
142 }, {
143 key: "render",
144 value: function render() {
145 var _this$props2 = this.props,
146 prefixCls = _this$props2.prefixCls,
147 className = _this$props2.className,
148 placeholder = _this$props2.placeholder,
149 disabledMinutes = _this$props2.disabledMinutes,
150 disabledSeconds = _this$props2.disabledSeconds,
151 hideDisabledOptions = _this$props2.hideDisabledOptions,
152 showHour = _this$props2.showHour,
153 showMinute = _this$props2.showMinute,
154 showSecond = _this$props2.showSecond,
155 format = _this$props2.format,
156 defaultOpenValue = _this$props2.defaultOpenValue,
157 clearText = _this$props2.clearText,
158 onEsc = _this$props2.onEsc,
159 addon = _this$props2.addon,
160 use12Hours = _this$props2.use12Hours,
161 focusOnOpen = _this$props2.focusOnOpen,
162 onKeyDown = _this$props2.onKeyDown,
163 hourStep = _this$props2.hourStep,
164 minuteStep = _this$props2.minuteStep,
165 secondStep = _this$props2.secondStep,
166 inputReadOnly = _this$props2.inputReadOnly,
167 clearIcon = _this$props2.clearIcon;
168 var _this$state = this.state,
169 value = _this$state.value,
170 currentSelectPanel = _this$state.currentSelectPanel;
171 var disabledHourOptions = this.disabledHours();
172 var disabledMinuteOptions = disabledMinutes(value ? value.hour() : null);
173 var disabledSecondOptions = disabledSeconds(value ? value.hour() : null, value ? value.minute() : null);
174 var hourOptions = generateOptions(24, disabledHourOptions, hideDisabledOptions, hourStep);
175 var minuteOptions = generateOptions(60, disabledMinuteOptions, hideDisabledOptions, minuteStep);
176 var secondOptions = generateOptions(60, disabledSecondOptions, hideDisabledOptions, secondStep);
177 var validDefaultOpenValue = toNearestValidTime(defaultOpenValue, hourOptions, minuteOptions, secondOptions);
178 return React.createElement("div", {
179 className: classNames(className, "".concat(prefixCls, "-inner"))
180 }, React.createElement(Header, {
181 clearText: clearText,
182 prefixCls: prefixCls,
183 defaultOpenValue: validDefaultOpenValue,
184 value: value,
185 currentSelectPanel: currentSelectPanel,
186 onEsc: onEsc,
187 format: format,
188 placeholder: placeholder,
189 hourOptions: hourOptions,
190 minuteOptions: minuteOptions,
191 secondOptions: secondOptions,
192 disabledHours: this.disabledHours,
193 disabledMinutes: disabledMinutes,
194 disabledSeconds: disabledSeconds,
195 onChange: this.onChange,
196 focusOnOpen: focusOnOpen,
197 onKeyDown: onKeyDown,
198 inputReadOnly: inputReadOnly,
199 clearIcon: clearIcon
200 }), React.createElement(Combobox, {
201 prefixCls: prefixCls,
202 value: value,
203 defaultOpenValue: validDefaultOpenValue,
204 format: format,
205 onChange: this.onChange,
206 onAmPmChange: this.onAmPmChange,
207 showHour: showHour,
208 showMinute: showMinute,
209 showSecond: showSecond,
210 hourOptions: hourOptions,
211 minuteOptions: minuteOptions,
212 secondOptions: secondOptions,
213 disabledHours: this.disabledHours,
214 disabledMinutes: disabledMinutes,
215 disabledSeconds: disabledSeconds,
216 onCurrentSelectPanelChange: this.onCurrentSelectPanelChange,
217 use12Hours: use12Hours,
218 isAM: this.isAM()
219 }), addon(this));
220 }
221 }]);
222
223 return Panel;
224}(Component);
225
226_defineProperty(Panel, "propTypes", {
227 clearText: PropTypes.string,
228 prefixCls: PropTypes.string,
229 className: PropTypes.string,
230 defaultOpenValue: PropTypes.object,
231 value: PropTypes.object,
232 placeholder: PropTypes.string,
233 format: PropTypes.string,
234 inputReadOnly: PropTypes.bool,
235 disabledHours: PropTypes.func,
236 disabledMinutes: PropTypes.func,
237 disabledSeconds: PropTypes.func,
238 hideDisabledOptions: PropTypes.bool,
239 onChange: PropTypes.func,
240 onAmPmChange: PropTypes.func,
241 onEsc: PropTypes.func,
242 showHour: PropTypes.bool,
243 showMinute: PropTypes.bool,
244 showSecond: PropTypes.bool,
245 use12Hours: PropTypes.bool,
246 hourStep: PropTypes.number,
247 minuteStep: PropTypes.number,
248 secondStep: PropTypes.number,
249 addon: PropTypes.func,
250 focusOnOpen: PropTypes.bool,
251 onKeyDown: PropTypes.func,
252 clearIcon: PropTypes.node
253});
254
255_defineProperty(Panel, "defaultProps", {
256 prefixCls: 'rc-time-picker-panel',
257 onChange: noop,
258 disabledHours: noop,
259 disabledMinutes: noop,
260 disabledSeconds: noop,
261 defaultOpenValue: moment(),
262 use12Hours: false,
263 addon: noop,
264 onKeyDown: noop,
265 onAmPmChange: noop,
266 inputReadOnly: false
267});
268
269export default Panel;
\No newline at end of file