UNPKG

2.08 kBJavaScriptView Raw
1import { datejs, KEYCODE } from '../../util';
2
3// 检查传入值是否为 dayjs 对象
4export function checkDayjsObj(props, propName, componentName) {
5 if (props[propName] && !datejs.isSelf(props[propName])) {
6 return new Error('Invalid prop ' + propName + ' supplied to ' + componentName + '. Required a dayjs object.');
7 }
8}
9
10// 检查传入值是否为 dayjs 对象
11export function checkDateValue(props, propName, componentName) {
12 if (props[propName] && !datejs.isSelf(props[propName]) && typeof props[propName] !== 'string') {
13 return new Error('Invalid prop ' + propName + ' supplied to ' + componentName + '. Required a dayjs object or format date string.');
14 }
15}
16
17/**
18 * 监听键盘事件,操作时间
19 * @param {KeyboardEvent} e
20 * @param {Object} param1
21 * @param {String} type second hour minute
22 */
23export function onTimeKeydown(e, _ref, type) {
24 var format = _ref.format,
25 timeInputStr = _ref.timeInputStr,
26 steps = _ref.steps,
27 value = _ref.value;
28
29 if ([KEYCODE.UP, KEYCODE.DOWN, KEYCODE.PAGE_UP, KEYCODE.PAGE_DOWN].indexOf(e.keyCode) === -1) return;
30 if (e.altKey && [KEYCODE.PAGE_UP, KEYCODE.PAGE_DOWN].indexOf(e.keyCode) === -1 || e.controlKey || e.shiftKey) return;
31
32 var time = datejs(timeInputStr, format, true);
33
34 if (time.isValid()) {
35 var stepUnit = e.altKey ? 'hour' : 'minute';
36 switch (e.keyCode) {
37 case KEYCODE.UP:
38 time = time.subtract(steps[type], type);
39 break;
40 case KEYCODE.DOWN:
41 time = time.add(steps[type], type);
42 break;
43 case KEYCODE.PAGE_UP:
44 time = time.subtract(steps[stepUnit], stepUnit);
45 break;
46 case KEYCODE.PAGE_DOWN:
47 time = time.add(steps[stepUnit], stepUnit);
48 break;
49 }
50 } else if (value) {
51 time = value.clone();
52 } else {
53 time = datejs();
54 time = time.hour(0).minute(0).second(0);
55 }
56
57 e.preventDefault();
58 return time.format(format);
59}
\No newline at end of file