Skip to content

Commit 8c29200

Browse files
committed
fix(input-otp): correct paste behavior to always start at first box
1 parent 82d086f commit 8c29200

File tree

2 files changed

+9
-12
lines changed

2 files changed

+9
-12
lines changed

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

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -551,24 +551,21 @@ export class InputOTP implements ComponentInterface {
551551
.filter((char) => validKeyPattern.test(char))
552552
.slice(0, length);
553553

554-
// Find the first empty input
555-
const startIndex = this.getFirstEmptyIndex();
556-
// If all boxes are filled, start at 0
557-
const pasteStart = startIndex === -1 ? 0 : startIndex;
558-
554+
// Always paste starting at the first box
559555
validChars.forEach((char, index) => {
560-
const targetIndex = pasteStart + index;
561-
if (targetIndex < length) {
562-
this.inputRefs[targetIndex].value = char;
563-
this.inputValues[targetIndex] = char;
556+
if (index < length) {
557+
this.inputRefs[index].value = char;
558+
this.inputValues[index] = char;
564559
}
565560
});
566561

562+
// Update the value so that all input boxes are updated
563+
this.value = validChars.join('');
567564
this.updateValue(event);
568565

569566
// Focus the next empty input after pasting
570567
// If all boxes are filled, focus the last input
571-
const nextEmptyIndex = pasteStart + validChars.length;
568+
const nextEmptyIndex = validChars.length;
572569
if (nextEmptyIndex < length) {
573570
inputRefs[nextEmptyIndex]?.focus();
574571
} else {

core/src/components/input-otp/test/basic/input-otp.e2e.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -587,7 +587,7 @@ configs({ modes: ['ios'] }).forEach(({ title, config }) => {
587587
await expect(inputBoxes.nth(2)).toBeFocused();
588588
});
589589

590-
test('should paste text into the last two input boxes when pasting 2 digits after typing 2 digits', async ({
590+
test('should paste text into the first two input boxes when pasting 2 digits after typing 2 digits', async ({
591591
page,
592592
}) => {
593593
await page.setContent(`<ion-input-otp>Description</ion-input-otp>`, config);
@@ -599,7 +599,7 @@ configs({ modes: ['ios'] }).forEach(({ title, config }) => {
599599

600600
const inputOtp = page.locator('ion-input-otp');
601601

602-
await verifyInputValues(inputOtp, ['1', '2', '3', '4']);
602+
await verifyInputValues(inputOtp, ['3', '4', '', '']);
603603
});
604604

605605
test('should paste text into all input boxes when pasting 4 digits after typing 4 digits', async ({ page }) => {

0 commit comments

Comments
 (0)