forked from vueComponent/ant-design-vue
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDateInput.jsx
220 lines (209 loc) · 5.56 KB
/
DateInput.jsx
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
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';
import KeyCode from '../../../_util/KeyCode';
let cachedSelectionStart;
let cachedSelectionEnd;
let dateInputInstance;
const 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), PropTypes.func]),
locale: PropTypes.object,
disabledDate: PropTypes.func,
// onChange: PropTypes.func,
// onClear: PropTypes.func,
placeholder: PropTypes.string,
// onSelect: PropTypes.func,
selectedValue: PropTypes.object,
clearIcon: PropTypes.any,
inputMode: PropTypes.string,
inputReadOnly: PropTypes.bool,
},
data() {
const selectedValue = this.selectedValue;
return {
str: formatDate(selectedValue, this.format),
invalid: false,
hasFocus: false,
};
},
watch: {
selectedValue() {
this.updateState();
},
format() {
this.updateState();
},
},
updated() {
this.$nextTick(() => {
if (
dateInputInstance &&
this.$data.hasFocus &&
!this.invalid &&
!(cachedSelectionStart === 0 && cachedSelectionEnd === 0)
) {
dateInputInstance.setSelectionRange(cachedSelectionStart, cachedSelectionEnd);
}
});
},
getInstance() {
return dateInputInstance;
},
methods: {
updateState() {
if (dateInputInstance) {
cachedSelectionStart = dateInputInstance.selectionStart;
cachedSelectionEnd = dateInputInstance.selectionEnd;
}
// when popup show, click body will call this, bug!
const selectedValue = this.selectedValue;
if (!this.$data.hasFocus) {
this.setState({
str: formatDate(selectedValue, this.format),
invalid: false,
});
}
},
onClear() {
this.setState({
str: '',
});
this.__emit('clear', null);
},
onInputChange(e) {
const { value: str, composing } = e.target;
const { str: oldStr = '' } = this;
if (e.isComposing || composing || oldStr === str) return;
const { disabledDate, format, selectedValue } = this.$props;
// 没有内容,合法并直接退出
if (!str) {
this.__emit('change', null);
this.setState({
invalid: false,
str,
});
return;
}
// 不合法直接退出
const parsed = moment(str, format, true);
if (!parsed.isValid()) {
this.setState({
invalid: true,
str,
});
return;
}
const 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,
});
return;
}
if (selectedValue !== value || (selectedValue && value && !selectedValue.isSame(value))) {
this.setState({
invalid: false,
str,
});
this.__emit('change', value);
}
},
onFocus() {
this.setState({ hasFocus: true });
},
onBlur() {
this.setState((prevState, prevProps) => ({
hasFocus: false,
str: formatDate(prevProps.value, prevProps.format),
}));
},
onKeyDown(event) {
const { keyCode } = event;
const { value, disabledDate } = this.$props;
if (keyCode === KeyCode.ENTER) {
const validateDate = !disabledDate || !disabledDate(value);
if (validateDate) {
this.__emit('select', value.clone());
}
event.preventDefault();
}
},
getRootDOMNode() {
return this.$el;
},
focus() {
if (dateInputInstance) {
dateInputInstance.focus();
}
},
saveDateInput(dateInput) {
dateInputInstance = dateInput;
},
},
render() {
const {
invalid,
str,
locale,
prefixCls,
placeholder,
disabled,
showClear,
inputMode,
inputReadOnly,
} = this;
const clearIcon = getComponentFromProp(this, 'clearIcon');
const invalidClass = invalid ? `${prefixCls}-input-invalid` : '';
return (
<div class={`${prefixCls}-input-wrap`}>
<div class={`${prefixCls}-date-input-wrap`}>
<input
{...{
directives: [
{
name: 'ant-ref',
value: this.saveDateInput,
},
{
name: 'ant-input',
},
],
}}
class={`${prefixCls}-input ${invalidClass}`}
value={str}
disabled={disabled}
placeholder={placeholder}
onInput={this.onInputChange}
onKeydown={this.onKeyDown}
onFocus={this.onFocus}
onBlur={this.onBlur}
inputMode={inputMode}
readOnly={inputReadOnly}
/>
</div>
{showClear ? (
<a role="button" title={locale.clear} onClick={this.onClear}>
{clearIcon || <span class={`${prefixCls}-clear-btn`} />}
</a>
) : null}
</div>
);
},
};
export default DateInput;