-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathinputNumber.vue
98 lines (94 loc) · 1.98 KB
/
inputNumber.vue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
<template>
<v-text-field class="v-input-number" dense flat solo background-color="info" v-model="inputVal"
:hide-details="hideDetails" @blur="onBlur" @keypress="onKeypress">
<template v-slot:prepend-inner>
<v-btn icon class="icon-btn" @click="subtract">
<v-icon>mdi-minus</v-icon>
</v-btn>
</template>
<template v-slot:append>
<v-btn class="icon-btn" icon @click="add">
<v-icon class="icon">mdi-plus</v-icon>
</v-btn>
</template>
</v-text-field>
</template>
<script>
export default {
model: { prop: 'value', event: 'input' },
props: {
value: {
type: [String, Number],
default: 0,
},
min: {
type: Number,
default: 0,
},
step: {
type: Number,
default: 1,
},
hideDetails: {
type: Boolean,
default: false,
},
},
data() {
return {
inputVal: '',
}
},
mounted() {
this.inputVal = this.value
},
watch: {
inputVal(newVal) {
this.emitEvent(newVal)
},
},
methods: {
emitEvent(val) {
this.$emit('input', val)
},
onBlur() {
let num = parseInt(this.inputVal, 10)
this.inputVal = Number.isNaN(num) || num < this.min ? this.min : num
},
onKeypress(e) {
const keyCode = e.keyCode
// 输入的不是0-9
if (keyCode < 48 || keyCode > 57) {
e.preventDefault()
return void 0
}
// 如果输入的第一位是0,输入的下一个字符会自动覆盖0
if (this.inputVal == '0') this.inputVal = ''
},
subtract() {
if (this.inputVal < this.min + this.step) return void 0
this.inputVal -= this.step
},
add() {
this.inputVal += this.step
},
},
components: {},
}
</script>
<style lang="scss">
.v-input-number {
.v-input__slot {
padding: 0 !important;
input {
text-align: center;
}
}
.icon-btn {
color: $light-4 !important;
&:hover {
color: $light-2 !important;
}
}
}
</style>