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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
import PropTypes from '../../_util/vue-types';
import BaseMixin from '../../_util/BaseMixin';
import { getOptionProps, hasProp, getEvents, getStyle } from '../../_util/props-util';
import { cloneElement } from '../../_util/vnode';
import createChainedFunction from '../../_util/createChainedFunction';
import KeyCode from '../../_util/KeyCode';
import placements from './picker/placements';
import Trigger from '../../vc-trigger';
import moment from 'moment';
import { setTimeout } from 'timers';
function isMoment(value) {
if (Array.isArray(value)) {
return value.length === 0 || value.findIndex(function (val) {
return val === undefined || moment.isMoment(val);
}) !== -1;
} else {
return value === undefined || moment.isMoment(value);
}
}
var MomentType = PropTypes.custom(isMoment);
var Picker = {
props: {
animation: PropTypes.oneOfType([PropTypes.func, PropTypes.string]),
disabled: PropTypes.bool,
transitionName: PropTypes.string,
format: PropTypes.string,
// onChange: PropTypes.func,
// onOpenChange: PropTypes.func,
children: PropTypes.func,
getCalendarContainer: PropTypes.func,
calendar: PropTypes.any,
open: PropTypes.bool,
defaultOpen: PropTypes.bool.def(false),
prefixCls: PropTypes.string.def('rc-calendar-picker'),
placement: PropTypes.any.def('bottomLeft'),
value: PropTypes.oneOfType([MomentType, PropTypes.arrayOf(MomentType)]),
defaultValue: PropTypes.oneOfType([MomentType, PropTypes.arrayOf(MomentType)]),
align: PropTypes.object.def({}),
dropdownClassName: PropTypes.string
},
mixins: [BaseMixin],
data: function data() {
var props = this.$props;
var open = void 0;
if (hasProp(this, 'open')) {
open = props.open;
} else {
open = props.defaultOpen;
}
var value = props.value || props.defaultValue;
return {
sOpen: open,
sValue: value
};
},
watch: {
value: function value(val) {
this.setState({
sValue: val
});
},
open: function open(val) {
this.setState({
sOpen: val
});
}
},
mounted: function mounted() {
this.preSOpen = this.sOpen;
},
updated: function updated() {
if (!this.preSOpen && this.sOpen) {
// setTimeout is for making sure saveCalendarRef happen before focusCalendar
this.focusTimeout = setTimeout(this.focusCalendar, 0);
}
this.preSOpen = this.sOpen;
},
beforeDestroy: function beforeDestroy() {
clearTimeout(this.focusTimeout);
},
methods: {
onCalendarKeyDown: function onCalendarKeyDown(event) {
if (event.keyCode === KeyCode.ESC) {
event.stopPropagation();
this.closeCalendar(this.focus);
}
},
onCalendarSelect: function onCalendarSelect(value) {
var cause = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
var props = this.$props;
if (!hasProp(this, 'value')) {
this.setState({
sValue: value
});
}
var calendarProps = getOptionProps(props.calendar);
if (cause.source === 'keyboard' || !calendarProps.timePicker && cause.source !== 'dateInput' || cause.source === 'todayButton') {
this.closeCalendar(this.focus);
}
this.__emit('change', value);
},
onKeyDown: function onKeyDown(event) {
if (event.keyCode === KeyCode.DOWN && !this.sOpen) {
this.openCalendar();
event.preventDefault();
}
},
onCalendarOk: function onCalendarOk() {
this.closeCalendar(this.focus);
},
onCalendarClear: function onCalendarClear() {
this.closeCalendar(this.focus);
},
onVisibleChange: function onVisibleChange(open) {
this.setOpen(open);
},
getCalendarElement: function getCalendarElement() {
var props = this.$props;
var calendarProps = getOptionProps(props.calendar);
var calendarEvents = getEvents(props.calendar);
var value = this.sValue;
var defaultValue = value;
var extraProps = {
ref: 'calendarInstance',
props: {
defaultValue: defaultValue || calendarProps.defaultValue,
selectedValue: value
},
on: {
keydown: this.onCalendarKeyDown,
ok: createChainedFunction(calendarEvents.ok, this.onCalendarOk),
select: createChainedFunction(calendarEvents.select, this.onCalendarSelect),
clear: createChainedFunction(calendarEvents.clear, this.onCalendarClear)
}
};
return cloneElement(props.calendar, extraProps);
},
setOpen: function setOpen(open, callback) {
if (this.sOpen !== open) {
if (!hasProp(this, 'open')) {
this.setState({
sOpen: open
}, callback);
}
this.__emit('openChange', open);
}
},
openCalendar: function openCalendar(callback) {
this.setOpen(true, callback);
},
closeCalendar: function closeCalendar(callback) {
this.setOpen(false, callback);
},
focus: function focus() {
if (!this.sOpen) {
this.$el.focus();
}
},
focusCalendar: function focusCalendar() {
if (this.sOpen && this.calendarInstance && this.calendarInstance.componentInstance) {
this.calendarInstance.componentInstance.focus();
}
}
},
render: function render() {
var h = arguments[0];
var props = getOptionProps(this);
var style = getStyle(this);
var prefixCls = props.prefixCls,
placement = props.placement,
getCalendarContainer = props.getCalendarContainer,
align = props.align,
animation = props.animation,
disabled = props.disabled,
dropdownClassName = props.dropdownClassName,
transitionName = props.transitionName;
var sValue = this.sValue,
sOpen = this.sOpen;
var children = this.$scopedSlots['default'];
var childrenState = {
value: sValue,
open: sOpen
};
if (this.sOpen || !this.calendarInstance) {
this.calendarInstance = this.getCalendarElement();
}
return h(
Trigger,
{
attrs: {
popupAlign: align,
builtinPlacements: placements,
popupPlacement: placement,
action: disabled && !sOpen ? [] : ['click'],
destroyPopupOnHide: true,
getPopupContainer: getCalendarContainer,
popupStyle: style,
popupAnimation: animation,
popupTransitionName: transitionName,
popupVisible: sOpen,
prefixCls: prefixCls,
popupClassName: dropdownClassName
},
on: {
'popupVisibleChange': this.onVisibleChange
}
},
[h(
'template',
{ slot: 'popup' },
[this.calendarInstance]
), cloneElement(children(childrenState, props), { on: { keydown: this.onKeyDown } })]
);
}
};
export default Picker;