11<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)" >
33</template >
44
55<script >
6+ import accounting from ' accounting-js'
7+
68export default {
79 name: ' Vue-Numeric' ,
810
911 props: {
12+ value: {
13+ type: [Number , String ],
14+ required: true
15+ },
16+
1017 default: {
1118 required: false ,
12- type: [String , Number ]
19+ type: [Number , String ]
20+ },
21+
22+ precision: {
23+ required: false ,
24+ type: [Number , String ]
1325 },
1426
1527 placeholder: {
@@ -18,8 +30,9 @@ export default {
1830 },
1931
2032 min: {
33+ default: 0 ,
2134 required: false ,
22- type: [String , Number ]
35+ type: [Number , String ]
2336 },
2437
2538 max: {
@@ -34,8 +47,8 @@ export default {
3447 },
3548
3649 separator: {
37- default: ' ' ,
38- required: false ,
50+ default: ' , ' ,
51+ required: true ,
3952 type: String
4053 }
4154 },
@@ -55,11 +68,23 @@ export default {
5568 },
5669
5770 minValue () {
58- return this .formatToNumber (this .min )
71+ if (this .min ) return this .formatToNumber (this .min )
72+ return undefined
5973 },
6074
6175 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 ' ,'
6388 }
6489 },
6590
@@ -85,16 +110,14 @@ export default {
85110 },
86111
87112 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 , ' ' ))
94115 },
95116
96117 processValue (value ) {
97- if (this .checkMaxValue (value)) {
118+ if (isNaN (value)) {
119+ this .updateValue (this .minValue )
120+ } else if (this .checkMaxValue (value)) {
98121 this .updateValue (this .maxValue )
99122 } else if (this .checkMinValue (value)) {
100123 this .updateValue (this .minValue )
@@ -104,10 +127,15 @@ export default {
104127 },
105128
106129 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 ))
109138 }
110-
111139 }
112140}
113141 </script >
0 commit comments