11<template >
2- <input type =" tel" ref = " numeric " :value = " value " @input =" updateValue (amountValue)" v-model =" amount" >
2+ <input type =" tel" :placeholder = " placeholder " ref = " numeric " @input =" processValue (amountValue)" v-model =" amount" >
33</template >
44
55<script >
66export default {
7-
8- name: ' vue-numeric' ,
7+ name: ' Vue-Numeric' ,
98
109 props: {
11- value: {
12- default: 0 ,
13- required: true ,
10+ default: {
11+ required: false ,
1412 type: [String , Number ]
1513 },
1614
17- currency: {
18- default: ' ' ,
15+ placeholder: {
1916 required: false ,
2017 type: String
2118 },
2219
2320 min: {
2421 required: false ,
25- type: Number
22+ type: [ String , Number ]
2623 },
2724
2825 max: {
2926 required: false ,
30- type: Number
27+ type: [String , Number ]
28+ },
29+
30+ currency: {
31+ default: ' ' ,
32+ required: false ,
33+ type: String
3134 },
3235
3336 separator: {
@@ -42,51 +45,69 @@ export default {
4245 }),
4346
4447 computed: {
48+ amountValue () {
49+ return this .formatToNumber (this .amount )
50+ },
51+
4552 defaultValue () {
46- return this .formatToNumber (this .value )
53+ if (this .default ) return this .formatToNumber (this .default )
54+ return 0
4755 },
4856
49- amountValue () {
50- return this .formatToNumber (this .amount )
57+ minValue () {
58+ return this .formatToNumber (this .min )
59+ },
60+
61+ maxValue () {
62+ return this .formatToNumber (this .max )
5163 }
5264 },
5365
66+ mounted () {
67+ if (this .default ) this .processValue (this .defaultValue )
68+ },
69+
5470 methods: {
55- checkMinValue (value ) {
56- if (this .min ) {
57- if (value > = this .min ) return true
58- return false
71+ checkMaxValue (value ) {
72+ if (this .max ) {
73+ if (value < = this .maxValue ) return false
74+ return true
5975 }
60- return true
76+ return false
6177 },
6278
63- checkMaxValue (value ) {
64- if (this .max ) {
65- if (value < = this .max ) return true
66- return false
79+ checkMinValue (value ) {
80+ if (this .min ) {
81+ if (value > = this .minValue ) return false
82+ return true
6783 }
68- return true
84+ return false
6985 },
7086
7187 formatToNumber (value ) {
72- + value .replace (/ [^ 0-9 ] + / g , ' ' )
88+ return Number ( + value .replace (/ [^ 0-9 ] + / g , ' ' ) )
7389 },
7490
7591 formatToCurrency (value ) {
76- const numberWithSeparator = Number ( value) .toString ().replace (/ \B (?=(\d {3} )+ (?!\d ))/ g , this .separator )
92+ const numberWithSeparator = value .toString ().replace (/ \B (?=(\d {3} )+ (?!\d ))/ g , this .separator )
7793 return this .currency + ' ' + numberWithSeparator
7894 },
7995
80- updateValue (value ) {
81- if (this .checkMinValue (value) && this .checkMaxValue (value)) {
82- this .amount = this .formatToCurrency (value)
83- this .$emit (' input' , value)
96+ processValue (value ) {
97+ if (this .checkMaxValue (value)) {
98+ this .updateValue (this .maxValue )
99+ } else if (this .checkMinValue (value)) {
100+ this .updateValue (this .minValue )
101+ } else {
102+ this .updateValue (value)
84103 }
104+ },
105+
106+ updateValue (value ) {
107+ this .amount = this .formatToCurrency (value)
108+ this .$emit (' input' , value)
85109 }
86- },
87110
88- mounted () {
89- this .updateValue (this .defaultValue )
90111 }
91112}
92- </script >
113+ </script >
0 commit comments