index.js
5.62 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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
"use strict";
var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));
var _typeof2 = _interopRequireDefault(require("@babel/runtime/helpers/typeof"));
var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
var React = _interopRequireWildcard(require("react"));
var _classnames = _interopRequireDefault(require("classnames"));
var _KeyCode = _interopRequireDefault(require("rc-util/lib/KeyCode"));
var _DatePanel = _interopRequireDefault(require("../DatePanel"));
var _TimePanel = _interopRequireDefault(require("../TimePanel"));
var _miscUtil = require("../../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 = (0, _miscUtil.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 = (0, _slicedToArray2.default)(_React$useState, 2),
activePanel = _React$useState2[0],
setActivePanel = _React$useState2[1];
var dateOperationRef = React.useRef({});
var timeOperationRef = React.useRef({});
var timeProps = (0, _typeof2.default)(showTime) === 'object' ? (0, _objectSpread2.default)({}, 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.default.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.default.LEFT, _KeyCode.default.RIGHT, _KeyCode.default.UP, _KeyCode.default.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: (0, _classnames.default)(panelPrefixCls, (0, _defineProperty2.default)({}, "".concat(panelPrefixCls, "-active"), activePanel))
}, /*#__PURE__*/React.createElement(_DatePanel.default, Object.assign({}, props, {
operationRef: dateOperationRef,
active: activePanel === 'date',
onSelect: function onSelect(date) {
onInternalSelect(setTime(generateConfig, date, showTime && (0, _typeof2.default)(showTime) === 'object' ? showTime.defaultValue : null), 'date');
}
})), /*#__PURE__*/React.createElement(_TimePanel.default, Object.assign({}, props, {
format: undefined
}, timeProps, disabledTimes, {
defaultValue: undefined,
operationRef: timeOperationRef,
active: activePanel === 'time',
onSelect: function onSelect(date) {
onInternalSelect(date, 'time');
}
})));
}
var _default = DatetimePanel;
exports.default = _default;