|
46 | 46 | handleInput(e, i) { |
47 | 47 | const input = e.target; |
48 | 48 |
|
49 | | - // Only allow numeric characters and limit to one character |
50 | | - input.value = input.value.replace(/\D/g, '').substring(0, 1); |
| 49 | + // If type is number only allow numeric characters and limit to one character |
| 50 | + input.value = (this.type === 'number') |
| 51 | + ? input.value.replace(/\D/g, '').substring(0, 1) |
| 52 | + : input.value.substring(0, 1); |
51 | 53 |
|
52 | 54 | this.state = Array.from({ length: this.length }).map((element, idx) => { |
53 | 55 | return this.$refs[`otp_${idx + 1}`].value || ''; |
|
65 | 67 | } |
66 | 68 | }, |
67 | 69 | handlePaste(e) { |
68 | | - // Get the pasted data, filter only numeric characters, and limit it to the maximum length of inputs |
69 | | - const paste = e.clipboardData.getData('text').replace(/\D/g, '').substring(0, this.length); |
70 | | - const inputs = Array.from(Array(this.length)); |
| 70 | + // Get the pasted data, if type is number filter only numeric characters, and limit it to the maximum length of inputs |
| 71 | + const paste = (this.type === 'number') |
| 72 | + ? e.clipboardData.getData('text').replace(/\D/g, '').substring(0, this.length) |
| 73 | + : e.clipboardData.getData('text'); |
71 | 74 |
|
72 | | - @this.set('{{ $statePath }}', paste); |
| 75 | + const inputs = Array.from(Array(this.length)); |
73 | 76 |
|
74 | 77 | inputs.forEach((element, idx) => { |
75 | 78 | if (paste[idx]) { |
76 | 79 | this.$refs[`otp_${idx + 1}`].value = paste[idx]; |
77 | 80 | } |
78 | 81 | }); |
79 | 82 |
|
| 83 | + const focusInputNumber = (paste.length < this.length) ? paste.length+1 : this.length; |
| 84 | +
|
| 85 | + this.$nextTick(() => { |
| 86 | + this.$refs[`otp_${focusInputNumber}`].focus(); |
| 87 | + }); |
| 88 | +
|
80 | 89 | if (paste.length === this.length) { |
81 | | - this.$nextTick(() => { |
82 | | - this.$refs[`otp_${this.length}`].focus(); |
83 | | - }); |
| 90 | + @this.set('{{ $statePath }}', paste); |
84 | 91 | } |
85 | 92 |
|
86 | 93 | e.preventDefault(); |
|
0 commit comments