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
import PropTypes from '../../../_util/vue-types';
import BaseMixin from '../../../_util/BaseMixin';
import { getComponentFromProp } from '../../../_util/props-util';
import moment from 'moment';
import { formatDate } from '../util';
var DateInput = {
mixins: [BaseMixin],
props: {
prefixCls: PropTypes.string,
timePicker: PropTypes.object,
value: PropTypes.object,
disabledTime: PropTypes.any,
format: PropTypes.oneOfType([PropTypes.string, PropTypes.arrayOf(PropTypes.string)]),
locale: PropTypes.object,
disabledDate: PropTypes.func,
// onChange: PropTypes.func,
// onClear: PropTypes.func,
placeholder: PropTypes.string,
// onSelect: PropTypes.func,
selectedValue: PropTypes.object,
clearIcon: PropTypes.any
},
data: function data() {
var selectedValue = this.selectedValue;
return {
str: formatDate(selectedValue, this.format),
invalid: false,
hasFocus: false
};
},
watch: {
selectedValue: function selectedValue() {
this.updateState();
},
format: function format() {
this.updateState();
}
},
updated: function updated() {
var _this = this;
this.$nextTick(function () {
if (_this.$data.hasFocus && !_this.invalid && !(_this.cachedSelectionStart === 0 && _this.cachedSelectionEnd === 0)) {
_this.$refs.dateInputInstance.setSelectionRange(_this.cachedSelectionStart, _this.cachedSelectionEnd);
}
});
},
methods: {
updateState: function updateState() {
this.cachedSelectionStart = this.$refs.dateInputInstance.selectionStart;
this.cachedSelectionEnd = this.$refs.dateInputInstance.selectionEnd;
// when popup show, click body will call this, bug!
var selectedValue = this.selectedValue;
if (!this.$data.hasFocus) {
this.setState({
str: formatDate(selectedValue, this.format),
invalid: false
});
}
},
onInputChange: function onInputChange(event) {
var str = event.target.value;
var _$props = this.$props,
disabledDate = _$props.disabledDate,
format = _$props.format,
selectedValue = _$props.selectedValue;
// 没有内容,合法并直接退出
if (!str) {
this.__emit('change', null);
this.setState({
invalid: false,
str: str
});
return;
}
var parsed = moment(str, format, true);
if (!parsed.isValid()) {
this.setState({
invalid: true,
str: str
});
return;
}
var value = this.value.clone();
value.year(parsed.year()).month(parsed.month()).date(parsed.date()).hour(parsed.hour()).minute(parsed.minute()).second(parsed.second());
if (!value || disabledDate && disabledDate(value)) {
this.setState({
invalid: true,
str: str
});
return;
}
if (selectedValue !== value || selectedValue && value && !selectedValue.isSame(value)) {
this.setState({
str: str
});
this.__emit('change', value);
}
},
onClear: function onClear() {
this.setState({
str: ''
});
this.__emit('clear', null);
},
getRootDOMNode: function getRootDOMNode() {
return this.$el;
},
focus: function focus() {
if (this.$refs.dateInputInstance) {
this.$refs.dateInputInstance.focus();
}
},
onFocus: function onFocus() {
this.setState({ hasFocus: true });
},
onBlur: function onBlur() {
this.setState(function (prevState, prevProps) {
return {
hasFocus: false,
str: formatDate(prevProps.value, prevProps.format)
};
});
}
},
render: function render() {
var h = arguments[0];
var invalid = this.invalid,
str = this.str,
locale = this.locale,
prefixCls = this.prefixCls,
placeholder = this.placeholder,
disabled = this.disabled,
showClear = this.showClear;
var clearIcon = getComponentFromProp(this, 'clearIcon');
var invalidClass = invalid ? prefixCls + '-input-invalid' : '';
return h(
'div',
{ 'class': prefixCls + '-input-wrap' },
[h(
'div',
{ 'class': prefixCls + '-date-input-wrap' },
[h('input', {
ref: 'dateInputInstance',
'class': prefixCls + '-input ' + invalidClass,
domProps: {
'value': str
},
attrs: {
disabled: disabled,
placeholder: placeholder
},
on: {
'input': this.onInputChange,
'focus': this.onFocus,
'blur': this.onBlur
}
})]
), showClear ? h(
'a',
{
attrs: { role: 'button', title: locale.clear },
on: {
'click': this.onClear
}
},
[clearIcon || h('span', { 'class': prefixCls + '-clear-btn' })]
) : null]
);
}
};
export default DateInput;