Skip to content

Commit 9ea649b

Browse files
committed
update to 1.2.0
1 parent f7172a1 commit 9ea649b

File tree

2 files changed

+51
-20
lines changed

2 files changed

+51
-20
lines changed

package.json

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "vue-numeric",
3-
"version": "1.1.2",
4-
"description": "Numeric input component based on Vue",
3+
"version": "1.2.0",
4+
"description": "Input field component to display currency value based on Vue.",
55
"author": "Kevin Ongko",
66
"main": "src/vue-numeric.vue",
77
"repository": {
@@ -23,5 +23,8 @@
2323
"vue.js"
2424
],
2525
"homepage": "https://github.com/kevinongko/vue-numeric#readme",
26-
"license": "MIT"
26+
"license": "MIT",
27+
"dependencies": {
28+
"accounting-js": "^1.1.1"
29+
}
2730
}

src/vue-numeric.vue

+45-17
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,27 @@
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+
68
export 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

Comments
 (0)