Created
October 13, 2020 16:39
-
-
Save porfirion/a9417aeba2cce3446a538b741467be93 to your computer and use it in GitHub Desktop.
Vue.js 3 minimalistic number input
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export default { | |
props: { | |
modelValue: [Number, String], | |
min: { | |
type: [Number, String], | |
default: null, | |
}, | |
max: { | |
type: [Number, String], | |
default: null, | |
} | |
}, | |
methods: { | |
inc() { | |
let newValue = Number(this.modelValue) + 1; | |
if (this.max != null) { | |
if (newValue > Number(this.max)) { | |
newValue = Number(this.max) | |
} | |
} | |
this.updateValue(newValue); | |
}, | |
dec() { | |
let newValue = Number(this.modelValue) - 1; | |
if (this.min != null) { | |
if (newValue < Number(this.min)) { | |
newValue = Number(this.min); | |
} | |
} | |
this.updateValue(newValue); | |
}, | |
updateValue(newValue) { | |
this.$emit('update:modelValue', newValue) | |
}, | |
}, | |
template: `<div class="number-input"> | |
<button class="number-input__button" @click="dec">-</button> | |
<input class="number-input__input" | |
type="number" | |
:value="modelValue" | |
:min="min" | |
:max="max" | |
@input="$emit('update:modelValue', $event.target.value)"/> | |
<button class="number-input__button" @click="inc">+</button> | |
</div>`, | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Minimalistic number input with inc/dec buttons for Vue.js 3
Supports min and max value both for buttons and native input up/down controls.
Usage:
Associated styles: