Skip to content

Commit 3f599c6

Browse files
committed
OnPaste method has been refactored
1 parent da47a90 commit 3f599c6

File tree

1 file changed

+17
-16
lines changed

1 file changed

+17
-16
lines changed

angular-code-input/src/lib/code-input.component.ts

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -105,37 +105,38 @@ export class CodeInputComponent implements AfterViewInit, OnInit, OnChanges {
105105
e.preventDefault();
106106
e.stopPropagation();
107107

108-
const data = e.clipboardData?.getData('text');
108+
const data = e.clipboardData ? e.clipboardData.getData('text') : undefined;
109109

110110
if (this.isEmpty(data)) {
111111
return;
112112
}
113113

114114
// Convert paste text into iterable
115115
const values = data.split('');
116-
let index = 0;
116+
let valIndex = 0;
117117

118-
for (const val of values) {
119-
const target = this.inputs[i + index];
120-
121-
// Cancel the loop when a value cannot be used
122-
if (!this.canInputValue(val)) {
123-
this.setInputValue(target, null);
124-
this.setStateForInput(target, InputState.reset);
125-
return;
118+
for (let j = i; j < this.inputs.length; j++) {
119+
// The values end is reached. Loop exit
120+
if (valIndex === values.length) {
121+
break;
126122
}
127123

128-
this.setInputValue(target, val.toString());
124+
const input = this.inputs[j];
125+
const val = values[valIndex];
129126

130-
// Emit changes when we have reached the last input or we are at the last cycle of our values array
131-
if (i + index + 1 > this.codeLength - 1 || index >= values.length - 1) {
132-
target.blur();
133-
this.emitChanges();
127+
// Cancel the loop when a value cannot be used
128+
if (!this.canInputValue(val)) {
129+
this.setInputValue(input, null);
130+
this.setStateForInput(input, InputState.reset);
134131
return;
135132
}
136133

137-
index += 1;
134+
this.setInputValue(input, val.toString());
135+
valIndex++;
138136
}
137+
138+
this.inputs[i].blur();
139+
this.emitChanges();
139140
}
140141

141142
async onKeydown(e: any, i: number): Promise<void> {

0 commit comments

Comments
 (0)