Skip to content

Commit c318e01

Browse files
committed
add min & max limiter
1 parent bdfe0e3 commit c318e01

File tree

3 files changed

+79
-44
lines changed

3 files changed

+79
-44
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

package.json

Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,30 @@
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.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",
27+
"dependencies": {
28+
"lodash": "^4.17.4"
29+
}
2730
}

src/vue-numeric.vue

Lines changed: 50 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,15 @@
11
<template>
2-
<input type="tel" ref="numeric" :value="value" @input="updateValue(amount)" v-model="amount">
2+
<input type="tel" ref="numeric" :value="value" @input="updateValue(amountValue)" v-model="amount">
33
</template>
44

55
<script>
6+
import _ from 'lodash'
7+
68
export default {
79
8-
name: 'Vue-Numeric',
10+
name: 'vue-numeric',
911
1012
props: {
11-
placeholder: {
12-
required: false,
13-
type: String
14-
},
15-
1613
value: {
1714
default: 0,
1815
required: true,
@@ -25,35 +22,69 @@ export default {
2522
type: String
2623
},
2724
25+
min: {
26+
required: false,
27+
type: Number
28+
},
29+
30+
max: {
31+
required: false,
32+
type: Number
33+
},
34+
2835
separator: {
2936
default: '',
3037
required: false,
3138
type: String
3239
}
3340
},
3441
35-
data () {
36-
return {
37-
amount: ''
38-
}
39-
},
42+
data: () => ({
43+
amount: ''
44+
}),
4045
41-
mounted () {
42-
this.updateValue(this.value)
46+
computed: {
47+
defaultValue () {
48+
return _.toNumber(this.value)
49+
},
50+
51+
amountValue () {
52+
return _.toNumber(this.amount)
53+
}
4354
},
4455
4556
methods: {
46-
updateValue (value) {
47-
const number = +value.replace(/[^0-9]+/g, '')
48-
this.amount = this.format(number)
49-
this.$emit('input', number)
57+
checkMinValue (value) {
58+
if (this.min) {
59+
if (value >= this.min) return true
60+
return false
61+
}
62+
return true
63+
},
64+
65+
checkMaxValue (value) {
66+
if (this.max) {
67+
if (value <= this.max) return true
68+
return false
69+
}
70+
return true
5071
},
5172
5273
format (value) {
5374
const numberWithSeparator = Number(value).toString().replace(/\B(?=(\d{3})+(?!\d))/g, this.separator)
5475
return this.currency + ' ' + numberWithSeparator
76+
},
77+
78+
updateValue (value) {
79+
if (this.checkMinValue(value) && this.checkMaxValue(value)) {
80+
this.amount = this.format(value)
81+
this.$emit('input', value)
82+
}
5583
}
56-
}
84+
},
5785
86+
mounted () {
87+
this.updateValue(this.defaultValue)
88+
}
5889
}
5990
</script>

0 commit comments

Comments
 (0)