7fa943eb
梁灏
init
|
1
2
3
4
5
|
<template>
<div :class="wrapClasses">
<div :class="handlerClasses">
<a
@click="up"
|
5d2d2d6f
梁灏
fixed #1654
|
6
|
@mousedown="preventDefault"
|
7fa943eb
梁灏
init
|
7
|
:class="upClasses">
|
95436eeb
梁灏
add InputNumber UI
|
8
|
<span :class="innerUpClasses" @click="preventDefault"></span>
|
7fa943eb
梁灏
init
|
9
10
11
|
</a>
<a
@click="down"
|
5d2d2d6f
梁灏
fixed #1654
|
12
|
@mousedown="preventDefault"
|
7fa943eb
梁灏
init
|
13
|
:class="downClasses">
|
95436eeb
梁灏
add InputNumber UI
|
14
|
<span :class="innerDownClasses" @click="preventDefault"></span>
|
7fa943eb
梁灏
init
|
15
16
17
18
|
</a>
</div>
<div :class="inputWrapClasses">
<input
|
acb79ba3
梁灏
fixed #433
|
19
|
:id="elementId"
|
7fa943eb
梁灏
init
|
20
21
|
:class="inputClasses"
:disabled="disabled"
|
c7a67856
子凡
fix
|
22
|
autocomplete="off"
|
f15c216a
丁强
Input 组件增加autofoc...
|
23
|
:autofocus="autofocus"
|
7fa943eb
梁灏
init
|
24
25
26
27
|
@focus="focus"
@blur="blur"
@keydown.stop="keyDown"
@change="change"
|
cb486838
Sergio Crisostomo
Add readonly prop...
|
28
|
:readonly="readonly"
|
7959adf7
梁灏
fixed #862
|
29
|
:name="name"
|
7a05b6e5
梁灏
fixed #1810
|
30
|
:value="precisionValue">
|
7fa943eb
梁灏
init
|
31
32
33
34
35
|
</div>
</div>
</template>
<script>
import { oneOf } from '../../utils/assist';
|
cd78c9c4
梁灏
some comps suppor...
|
36
|
import Emitter from '../../mixins/emitter';
|
7fa943eb
梁灏
init
|
37
38
|
const prefixCls = 'ivu-input-number';
|
95436eeb
梁灏
add InputNumber UI
|
39
|
const iconPrefixCls = 'ivu-icon';
|
7fa943eb
梁灏
init
|
40
41
|
function isValueNumber (value) {
|
8df29435
Rijn
changed regular e...
|
42
|
return (/(^-?[0-9]+\.{1}\d+$)|(^-?[1-9][0-9]*$)|(^-?0{1}$)/).test(value + '');
|
7fa943eb
梁灏
init
|
43
44
|
}
function addNum (num1, num2) {
|
17e1fcf1
梁灏
init DatePicker
|
45
|
let sq1, sq2, m;
|
7fa943eb
梁灏
init
|
46
|
try {
|
b0893113
jingsam
add eslint
|
47
|
sq1 = num1.toString().split('.')[1].length;
|
7fa943eb
梁灏
init
|
48
49
50
51
52
|
}
catch (e) {
sq1 = 0;
}
try {
|
b0893113
jingsam
add eslint
|
53
|
sq2 = num2.toString().split('.')[1].length;
|
7fa943eb
梁灏
init
|
54
55
56
57
58
59
60
61
62
63
64
|
}
catch (e) {
sq2 = 0;
}
// if (sq1 === 0 || sq2 === 0) {
// return num1 + num2;
// } else {
// m = Math.pow(10, Math.max(sq1, sq2));
// return (num1 * m + num2 * m) / m;
// }
m = Math.pow(10, Math.max(sq1, sq2));
|
075f6215
leonisme
Fix a bug in inpu...
|
65
|
return (Math.round(num1 * m) + Math.round(num2 * m)) / m;
|
7fa943eb
梁灏
init
|
66
67
68
|
}
export default {
|
34ee7b4a
梁灏
support Tree & ad...
|
69
|
name: 'InputNumber',
|
cd78c9c4
梁灏
some comps suppor...
|
70
|
mixins: [ Emitter ],
|
7fa943eb
梁灏
init
|
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
|
props: {
max: {
type: Number,
default: Infinity
},
min: {
type: Number,
default: -Infinity
},
step: {
type: Number,
default: 1
},
value: {
type: Number,
default: 1
},
size: {
validator (value) {
|
f00a037c
梁灏
some Components's...
|
90
|
return oneOf(value, ['small', 'large', 'default']);
|
7fa943eb
梁灏
init
|
91
92
93
94
95
|
}
},
disabled: {
type: Boolean,
default: false
|
f15c216a
丁强
Input 组件增加autofoc...
|
96
97
|
},
autofocus: {
|
8d3a02a5
丁强
修改input组件 autofoc...
|
98
99
|
type: Boolean,
default: false
|
7959adf7
梁灏
fixed #862
|
100
|
},
|
cb486838
Sergio Crisostomo
Add readonly prop...
|
101
102
103
104
|
readonly: {
type: Boolean,
default: false
},
|
7959adf7
梁灏
fixed #862
|
105
106
|
name: {
type: String
|
7a05b6e5
梁灏
fixed #1810
|
107
108
109
|
},
precision: {
type: Number
|
acb79ba3
梁灏
fixed #433
|
110
111
112
|
},
elementId: {
type: String
|
7fa943eb
梁灏
init
|
113
114
115
116
117
118
|
}
},
data () {
return {
focused: false,
upDisabled: false,
|
c97c42ab
梁灏
support InputNumber
|
119
120
|
downDisabled: false,
currentValue: this.value
|
b0893113
jingsam
add eslint
|
121
|
};
|
7fa943eb
梁灏
init
|
122
123
124
125
126
127
128
129
130
131
|
},
computed: {
wrapClasses () {
return [
`${prefixCls}`,
{
[`${prefixCls}-${this.size}`]: !!this.size,
[`${prefixCls}-disabled`]: this.disabled,
[`${prefixCls}-focused`]: this.focused
}
|
b0893113
jingsam
add eslint
|
132
|
];
|
7fa943eb
梁灏
init
|
133
134
135
136
137
138
139
140
141
142
143
|
},
handlerClasses () {
return `${prefixCls}-handler-wrap`;
},
upClasses () {
return [
`${prefixCls}-handler`,
`${prefixCls}-handler-up`,
{
[`${prefixCls}-handler-up-disabled`]: this.upDisabled
}
|
b0893113
jingsam
add eslint
|
144
|
];
|
7fa943eb
梁灏
init
|
145
146
|
},
innerUpClasses () {
|
95436eeb
梁灏
add InputNumber UI
|
147
|
return `${prefixCls}-handler-up-inner ${iconPrefixCls} ${iconPrefixCls}-ios-arrow-up`;
|
7fa943eb
梁灏
init
|
148
149
150
151
152
153
154
155
|
},
downClasses () {
return [
`${prefixCls}-handler`,
`${prefixCls}-handler-down`,
{
[`${prefixCls}-handler-down-disabled`]: this.downDisabled
}
|
b0893113
jingsam
add eslint
|
156
|
];
|
7fa943eb
梁灏
init
|
157
158
|
},
innerDownClasses () {
|
95436eeb
梁灏
add InputNumber UI
|
159
|
return `${prefixCls}-handler-down-inner ${iconPrefixCls} ${iconPrefixCls}-ios-arrow-down`;
|
7fa943eb
梁灏
init
|
160
161
162
|
},
inputWrapClasses () {
return `${prefixCls}-input-wrap`;
|
95436eeb
梁灏
add InputNumber UI
|
163
164
165
|
},
inputClasses () {
return `${prefixCls}-input`;
|
7a05b6e5
梁灏
fixed #1810
|
166
167
168
|
},
precisionValue () {
// can not display 1.0
|
cb2678c4
梁灏
fix InputNumber b...
|
169
|
return this.precision ? this.currentValue.toFixed(this.precision) : this.currentValue;
|
7fa943eb
梁灏
init
|
170
171
172
173
174
175
|
}
},
methods: {
preventDefault (e) {
e.preventDefault();
},
|
1ff55186
梁灏
optimize InputNum...
|
176
177
178
|
up (e) {
const targetVal = Number(e.target.value);
if (this.upDisabled && isNaN(targetVal)) {
|
7fa943eb
梁灏
init
|
179
180
|
return false;
}
|
1ff55186
梁灏
optimize InputNum...
|
181
|
this.changeStep('up', e);
|
7fa943eb
梁灏
init
|
182
|
},
|
1ff55186
梁灏
optimize InputNum...
|
183
184
185
|
down (e) {
const targetVal = Number(e.target.value);
if (this.downDisabled && isNaN(targetVal)) {
|
7fa943eb
梁灏
init
|
186
187
|
return false;
}
|
1ff55186
梁灏
optimize InputNum...
|
188
|
this.changeStep('down', e);
|
7fa943eb
梁灏
init
|
189
|
},
|
1ff55186
梁灏
optimize InputNum...
|
190
|
changeStep (type, e) {
|
cb486838
Sergio Crisostomo
Add readonly prop...
|
191
|
if (this.disabled || this.readonly) {
|
7fa943eb
梁灏
init
|
192
193
194
|
return false;
}
|
1ff55186
梁灏
optimize InputNum...
|
195
|
const targetVal = Number(e.target.value);
|
c97c42ab
梁灏
support InputNumber
|
196
|
let val = Number(this.currentValue);
|
7fa943eb
梁灏
init
|
197
198
199
200
201
|
const step = Number(this.step);
if (isNaN(val)) {
return false;
}
|
1ff55186
梁灏
optimize InputNum...
|
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
|
// input a number, and key up or down
if (!isNaN(targetVal)) {
if (type === 'up') {
if (addNum(targetVal, step) <= this.max) {
val = targetVal;
} else {
return false;
}
} else if (type === 'down') {
if (addNum(targetVal, -step) >= this.min) {
val = targetVal;
} else {
return false;
}
}
}
if (type === 'up') {
|
7fa943eb
梁灏
init
|
220
|
val = addNum(val, step);
|
1ff55186
梁灏
optimize InputNum...
|
221
|
} else if (type === 'down') {
|
7fa943eb
梁灏
init
|
222
223
224
225
226
|
val = addNum(val, -step);
}
this.setValue(val);
},
setValue (val) {
|
cb2678c4
梁灏
fix InputNumber b...
|
227
|
// 如果 step 是小数,且没有设置 precision,是有问题的
|
815f8354
Sergio Crisostomo
Check if isNaN so...
|
228
|
if (!isNaN(this.precision)) val = Number(Number(val).toFixed(this.precision));
|
cb2678c4
梁灏
fix InputNumber b...
|
229
|
|
0b94936a
梁灏
fixed #28
|
230
|
this.$nextTick(() => {
|
c97c42ab
梁灏
support InputNumber
|
231
232
|
this.currentValue = val;
this.$emit('input', val);
|
4a260ed5
梁灏
update InputNumber
|
233
|
this.$emit('on-change', val);
|
cd78c9c4
梁灏
some comps suppor...
|
234
|
this.dispatch('FormItem', 'on-form-change', val);
|
0b94936a
梁灏
fixed #28
|
235
|
});
|
7fa943eb
梁灏
init
|
236
237
238
239
240
241
242
243
244
245
|
},
focus () {
this.focused = true;
},
blur () {
this.focused = false;
},
keyDown (e) {
if (e.keyCode === 38) {
e.preventDefault();
|
1ff55186
梁灏
optimize InputNum...
|
246
|
this.up(e);
|
7fa943eb
梁灏
init
|
247
248
|
} else if (e.keyCode === 40) {
e.preventDefault();
|
1ff55186
梁灏
optimize InputNum...
|
249
|
this.down(e);
|
7fa943eb
梁灏
init
|
250
251
252
253
254
255
256
257
258
259
|
}
},
change (event) {
let val = event.target.value.trim();
const max = this.max;
const min = this.min;
if (isValueNumber(val)) {
val = Number(val);
|
c97c42ab
梁灏
support InputNumber
|
260
|
this.currentValue = val;
|
0b94936a
梁灏
fixed #28
|
261
|
|
7fa943eb
梁灏
init
|
262
263
264
265
266
267
268
269
|
if (val > max) {
this.setValue(max);
} else if (val < min) {
this.setValue(min);
} else {
this.setValue(val);
}
} else {
|
c97c42ab
梁灏
support InputNumber
|
270
|
event.target.value = this.currentValue;
|
7fa943eb
梁灏
init
|
271
|
}
|
0b94936a
梁灏
fixed #28
|
272
273
|
},
changeVal (val) {
|
7fa943eb
梁灏
init
|
274
275
276
|
if (isValueNumber(val) || val === 0) {
val = Number(val);
const step = this.step;
|
0b94936a
梁灏
fixed #28
|
277
278
279
|
this.upDisabled = val + step > this.max;
this.downDisabled = val - step < this.min;
|
7fa943eb
梁灏
init
|
280
281
282
283
284
|
} else {
this.upDisabled = true;
this.downDisabled = true;
}
}
|
0b94936a
梁灏
fixed #28
|
285
|
},
|
c97c42ab
梁灏
support InputNumber
|
286
287
|
mounted () {
this.changeVal(this.currentValue);
|
0b94936a
梁灏
fixed #28
|
288
289
290
|
},
watch: {
value (val) {
|
c97c42ab
梁灏
support InputNumber
|
291
292
293
|
this.currentValue = val;
},
currentValue (val) {
|
0b94936a
梁灏
fixed #28
|
294
|
this.changeVal(val);
|
fa66bfb1
Rijn
add watchers for ...
|
295
296
297
298
299
300
|
},
min () {
this.changeVal(this.currentValue);
},
max () {
this.changeVal(this.currentValue);
|
0b94936a
梁灏
fixed #28
|
301
|
}
|
7fa943eb
梁灏
init
|
302
|
}
|
b0893113
jingsam
add eslint
|
303
|
};
|
c7a67856
子凡
fix
|
304
|
</script>
|