Skip to content

Commit e5dc4ca

Browse files
authored
Merge pull request #1 from kevinongko/develop
Develop
2 parents bdfe0e3 + 40acd54 commit e5dc4ca

File tree

4 files changed

+101
-47
lines changed

4 files changed

+101
-47
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
npm-debug.log
2+
node_modules

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: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,27 @@
11
{
2-
"name": "vue-numeric",
3-
"version": "1.0.2",
4-
"description": "Numeric input component based on Vue",
5-
"author": "Kevin Ongko",
6-
"main": "src/vue-numeric.vue",
7-
"repository": {
8-
"type": "git",
9-
"url": "git+https://github.com/kevinongko/vue-numeric.git"
10-
},
11-
"bugs": {
12-
"url": "https://github.com/kevinongko/vue-numeric/issues"
13-
},
14-
"keywords": [
15-
"component",
16-
"currency",
17-
"input",
18-
"text",
19-
"number",
20-
"numeric",
21-
"separator",
22-
"vue",
23-
"vue.js"
24-
],
25-
"homepage": "https://github.com/kevinongko/vue-numeric#readme",
26-
"license": "MIT"
2+
"name": "vue-numeric",
3+
"version": "1.1.0",
4+
"description": "Numeric input component based on Vue",
5+
"author": "Kevin Ongko",
6+
"main": "src/vue-numeric.vue",
7+
"repository": {
8+
"type": "git",
9+
"url": "git+https://github.com/kevinongko/vue-numeric.git"
10+
},
11+
"bugs": {
12+
"url": "https://github.com/kevinongko/vue-numeric/issues"
13+
},
14+
"keywords": [
15+
"component",
16+
"currency",
17+
"input",
18+
"text",
19+
"number",
20+
"numeric",
21+
"separator",
22+
"vue",
23+
"vue.js"
24+
],
25+
"homepage": "https://github.com/kevinongko/vue-numeric#readme",
26+
"license": "MIT"
2727
}

src/vue-numeric.vue

Lines changed: 71 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,29 @@
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>
66
export 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', number)
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))/g, this.separator)
91+
formatToCurrency (value) {
92+
const numberWithSeparator = value.toString().replace(/\B(?=(\d{3})+(?!\d))/g, this.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', value)
55109
}
56-
}
57110
111+
}
58112
}
59-
</script>
113+
</script>

0 commit comments

Comments
 (0)