PasswordInputer.vue
3.7 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
<template>
<v-card flat class="PasswordInputer">
<div class="title">请输入交易密码</div>
<div class="PasswordInputBlock">
<input class="PasswordInput" id="PasswordInput-0" ref="0" maxlength="1" type="number" pattern="[0-9]*" v-model="inputValue[0]" @input="passwordInput" @keydown="keydown">
<input class="PasswordInput" id="PasswordInput-1" ref="1" maxlength="1" type="number" pattern="[0-9]*" v-model="inputValue[1]" @input="passwordInput" @keydown="keydown">
<input class="PasswordInput" id="PasswordInput-2" ref="2" maxlength="1" type="number" pattern="[0-9]*" v-model="inputValue[2]" @input="passwordInput" @keydown="keydown">
<input class="PasswordInput" id="PasswordInput-3" ref="3" maxlength="1" type="number" pattern="[0-9]*" v-model="inputValue[3]" @input="passwordInput" @keydown="keydown">
<input class="PasswordInput" id="PasswordInput-4" ref="4" maxlength="1" type="number" pattern="[0-9]*" v-model="inputValue[4]" @input="passwordInput" @keydown="keydown">
<input class="PasswordInput" id="PasswordInput-5" ref="5" maxlength="1" type="number" pattern="[0-9]*" v-model="inputValue[5]" @input="passwordInput" @keydown="keydown">
</div>
<div class="submitButton" @click="submit">确 定</div>
</v-card>
</template>
<script>
export default {
data: () => ({
password: '',
inputValue: {
0: '',
1: '',
2: '',
3: '',
4: '',
5: '',
},
}),
props: {
display: {
type: Boolean,
default: false
}
},
watch: {
display: function (val, oldVal) {
if (val) {
this.reset()
this.$refs['0'].focus()
}
},
},
methods: {
reset() {
this.password = ''
this.inputValue = {
0: '',
1: '',
2: '',
3: '',
4: '',
5: ''
}
},
passwordInput(e) {
let id = e.target.id
let index = parseInt(id.split('-')[1])
if (e.inputType === 'deleteContentBackward') return
if (index < 5) {
index++
let next = this.$refs[index.toString()]
next.focus()
// if (this.inputValue[index] !== '') {
// next.setSelectionRange(0, 1)
// return
// }
} else {
this.inputValue[5] = this.inputValue[5].slice(0,1)
this.password = this.passwordConnect()
}
},
passwordConnect() {
let result = ''
for (let i = 0; i < 6; i++) {
let code = this.inputValue[i]
if (code) {
result += code
} else {
return false
}
}
return result
},
keydown(e) {
if (e.key !== 'Backspace') return
const id = e.target.id
let index = parseInt(id.split('-')[1])
if (index === 0) return
if (index === 5 && this.inputValue[5] !== '') {
this.inputValue[5] = ''
return
}
const before = this.$refs[(index - 1).toString()]
before.focus()
},
submit() {
if (this.password.length !== 6) return
this.$emit('passwordSubmit', this.password)
}
}
}
</script>
<style lang="scss" scoped>
.PasswordInputer {
background-color: #fff;
color: #000;
.title {
margin-top: 1rem;
}
.PasswordInputBlock {
margin-top: 2.9rem;
text-align: center;
.PasswordInput {
width: 2.5rem;
height: 2.5rem;
line-height: 2.5rem;
background-color: #fff;
border: 1px solid $gold;
outline: none;
text-align: center;
font-size: 1.5rem;
margin: 0 0.75rem;
}
}
.submitButton {
height: 3.5rem;
font-size: 1.2rem;
line-height: 3.5rem;
border-top: 1px solid #ccc;
border-bottom: 1px solid #ccc;
margin-top: 3rem;
}
}
</style>