11<template >
2-  <input  type =" tel" ref = " numeric "   :value = " value " @input =" updateValue(amount )" 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- 
87 name:  ' Vue-Numeric'  
98
109 props:  { 
10+  default:  { 
11+  required:  false , 
12+  type:  [String , Number ] 
13+  }, 
14+ 
1115 placeholder:  { 
1216 required:  false , 
1317 type:  String  
1418 }, 
1519
16-  value:  { 
17-  default:  0 , 
18-  required:  true , 
20+  min:  { 
21+  required:  false , 
22+  type:  [String , Number ] 
23+  }, 
24+ 
25+  max:  { 
26+  required:  false , 
1927 type:  [String , Number ] 
2028 }, 
2129
@@ -32,28 +40,74 @@ export default {
3240 } 
3341 }, 
3442
35-  data  () { 
36-  return  { 
37-  amount:  ' '  
43+  data :  () =>  ({ 
44+  amount:  ' '  
45+  }), 
46+ 
47+  computed:  { 
48+  amountValue  () { 
49+  return  this .formatToNumber (this .amount ) 
50+  }, 
51+ 
52+  defaultValue  () { 
53+  if  (this .default ) return  this .formatToNumber (this .default ) 
54+  return  0  
55+  }, 
56+ 
57+  minValue  () { 
58+  return  this .formatToNumber (this .min ) 
59+  }, 
60+ 
61+  maxValue  () { 
62+  return  this .formatToNumber (this .max ) 
3863 } 
3964 }, 
4065
4166 mounted  () { 
42-  this .updateValue (this .value ) 
67+  if  ( this .default )  this . processValue (this .defaultValue ) 
4368 }, 
4469
4570 methods:  { 
46-  updateValue  (value ) { 
47-  const  number  =  + value .replace (/ [^ 0-9 ] + / g' '  
48-  this .amount  =  this .format (number) 
49-  this .$emit (' input'  
71+  checkMaxValue  (value ) { 
72+  if  (this .max ) { 
73+  if  (value <=  this .maxValue ) return  false  
74+  return  true  
75+  } 
76+  return  false  
77+  }, 
78+ 
79+  checkMinValue  (value ) { 
80+  if  (this .min ) { 
81+  if  (value >=  this .minValue ) return  false  
82+  return  true  
83+  } 
84+  return  false  
85+  }, 
86+ 
87+  formatToNumber  (value ) { 
88+  return  Number (+ value .replace (/ [^ 0-9 ] + / g' '  
5089 }, 
5190
52-  format  (value ) { 
53-  const  numberWithSeparator  =  Number ( value) .toString ().replace (/ \B (?=(\d {3} )+ (?!\d ))/ gthis .separator ) 
91+  formatToCurrency  (value ) { 
92+  const  numberWithSeparator  =  value .toString ().replace (/ \B (?=(\d {3} )+ (?!\d ))/ gthis .separator ) 
5493 return  this .currency  +  '  ' +  numberWithSeparator 
94+  }, 
95+ 
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) 
103+  } 
104+  }, 
105+ 
106+  updateValue  (value ) { 
107+  this .amount  =  this .formatToCurrency (value) 
108+  this .$emit (' input'  
55109 } 
56-  } 
57110
111+  } 
58112} 
59- script >
113+ script >
0 commit comments