index.vue
2.8 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
<template>
<div class="vux-x-input weui-cell">
<div class="weui-cell__bd weui-cell__primary">
<input class="weui-input" style="text-align: center;" type="number" v-on="listeners" v-model="val" ref="input" :placeholder="placeholder" :disabled="disabled" @focus="onFocus">
</div>
</div>
</template>
<script type="text/ecmascript-6">
export default {
mounted () {
this.updateValue(this.value)
},
name: 'vue-pattern-input',
props: {
value: {
required: true,
default: '',
type: [Number, String]
},
regExp: {
type: RegExp,
default: null
},
replacement: {
type: String,
default: ''
},
placeholder: {
type: String,
default: ''
},
disabled: {
type: Boolean,
default: false
}
},
data () {
return {
val: '',
tmp: ''
}
},
computed: {
listeners () {
const listeners = {}
Object.keys(this.$listeners).forEach(fnName => {
listeners[fnName] = (e) => {
let val = e.target.value !== '' ? e.target.value : e.data
// console.warn(val);
this.$listeners[fnName](val)
}
})
listeners.input = (e) => {
let val = e.target.value !== '' ? e.target.value : e.data
// console.warn(val);
this.updateValue(val)
}
return listeners
}
},
methods: {
// format the value of input
formatValue (val) {
let formattedValue = ''
// const formattedValue = val.toString().replace(this.regExp, this.replacement)
val = _.isNull(val) ? '' : val
if (this.regExp.test(val.toString())) {
formattedValue = val.toString();
this.tmp = val.toString()
} else {
formattedValue = this.tmp ? this.tmp : ' '
}
return formattedValue
},
// update the value of input
updateValue (val) {
const formattedValue = this.formatValue(val)
this.val = formattedValue
this.emitInput(formattedValue)
this.emitChange(formattedValue)
},
// emit input event
emitInput (val) {
this.$emit('input', val)
},
// emit change event
emitChange () {
this.$emit('change', this.val)
},
onFocus (e) {
this.$emit('on-focus', e);
}
},
watch: {
// watch value prop
value (val) {
if (val !== this.val) {
this.updateValue(val)
}
}
}
}
</script>
<style lang="less">
.vux-x-input.weui-cell {
border: 1px solid #eee;
padding: 0;
border-radius: .2rem;
}
.weui-cell__bd {
-ms-flex: 1;
flex: 1;
}
.weui-input {
width: 100%;
border: 0;
outline: 0;
-webkit-appearance: none;
background-color: transparent;
font-size: inherit;
color: inherit;
height: 1.41176471em;
line-height: 1.41176471;
}
</style>