UNPKG

12.8 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports["default"] = void 0;
7
8var _react = _interopRequireWildcard(require("react"));
9
10var _propTypes = _interopRequireDefault(require("prop-types"));
11
12var _moment = _interopRequireDefault(require("moment"));
13
14var _classnames = _interopRequireDefault(require("classnames"));
15
16var _reactLifecyclesCompat = require("react-lifecycles-compat");
17
18var _Header = _interopRequireDefault(require("./Header"));
19
20var _Combobox = _interopRequireDefault(require("./Combobox"));
21
22function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
23
24function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj["default"] = obj; return newObj; } }
25
26function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }
27
28function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(source, true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(source).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
29
30function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
31
32function _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); } }
33
34function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
35
36function _possibleConstructorReturn(self, call) { if (call && (typeof call === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }
37
38function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
39
40function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
41
42function _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); }
43
44function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
45
46function _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; }
47
48function noop() {}
49
50function generateOptions(length, disabledOptions, hideDisabledOptions) {
51 var step = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : 1;
52 var arr = [];
53
54 for (var value = 0; value < length; value += step) {
55 if (!disabledOptions || disabledOptions.indexOf(value) < 0 || !hideDisabledOptions) {
56 arr.push(value);
57 }
58 }
59
60 return arr;
61}
62
63function toNearestValidTime(time, hourOptions, minuteOptions, secondOptions) {
64 var hour = hourOptions.slice().sort(function (a, b) {
65 return Math.abs(time.hour() - a) - Math.abs(time.hour() - b);
66 })[0];
67 var minute = minuteOptions.slice().sort(function (a, b) {
68 return Math.abs(time.minute() - a) - Math.abs(time.minute() - b);
69 })[0];
70 var second = secondOptions.slice().sort(function (a, b) {
71 return Math.abs(time.second() - a) - Math.abs(time.second() - b);
72 })[0];
73 return (0, _moment["default"])("".concat(hour, ":").concat(minute, ":").concat(second), 'HH:mm:ss');
74}
75
76var Panel =
77/*#__PURE__*/
78function (_Component) {
79 _inherits(Panel, _Component);
80
81 function Panel() {
82 var _getPrototypeOf2;
83
84 var _this;
85
86 _classCallCheck(this, Panel);
87
88 for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
89 args[_key] = arguments[_key];
90 }
91
92 _this = _possibleConstructorReturn(this, (_getPrototypeOf2 = _getPrototypeOf(Panel)).call.apply(_getPrototypeOf2, [this].concat(args)));
93
94 _defineProperty(_assertThisInitialized(_this), "state", {});
95
96 _defineProperty(_assertThisInitialized(_this), "onChange", function (newValue) {
97 var onChange = _this.props.onChange;
98
99 _this.setState({
100 value: newValue
101 });
102
103 onChange(newValue);
104 });
105
106 _defineProperty(_assertThisInitialized(_this), "onAmPmChange", function (ampm) {
107 var onAmPmChange = _this.props.onAmPmChange;
108 onAmPmChange(ampm);
109 });
110
111 _defineProperty(_assertThisInitialized(_this), "onCurrentSelectPanelChange", function (currentSelectPanel) {
112 _this.setState({
113 currentSelectPanel: currentSelectPanel
114 });
115 });
116
117 _defineProperty(_assertThisInitialized(_this), "disabledHours", function () {
118 var _this$props = _this.props,
119 use12Hours = _this$props.use12Hours,
120 disabledHours = _this$props.disabledHours;
121 var disabledOptions = disabledHours();
122
123 if (use12Hours && Array.isArray(disabledOptions)) {
124 if (_this.isAM()) {
125 disabledOptions = disabledOptions.filter(function (h) {
126 return h < 12;
127 }).map(function (h) {
128 return h === 0 ? 12 : h;
129 });
130 } else {
131 disabledOptions = disabledOptions.map(function (h) {
132 return h === 12 ? 12 : h - 12;
133 });
134 }
135 }
136
137 return disabledOptions;
138 });
139
140 return _this;
141 }
142
143 _createClass(Panel, [{
144 key: "close",
145 // https://github.com/ant-design/ant-design/issues/5829
146 value: function close() {
147 var onEsc = this.props.onEsc;
148 onEsc();
149 }
150 }, {
151 key: "isAM",
152 value: function isAM() {
153 var defaultOpenValue = this.props.defaultOpenValue;
154 var value = this.state.value;
155 var realValue = value || defaultOpenValue;
156 return realValue.hour() >= 0 && realValue.hour() < 12;
157 }
158 }, {
159 key: "render",
160 value: function render() {
161 var _this$props2 = this.props,
162 prefixCls = _this$props2.prefixCls,
163 className = _this$props2.className,
164 placeholder = _this$props2.placeholder,
165 disabledMinutes = _this$props2.disabledMinutes,
166 disabledSeconds = _this$props2.disabledSeconds,
167 hideDisabledOptions = _this$props2.hideDisabledOptions,
168 showHour = _this$props2.showHour,
169 showMinute = _this$props2.showMinute,
170 showSecond = _this$props2.showSecond,
171 format = _this$props2.format,
172 defaultOpenValue = _this$props2.defaultOpenValue,
173 clearText = _this$props2.clearText,
174 onEsc = _this$props2.onEsc,
175 addon = _this$props2.addon,
176 use12Hours = _this$props2.use12Hours,
177 focusOnOpen = _this$props2.focusOnOpen,
178 onKeyDown = _this$props2.onKeyDown,
179 hourStep = _this$props2.hourStep,
180 minuteStep = _this$props2.minuteStep,
181 secondStep = _this$props2.secondStep,
182 inputReadOnly = _this$props2.inputReadOnly,
183 clearIcon = _this$props2.clearIcon;
184 var _this$state = this.state,
185 value = _this$state.value,
186 currentSelectPanel = _this$state.currentSelectPanel;
187 var disabledHourOptions = this.disabledHours();
188 var disabledMinuteOptions = disabledMinutes(value ? value.hour() : null);
189 var disabledSecondOptions = disabledSeconds(value ? value.hour() : null, value ? value.minute() : null);
190 var hourOptions = generateOptions(24, disabledHourOptions, hideDisabledOptions, hourStep);
191 var minuteOptions = generateOptions(60, disabledMinuteOptions, hideDisabledOptions, minuteStep);
192 var secondOptions = generateOptions(60, disabledSecondOptions, hideDisabledOptions, secondStep);
193 var validDefaultOpenValue = toNearestValidTime(defaultOpenValue, hourOptions, minuteOptions, secondOptions);
194 return _react["default"].createElement("div", {
195 className: (0, _classnames["default"])(className, "".concat(prefixCls, "-inner"))
196 }, _react["default"].createElement(_Header["default"], {
197 clearText: clearText,
198 prefixCls: prefixCls,
199 defaultOpenValue: validDefaultOpenValue,
200 value: value,
201 currentSelectPanel: currentSelectPanel,
202 onEsc: onEsc,
203 format: format,
204 placeholder: placeholder,
205 hourOptions: hourOptions,
206 minuteOptions: minuteOptions,
207 secondOptions: secondOptions,
208 disabledHours: this.disabledHours,
209 disabledMinutes: disabledMinutes,
210 disabledSeconds: disabledSeconds,
211 onChange: this.onChange,
212 focusOnOpen: focusOnOpen,
213 onKeyDown: onKeyDown,
214 inputReadOnly: inputReadOnly,
215 clearIcon: clearIcon
216 }), _react["default"].createElement(_Combobox["default"], {
217 prefixCls: prefixCls,
218 value: value,
219 defaultOpenValue: validDefaultOpenValue,
220 format: format,
221 onChange: this.onChange,
222 onAmPmChange: this.onAmPmChange,
223 showHour: showHour,
224 showMinute: showMinute,
225 showSecond: showSecond,
226 hourOptions: hourOptions,
227 minuteOptions: minuteOptions,
228 secondOptions: secondOptions,
229 disabledHours: this.disabledHours,
230 disabledMinutes: disabledMinutes,
231 disabledSeconds: disabledSeconds,
232 onCurrentSelectPanelChange: this.onCurrentSelectPanelChange,
233 use12Hours: use12Hours,
234 onEsc: onEsc,
235 isAM: this.isAM()
236 }), addon(this));
237 }
238 }], [{
239 key: "getDerivedStateFromProps",
240 value: function getDerivedStateFromProps(props, state) {
241 if ('value' in props) {
242 return _objectSpread({}, state, {
243 value: props.value
244 });
245 }
246
247 return null;
248 }
249 }]);
250
251 return Panel;
252}(_react.Component);
253
254_defineProperty(Panel, "propTypes", {
255 clearText: _propTypes["default"].string,
256 prefixCls: _propTypes["default"].string,
257 className: _propTypes["default"].string,
258 defaultOpenValue: _propTypes["default"].object,
259 value: _propTypes["default"].object,
260 placeholder: _propTypes["default"].string,
261 format: _propTypes["default"].string,
262 inputReadOnly: _propTypes["default"].bool,
263 disabledHours: _propTypes["default"].func,
264 disabledMinutes: _propTypes["default"].func,
265 disabledSeconds: _propTypes["default"].func,
266 hideDisabledOptions: _propTypes["default"].bool,
267 onChange: _propTypes["default"].func,
268 onAmPmChange: _propTypes["default"].func,
269 onEsc: _propTypes["default"].func,
270 showHour: _propTypes["default"].bool,
271 showMinute: _propTypes["default"].bool,
272 showSecond: _propTypes["default"].bool,
273 use12Hours: _propTypes["default"].bool,
274 hourStep: _propTypes["default"].number,
275 minuteStep: _propTypes["default"].number,
276 secondStep: _propTypes["default"].number,
277 addon: _propTypes["default"].func,
278 focusOnOpen: _propTypes["default"].bool,
279 onKeyDown: _propTypes["default"].func,
280 clearIcon: _propTypes["default"].node
281});
282
283_defineProperty(Panel, "defaultProps", {
284 prefixCls: 'rc-time-picker-panel',
285 onChange: noop,
286 disabledHours: noop,
287 disabledMinutes: noop,
288 disabledSeconds: noop,
289 defaultOpenValue: (0, _moment["default"])(),
290 use12Hours: false,
291 addon: noop,
292 onKeyDown: noop,
293 onAmPmChange: noop,
294 inputReadOnly: false
295});
296
297(0, _reactLifecyclesCompat.polyfill)(Panel);
298var _default = Panel;
299exports["default"] = _default;
\No newline at end of file