1
1
<template >
2
- <input type =" tel" :placeholder = " placeholder " ref =" numeric" @input =" processValue(amountValue)" v-model = " amount " >
2
+ <input type =" tel" :value = " value " v-model = " amount " ref =" numeric" :placeholder = " placeholder " @blur =" processValue(amountValue)" >
3
3
</template >
4
4
5
5
<script >
6
+ import accounting from ' accounting-js'
7
+
6
8
export default {
7
9
name: ' Vue-Numeric' ,
8
10
9
11
props: {
12
+ value: {
13
+ type: [Number , String ],
14
+ required: true
15
+ },
16
+
10
17
default: {
11
18
required: false ,
12
- type: [String , Number ]
19
+ type: [Number , String ]
20
+ },
21
+
22
+ precision: {
23
+ required: false ,
24
+ type: [Number , String ]
13
25
},
14
26
15
27
placeholder: {
@@ -18,8 +30,9 @@ export default {
18
30
},
19
31
20
32
min: {
33
+ default: 0 ,
21
34
required: false ,
22
- type: [String , Number ]
35
+ type: [Number , String ]
23
36
},
24
37
25
38
max: {
@@ -34,8 +47,8 @@ export default {
34
47
},
35
48
36
49
separator: {
37
- default: ' ' ,
38
- required: false ,
50
+ default: ' , ' ,
51
+ required: true ,
39
52
type: String
40
53
}
41
54
},
@@ -55,11 +68,23 @@ export default {
55
68
},
56
69
57
70
minValue () {
58
- return this .formatToNumber (this .min )
71
+ if (this .min ) return this .formatToNumber (this .min )
72
+ return undefined
59
73
},
60
74
61
75
maxValue () {
62
- return this .formatToNumber (this .max )
76
+ if (this .max ) return this .formatToNumber (this .max )
77
+ return undefined
78
+ },
79
+
80
+ decimalSeparator () {
81
+ if (this .separator === ' .' ) return ' ,'
82
+ return ' .'
83
+ },
84
+
85
+ thousandSeparator () {
86
+ if (this .separator === ' .' ) return ' .'
87
+ return ' ,'
63
88
}
64
89
},
65
90
@@ -85,16 +110,14 @@ export default {
85
110
},
86
111
87
112
formatToNumber (value ) {
88
- return Number (+ value .replace (/ [^ 0-9 ] + / g , ' ' ))
89
- },
90
-
91
- formatToCurrency (value ) {
92
- const numberWithSeparator = value .toString ().replace (/ \B (?=(\d {3} )+ (?!\d ))/ g , this .separator )
93
- return this .currency + ' ' + numberWithSeparator
113
+ if (this .thousandSeparator === ' .' ) return Number (+ value .replace (/ [^ 0-9 ,] + / g , ' ' ).replace (' ,' , ' .' ))
114
+ if (this .thousandSeparator === ' ,' ) return Number (+ value .replace (/ [^ 0-9 . ] + / g , ' ' ))
94
115
},
95
116
96
117
processValue (value ) {
97
- if (this .checkMaxValue (value)) {
118
+ if (isNaN (value)) {
119
+ this .updateValue (this .minValue )
120
+ } else if (this .checkMaxValue (value)) {
98
121
this .updateValue (this .maxValue )
99
122
} else if (this .checkMinValue (value)) {
100
123
this .updateValue (this .minValue )
@@ -104,10 +127,15 @@ export default {
104
127
},
105
128
106
129
updateValue (value ) {
107
- this .amount = this .formatToCurrency (value)
108
- this .$emit (' input' , value)
130
+ this .amount = accounting .formatMoney (value, {
131
+ symbol: this .currency + ' ' ,
132
+ precision: Number (this .precision ),
133
+ decimal: this .decimalSeparator ,
134
+ thousand: this .thousandSeparator
135
+ })
136
+
137
+ this .$emit (' input' , accounting .toFixed (value, this .precision ))
109
138
}
110
-
111
139
}
112
140
}
113
141
</script >
0 commit comments