-
Notifications
You must be signed in to change notification settings - Fork 373
/
mpx-input.vue
157 lines (152 loc) · 3.61 KB
/
mpx-input.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
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
<script>
import getInnerListeners, {
extendEvent,
getCustomEvent
} from '@mpxjs/webpack-plugin/lib/runtime/components/web/getInnerListeners'
export default {
name: 'mpx-input',
props: {
name: String,
value: {
type: String,
default: ''
},
type: {
type: String,
default: 'text'
},
password: Boolean,
placeholder: String,
disabled: Boolean,
maxlength: {
type: Number,
default: 140
},
autoFocus: Boolean,
focus: Boolean,
cursor: {
type: Number,
default: -1
},
selectionStart: {
type: Number,
default: -1
},
selectionEnd: {
type: Number,
default: -1
}
},
watch: {
cursor: {
handler (val) {
if (val !== -1) this.setSelectionRange(val, val)
},
immediate: true
},
selectionStart: {
handler (val) {
if (val !== -1) this.setSelectionRange(val)
},
immediate: true
},
selectionEnd: {
handler (val) {
if (val !== -1) this.setSelectionRange(undefined, val)
},
immediate: true
}
},
render (createElement) {
const mergeBefore = {
input (e) {
extendEvent(e, {
detail: {
value: e.target.value
}
})
},
focus (e) {
extendEvent(e, {
detail: {
value: e.target.value
}
})
},
blur (e) {
extendEvent(e, {
detail: {
value: e.target.value
}
})
}
}
const attrs = {
name: this.name,
value: this.value,
type: this.password ? 'password' : this.type,
placeholder: this.placeholder,
disabled: this.disabled,
autofocus: this.focus || this.autoFocus
}
if (this.maxlength !== -1) {
attrs.maxlength = this.maxlength
}
const data = {
class: 'mpx-input',
on: getInnerListeners(this, { mergeBefore }),
attrs,
ref: 'input'
}
return createElement('input', data, this.$slots.default)
},
methods: {
getValue () {
return this.$refs.input.value
},
setValue (value) {
this.$refs.input.value = value
},
notifyChange (value) {
if (value !== undefined) {
this.setValue(value)
}
// 通过原生input派发事件
this.$refs.input.dispatchEvent(getCustomEvent('input'))
},
setSelectionRange (start, end) {
if (!this.__selectionRange) this.__selectionRange = {
start: -1,
end: -1,
setting: false
}
if (start !== undefined) this.__selectionRange.start = start
if (end !== undefined) this.__selectionRange.end = end
if (!this.__selectionRange.setting) {
this.__selectionRange.setting = true
this.$nextTick(() => {
this.__selectionRange.setting = false
if (this.__selectionRange.start !== -1 && this.__selectionRange.end !== -1) {
this.$refs.input.setSelectionRange(this.__selectionRange.start, this.__selectionRange.end)
}
})
}
}
}
}
</script>
<style lang="stylus">
.mpx-input
cursor auto
width 100%
padding 0
border 0
font inherit
display block
height 1.4rem
text-overflow clip
overflow hidden
white-space nowrap
font-family UICTFontTextStyleBody
min-height 1.4rem
</style>