index.js
4.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _typeof from "@babel/runtime/helpers/esm/typeof";
import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
import * as React from 'react';
import classNames from 'classnames';
import KeyCode from "rc-util/es/KeyCode";
import DatePanel from '../DatePanel';
import TimePanel from '../TimePanel';
import { tuple } from '../../utils/miscUtil';
function setTime(generateConfig, date, defaultDate) {
if (!defaultDate) {
return date;
}
var newDate = date;
newDate = generateConfig.setHour(newDate, generateConfig.getHour(defaultDate));
newDate = generateConfig.setMinute(newDate, generateConfig.getMinute(defaultDate));
newDate = generateConfig.setSecond(newDate, generateConfig.getSecond(defaultDate));
return newDate;
}
var ACTIVE_PANEL = tuple('date', 'time');
function DatetimePanel(props) {
var prefixCls = props.prefixCls,
operationRef = props.operationRef,
generateConfig = props.generateConfig,
value = props.value,
defaultValue = props.defaultValue,
disabledTime = props.disabledTime,
showTime = props.showTime,
onSelect = props.onSelect;
var panelPrefixCls = "".concat(prefixCls, "-datetime-panel");
var _React$useState = React.useState(null),
_React$useState2 = _slicedToArray(_React$useState, 2),
activePanel = _React$useState2[0],
setActivePanel = _React$useState2[1];
var dateOperationRef = React.useRef({});
var timeOperationRef = React.useRef({});
var timeProps = _typeof(showTime) === 'object' ? _objectSpread({}, showTime) : {}; // ======================= Keyboard =======================
function getNextActive(offset) {
var activeIndex = ACTIVE_PANEL.indexOf(activePanel) + offset;
var nextActivePanel = ACTIVE_PANEL[activeIndex] || null;
return nextActivePanel;
}
var onBlur = function onBlur(e) {
if (timeOperationRef.current.onBlur) {
timeOperationRef.current.onBlur(e);
}
setActivePanel(null);
};
operationRef.current = {
onKeyDown: function onKeyDown(event) {
// Switch active panel
if (event.which === KeyCode.TAB) {
var nextActivePanel = getNextActive(event.shiftKey ? -1 : 1);
setActivePanel(nextActivePanel);
if (nextActivePanel) {
event.preventDefault();
}
return true;
} // Operate on current active panel
if (activePanel) {
var ref = activePanel === 'date' ? dateOperationRef : timeOperationRef;
if (ref.current && ref.current.onKeyDown) {
ref.current.onKeyDown(event);
}
return true;
} // Switch first active panel if operate without panel
if ([KeyCode.LEFT, KeyCode.RIGHT, KeyCode.UP, KeyCode.DOWN].includes(event.which)) {
setActivePanel('date');
return true;
}
return false;
},
onBlur: onBlur,
onClose: onBlur
}; // ======================== Events ========================
var onInternalSelect = function onInternalSelect(date, source) {
var selectedDate = date;
if (source === 'date' && !value && timeProps.defaultValue) {
// Date with time defaultValue
selectedDate = generateConfig.setHour(selectedDate, generateConfig.getHour(timeProps.defaultValue));
selectedDate = generateConfig.setMinute(selectedDate, generateConfig.getMinute(timeProps.defaultValue));
selectedDate = generateConfig.setSecond(selectedDate, generateConfig.getSecond(timeProps.defaultValue));
} else if (source === 'time' && !value && defaultValue) {
selectedDate = generateConfig.setYear(selectedDate, generateConfig.getYear(defaultValue));
selectedDate = generateConfig.setMonth(selectedDate, generateConfig.getMonth(defaultValue));
selectedDate = generateConfig.setDate(selectedDate, generateConfig.getDate(defaultValue));
}
if (onSelect) {
onSelect(selectedDate, 'mouse');
}
}; // ======================== Render ========================
var disabledTimes = disabledTime ? disabledTime(value || null) : {};
return /*#__PURE__*/React.createElement("div", {
className: classNames(panelPrefixCls, _defineProperty({}, "".concat(panelPrefixCls, "-active"), activePanel))
}, /*#__PURE__*/React.createElement(DatePanel, Object.assign({}, props, {
operationRef: dateOperationRef,
active: activePanel === 'date',
onSelect: function onSelect(date) {
onInternalSelect(setTime(generateConfig, date, showTime && _typeof(showTime) === 'object' ? showTime.defaultValue : null), 'date');
}
})), /*#__PURE__*/React.createElement(TimePanel, Object.assign({}, props, {
format: undefined
}, timeProps, disabledTimes, {
defaultValue: undefined,
operationRef: timeOperationRef,
active: activePanel === 'time',
onSelect: function onSelect(date) {
onInternalSelect(date, 'time');
}
})));
}
export default DatetimePanel;