Skip to content

Commit 40acd54

Browse files
committed
fix bug and update version
1 parent cb09779 commit 40acd54

File tree

3 files changed

+59
-39
lines changed

3 files changed

+59
-39
lines changed

README.md

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
# vue-numeric
22

33
[![npm version](https://badge.fury.io/js/vue-numeric.svg)](https://badge.fury.io/js/vue-numeric)
4-
[![npm](https://img.shields.io/npm/dt/vue-numeric.svg)](https://www.npmjs.com/package/vue-numeric)
5-
[![npm](https://img.shields.io/npm/l/vue-numeric.svg)](http://opensource.org/licenses/MIT)
4+
[![license](https://img.shields.io/github/license/mashape/apistatus.svg)](http://opensource.org/licenses/MIT)
65

76
Numeric input component based on [Vue](https://vuejs.org/).
87

@@ -29,17 +28,17 @@ import VueNumeric from 'vue-numeric'
2928
export default {
3029
3130
name: 'App',
32-
31+
3332
components: {
3433
VueNumeric
3534
},
36-
35+
3736
data () {
3837
return {
3938
price: ''
4039
}
4140
}
42-
41+
4342
}
4443
</script>
4544

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "vue-numeric",
3-
"version": "1.0.2",
3+
"version": "1.1.0",
44
"description": "Numeric input component based on Vue",
55
"author": "Kevin Ongko",
66
"main": "src/vue-numeric.vue",

src/vue-numeric.vue

Lines changed: 54 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,36 @@
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>
66
export 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

Comments
 (0)