forked from didi/mand-mobile
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.vue
More file actions
348 lines (329 loc) · 7.37 KB
/
index.vue
File metadata and controls
348 lines (329 loc) · 7.37 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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
<template>
<div class="md-codebox-wrapper">
<div
class="md-codebox"
:class="{
'is-disabled': disabled,
'is-justify': justify
}"
@click="focus"
>
<template v-if="maxlength > 0">
<span
v-for="i in num"
:key="i"
:class="[
'md-codebox-box',
(i === code.length + 1) && focused && 'is-active',
code.charAt(i-1) !== '' && 'is-filled'
]"
>
<template v-if="code.charAt(i-1)">
<template v-if="mask">
<i class="md-codebox-dot"></i>
</template>
<template v-else>
{{code.charAt(i-1)}}
</template>
</template>
<template v-if="i === code.length + 1 && focused">
<i class="md-codebox-blink"></i>
</template>
</span>
</template>
<template v-else>
<input
v-if="mask"
type="password"
:maxlength="maxlength"
:value="code"
readonly
disabled
:class="['md-codebox-holder', focused && 'is-active']"
/>
<input
v-else
type="text"
:maxlength="maxlength"
:value="code"
readonly
disabled
:class="['md-codebox-holder', focused && 'is-active']"
/>
</template>
</div>
<slot></slot>
<form action="" v-show="system" @submit="$_onSubmit">
<input
:value="code"
:type="mask ? 'password' : 'text'"
:maxlength="maxlength"
@input="$_onInputChange"
ref="input"
class="md-codebox-input"
/>
</form>
<md-number-keyboard
v-show="!system"
ref="keyboard"
class="md-codebox-keyboard"
:type="maxlength > 0 ? 'simple' : 'professional'"
:okText="okText"
:disorder="disorder"
:is-view="isView"
v-model="focused"
@delete="$_onDelete"
@enter="$_onEnter"
@confirm="$_onConfirm"
></md-number-keyboard>
</div>
</template>
<script>import NumberKeyboard from '../number-keyboard'
export default {
name: 'md-codebox',
components: {
[NumberKeyboard.name]: NumberKeyboard,
},
props: {
value: {
type: String,
default: '',
},
maxlength: {
type: [Number, String],
default: 4,
},
autofocus: {
type: Boolean,
default: false,
},
disabled: {
type: Boolean,
default: false,
},
justify: {
type: Boolean,
default: false,
},
mask: {
type: Boolean,
default: false,
},
closable: {
type: Boolean,
default: true,
},
system: {
type: Boolean,
default: false,
},
okText: {
type: String,
},
disorder: {
type: Boolean,
default: false,
},
isView: {
type: Boolean,
default: false,
},
},
data() {
return {
code: '',
focused: this.autofocus,
}
},
watch: {
value: {
immediate: true,
handler(val) {
if (val !== this.code) {
this.code = val
}
},
},
},
computed: {
num() {
return Math.abs(parseInt(this.maxlength, 10)) || 1
},
},
mounted() {
if (this.closable) {
document.addEventListener('click', this.$_handleOutClick)
}
if (!this.system && !this.isView) {
document.body.appendChild(this.$refs.keyboard.$el)
}
},
beforeDestroy() {
if (this.closable) {
document.removeEventListener('click', this.$_handleOutClick)
}
if (this.focused) {
this.blur()
}
if (!this.system && !this.isView) {
document.body.removeChild(this.$refs.keyboard.$el)
}
},
methods: {
// MARK: private methods
// MARK: events handler
$_handleOutClick(e) {
if (!this.$el.contains(e.target)) {
this.focused = false
}
},
$_onInputChange(e) {
if (this.maxlength < 0 || e.target.value.length <= this.maxlength) {
this.code = e.target.value
}
if (this.code.length === this.maxlength) {
this.$emit('submit', this.code)
}
this.$emit('input', this.code)
},
$_onSubmit(e) {
e.preventDefault()
this.$emit('submit', this.code)
},
$_onEnter(val) {
if ((this.maxlength < 0 || this.code.length < this.maxlength) && val !== '.') {
this.code += val
}
if (this.code.length === this.maxlength) {
this.$nextTick(function() {
this.$emit('submit', this.code)
})
}
this.$emit('input', this.code)
},
$_onDelete() {
this.code = this.code.slice(0, this.code.length - 1)
this.$emit('input', this.code)
},
$_onConfirm() {
this.$emit('submit', this.code)
},
// MARK: public methods
blur() {
this.focused = false
if (this.system) {
this.$refs.input.blur()
}
},
focus() {
if (this.disabled) {
return
}
this.focused = true
if (this.system) {
this.$refs.input.focus()
}
},
},
}
</script>
<style lang="stylus">
.md-codebox-wrapper
.md-codebox-input
position absolute
left -9999px
opacity 0
.md-codebox
position relative
display flex
flex-wrap nowrap
justify-content center
&.is-justify
.md-codebox-box
flex 1 1 0%
.md-codebox-box
position relative
flex 0 1 codebox-width
width codebox-width
height codebox-height
display flex
align-items center
justify-content center
color codebox-color
font-family font-family-number
font-size codebox-font-size
font-weight normal
line-height 1.2
if codebox-gutter is a 'unit'
margin-left (codebox-gutter / 2)
margin-right (codebox-gutter / 2)
else
margin-left "calc(%s / 2)" % codebox-gutter
margin-right "calc(%s / 2)" % codebox-gutter
hairline(bottom, color-border-element)
&:first-child
margin-left 0
&:last-child
margin-right 0
&.is-active, &.is-filled
border-color codebox-border-active-color
.md-codebox-blink
display block
if tab-height is a 'unit'
height (codebox-height * 0.8)
else
height "calc(%s * 0.8)" % codebox-height
width 2px
background-color codebox-blink-color
animation md-codebox-flash steps(2) 1s infinite
.md-codebox-dot
display block
height 10px
width 10px
border-radius 5px
background-color codebox-dot-color
.md-codebox-holder
pointer-events none
height codebox-height
line-height codebox-height
padding 0 codebox-holder-space
width 100%
text-align center
font-size codebox-font-size
outline none
color codebox-color
letter-spacing 0.1em
border-radius 0
border-style solid
border-width 0 0 codebox-border-width 0
border-color codebox-border-color
background none
box-shadow none
box-sizing border-box
-webkit-appearance none
-webkit-text-fill-color codebox-color
&[disabled],
&[readonly]
opacity 1
color codebox-color
border-color codebox-border-color
&.is-active
border-color codebox-border-active-color
.md-codebox.is-disabled
.md-codebox-box
color codebox-disabled-color
border-color codebox-disabled-color
.md-codebox-blink
display none
.md-codebox-dot
background-color codebox-disabled-color
.md-codebox-holder
color codebox-disabled-color
border-color codebox-disabled-color
@keyframes md-codebox-flash
from
opacity 0
to
opacity 1
</style>