UNPKG

4.62 kBJavaScriptView Raw
1import moment from 'moment';
2import { KEYCODE } from '../../util';
3
4export var PANEL = {
5 TIME: 'time-panel',
6 DATE: 'date-panel'
7};
8
9export var DEFAULT_TIME_FORMAT = 'HH:mm:ss';
10
11export function isFunction(obj) {
12 return !!(obj && obj.constructor && obj.call && obj.apply);
13}
14
15/**
16 * 将 source 的 time 替换为 target 的 time
17 * @param {Object} source 输入值
18 * @param {Object} target 目标值
19 */
20export function resetValueTime(source, target) {
21 if (!moment.isMoment(source) || !moment.isMoment(target)) {
22 return source;
23 }
24 return source.clone().hour(target.hour()).minute(target.minute()).second(target.second());
25}
26
27export function formatDateValue(value, format) {
28 var val = typeof value === 'string' ? moment(value, format, false) : value;
29 if (val && moment.isMoment(val) && val.isValid()) {
30 return val;
31 }
32
33 return null;
34}
35
36export function checkDateValue(props, propName, componentName) {
37 // 支持传入 moment 对象或字符串,字符串不检测是否为日期字符串
38 if (props[propName] && !moment.isMoment(props[propName]) && typeof props[propName] !== 'string') {
39 return new Error('Invalid prop ' + propName + ' supplied to ' + componentName + '. Required a moment object or format date string!');
40 }
41}
42
43export function getDateTimeFormat(format, showTime, type) {
44 if (!format && type) {
45 format = {
46 date: 'YYYY-MM-DD',
47 month: 'YYYY-MM',
48 year: 'YYYY',
49 time: ''
50 }[type];
51 }
52 var timeFormat = showTime ? showTime.format || DEFAULT_TIME_FORMAT : '';
53 var dateTimeFormat = timeFormat ? format + ' ' + timeFormat : format;
54 return {
55 format: format,
56 timeFormat: timeFormat,
57 dateTimeFormat: dateTimeFormat
58 };
59}
60
61export function extend(source, target) {
62 for (var key in source) {
63 if (source.hasOwnProperty(key)) {
64 target[key] = source[key];
65 }
66 }
67 return target;
68}
69
70/**
71 * 监听键盘事件,操作日期字符串
72 * @param {KeyboardEvent} e 事件对象
73 * @param {Object} param1
74 * @param {String} type 类型 year month day
75 */
76export function onDateKeydown(e, _ref, type) {
77 var format = _ref.format,
78 dateInputStr = _ref.dateInputStr,
79 value = _ref.value;
80
81 if ([KEYCODE.UP, KEYCODE.DOWN, KEYCODE.PAGE_UP, KEYCODE.PAGE_DOWN].indexOf(e.keyCode) === -1) {
82 return;
83 }
84
85 if (e.altKey && [KEYCODE.PAGE_UP, KEYCODE.PAGE_DOWN].indexOf(e.keyCode) === -1 || e.controlKey || e.shiftKey) {
86 return;
87 }
88
89 var date = moment(dateInputStr, format, true);
90
91 if (date.isValid()) {
92 var stepUnit = e.altKey ? 'year' : 'month';
93 switch (e.keyCode) {
94 case KEYCODE.UP:
95 date.subtract(1, type);
96 break;
97 case KEYCODE.DOWN:
98 date.add(1, type);
99 break;
100 case KEYCODE.PAGE_UP:
101 date.subtract(1, stepUnit);
102 break;
103 case KEYCODE.PAGE_DOWN:
104 date.add(1, stepUnit);
105 break;
106 }
107 } else if (value) {
108 date = value.clone();
109 } else {
110 date = moment();
111 }
112
113 e.preventDefault();
114 return date.format(format);
115}
116
117/**
118 * 监听键盘事件,操作时间
119 * @param {KeyboardEvent} e
120 * @param {Object} param1
121 * @param {String} type second hour minute
122 */
123export function onTimeKeydown(e, _ref2, type) {
124 var format = _ref2.format,
125 timeInputStr = _ref2.timeInputStr,
126 steps = _ref2.steps,
127 value = _ref2.value;
128
129 if ([KEYCODE.UP, KEYCODE.DOWN, KEYCODE.PAGE_UP, KEYCODE.PAGE_DOWN].indexOf(e.keyCode) === -1) return;
130 if (e.altKey && [KEYCODE.PAGE_UP, KEYCODE.PAGE_DOWN].indexOf(e.keyCode) === -1 || e.controlKey || e.shiftKey) return;
131
132 var time = moment(timeInputStr, format, true);
133
134 if (time.isValid()) {
135 var stepUnit = e.altKey ? 'hour' : 'minute';
136 switch (e.keyCode) {
137 case KEYCODE.UP:
138 time.subtract(steps[type], type);
139 break;
140 case KEYCODE.DOWN:
141 time.add(steps[type], type);
142 break;
143 case KEYCODE.PAGE_UP:
144 time.subtract(steps[stepUnit], stepUnit);
145 break;
146 case KEYCODE.PAGE_DOWN:
147 time.add(steps[stepUnit], stepUnit);
148 break;
149 }
150 } else if (value) {
151 time = value.clone();
152 } else {
153 time = moment().hours(0).minutes(0).seconds(0);
154 }
155
156 e.preventDefault();
157 return time.format(format);
158}
\No newline at end of file