inputNumberRange.vue
4.56 KB
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
<template>
<div>
<div class="input-number-range" :class="{ 'is-disabled': disabled }">
<div class="flex">
<div class="from">
<el-input ref="input_from" v-model="userInputForm" :disabled="disabled" placeholder="最小值" @keyup.enter.native="handleBlurFrom" @blur="handleBlurFrom" @focus="handleFocusFrom" @input="handleInputChangeFrom" @change="handleInputChangeFromChange"></el-input>
</div>
<div class="center">
<span>-</span>
</div>
<div class="to from">
<el-input ref="input_to" v-model="userInputTo" :disabled="disabled" placeholder="最大值" @keyup.enter.native="handleBlurTo" @blur="handleBlurTo" @focus="handleFocusTo" @input="handleInputChangeTo" @change="handleInputChangeToChange"></el-input>
</div>
</div>
</div>
</div>
</template>
<script>
export default {
name: 'InputNumberRange',
props: {
// 初始化范围
value: { required: true },
// 是否禁用
disabled: {
type: Boolean,
default: false
},
// 精度参数
precision: {
type: Number,
default: 0,
validator(val) {
return val >= 0 && val === parseInt(val, 10)
}
},
spescInputVal: {
type: String
}
},
data() {
return {
userInputForm: null,
userInputTo: null
}
},
watch: {
value: {
immediate: true,
handler(value) {
/** 初始化范围 */
if (value instanceof Array && this.precision !== undefined) {
this.userInputForm = typeof value[0] === 'number' ? value[0] : null
this.userInputTo = typeof value[1] === 'number' ? value[1] : null
}
}
}
},
mounted() {
if (this.spescInputVal) {
// let userInputForm = null
// let userInputTo = null
console.log(this.spescInputVal)
let [userInputForm, userInputTo] = this.spescInputVal.split('-')
this.userInputForm = userInputForm
this.userInputTo = userInputTo
}
},
methods: {
// 根据精度保留数字
toPrecision(num, precision) {
if (precision === undefined) precision = 0
return parseFloat(Math.round(num * Math.pow(10, precision)) / Math.pow(10, precision))
},
handleBlurFrom(event) {
this.$emit('blurfrom', event)
this.confirm()
},
handleFocusFrom(event) {
this.$emit('focusfrom', event)
},
confirm() {
// 如果两个都有值那就传递事件
if ((this.userInputForm || this.userInputForm == 0) && this.userInputTo) {
this.$emit('confirm', { value: `${this.userInputForm}-${this.userInputTo}` })
}
},
handleBlurTo(event) {
this.confirm()
},
handleFocusTo(event) {},
handleInputFrom(value) {},
handleInputTo(value) {
this.$emit('inputto', value)
},
// from输入框change事件
handleInputChangeFrom(value) {
this.userInputForm = value.replace(/[^0-9.]/g, '').replace(/^(100|[1-9]?\d)(\.\d{0,2})?.*$/, '$1$2')
},
handleInputChangeFromChange() {
// 看一下this.userInputTo 是否有值,如果有应该比它小
if (this.userInputTo) {
console.log('this.userInputTo', this.userInputTo, this.userInputForm, this.userInputTo < this.userInputForm)
if (parseInt(this.userInputTo) < parseInt(this.userInputForm)) {
this.userInputForm = this.userInputTo
}
}
},
// to输入框change事件
handleInputChangeTo(value) {
this.userInputTo = value.replace(/[^0-9.]/g, '').replace(/^(100|[1-9]?\d)(\.\d{0,2})?.*$/, '$1$2')
},
handleInputChangeToChange() {
// 看一下this.userInputForm 是否有值,如果有应该比它大
if (parseInt(this.userInputTo) < parseInt(this.userInputForm)) {
this.userInputTo = this.userInputForm
}
}
}
}
</script>
<style lang="scss" scoped>
// 取消element原有的input框样式
::v-deep .el-input--mini .el-input__inner {
border: 0px;
margin: 0;
padding: 0 0px;
background-color: transparent;
}
.input-number-range {
.from {
width: 75px;
}
// background-color: #fff;
// border: 1px solid #dcdfe6;
// border-radius: 4px;
}
.flex {
display: flex;
flex-direction: row;
width: 100%;
// justify-content: center;
margin-top: 10px;
align-items: center;
.center {
margin-top: 1px;
}
}
.is-disabled {
background-color: #eef0f6;
border-color: #e4e7ed;
color: #c0c4cc;
cursor: not-allowed;
}
</style>