Skip to content

Commit 3e4fe5a

Browse files
committed
fix(input-otp): shift input boxes over 1 when clearing a value in the middle
1 parent fcb8e1d commit 3e4fe5a

File tree

1 file changed

+20
-8
lines changed

1 file changed

+20
-8
lines changed

core/src/components/input-otp/input-otp.tsx

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -203,16 +203,27 @@ export class InputOTP implements ComponentInterface {
203203

204204
if (event.key === 'Backspace') {
205205
if (this.inputValues[index]) {
206-
// If current input has a value, clear it
207-
this.inputRefs[index].value = '';
206+
// Remove the value at the current index
208207
this.inputValues[index] = '';
208+
209+
// Shift all values to the right of the current index left by one
210+
for (let i = index; i < length - 1; i++) {
211+
this.inputValues[i] = this.inputValues[i + 1];
212+
}
213+
214+
// Clear the last box
215+
this.inputValues[length - 1] = '';
216+
217+
// Update all inputRefs to match inputValues
218+
for (let i = 0; i < length; i++) {
219+
this.inputRefs[i].value = this.inputValues[i] || '';
220+
}
221+
209222
this.updateValue();
210-
} else if (index > 0) {
211-
// If current input is empty, move to previous input and clear its value
223+
event.preventDefault();
224+
} else if (!this.inputValues[index] && index > 0) {
225+
// If current input is empty, move to previous input
212226
this.focusPrevious(index);
213-
this.inputRefs[index - 1].value = '';
214-
this.inputValues[index - 1] = '';
215-
this.updateValue();
216227
}
217228
} else if (event.key === 'ArrowLeft') {
218229
event.preventDefault();
@@ -365,7 +376,8 @@ export class InputOTP implements ComponentInterface {
365376
}
366377

367378
render() {
368-
const { color, disabled, fill, hasFocus, inputId, inputRefs, inputValues, length, readonly, shape, size, type } = this;
379+
const { color, disabled, fill, hasFocus, inputId, inputRefs, inputValues, length, readonly, shape, size, type } =
380+
this;
369381
const mode = getIonMode(this);
370382
const inputmode = this.getInputmode();
371383
const tabbableIndex = this.getTabbableIndex();

0 commit comments

Comments
 (0)