forked from Spikef/vmc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnumber.vue
79 lines (74 loc) · 2.38 KB
/
number.vue
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
<template>
<div class="vmc-number">
<span class="vmc-1px" @click="changeCount(0)">-</span>
<m-input :value.sync="value" :min="min" :max="max" :type="decimal ? 'number' : 'integer'" :style="style"></m-input>
<span class="vmc-1px" @click="changeCount(1)">+</span>
</div>
</template>
<script type="text/ecmascript-6">
import { mInput } from '../input';
import { getCSSSize } from '../../utils';
export default {
components: {
mInput
},
props: {
decimal: Boolean,
value: [String, Number],
width: [String, Number],
min: {
type: [Number, String],
coerce: Number,
default: -Infinity
},
max: {
type: [Number, String],
coerce: Number,
default: Infinity
},
step: {
type: [Number, String],
coerce: Number,
default: 1
}
},
computed: {
style() {
if (this.width) {
return {
width: getCSSSize(this.width)
}
}
}
},
methods: {
changeCount(add) {
var value = Number(this.value);
var step = add ? this.step : -this.step;
var val = this.decimal ? this.decimalPlus(value, step) : value + step;
if (val >= this.min && val <= this.max) {
this.value = val;
}
},
decimalPlus(a, b) {
// http://www.cnblogs.com/colder/p/5775555.html
var x = Number(String(a).replace('.', ''));
var y = Number(String(b).replace('.', ''));
var m = (String(a).split('.')[1] || '').length;
var n = (String(b).split('.')[1] || '').length;
var c = Math.abs(m - n);
var e = Math.max(m, n);
var p = Math.pow(10, e);
var q = Math.pow(10, c);
if (c > 0) {
if (m > n) {
y = y * q
} else {
x = x * q;
}
}
return (x + y) / p;
}
}
}
</script>