Skip to content

Commit 6b65ceb

Browse files
committed
fix(input-otp): do not shift values right if the input value is selected
1 parent 6190d0c commit 6b65ceb

File tree

2 files changed

+30
-3
lines changed

2 files changed

+30
-3
lines changed

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -412,11 +412,14 @@ export class InputOTP implements ComponentInterface {
412412
private onKeyDown = (index: number) => (event: KeyboardEvent) => {
413413
const { length } = this;
414414
const rtl = isRTL(this.el);
415+
const input = event.target as HTMLInputElement;
415416

416-
// Do not process the paste shortcut to avoid changing
417-
// the value to the letter "v" on paste
418417
const isPasteShortcut = (event.metaKey || event.ctrlKey) && event.key.toLowerCase() === 'v';
419-
if (isPasteShortcut) {
418+
const isTextSelection = input.selectionStart !== input.selectionEnd;
419+
420+
// Return if the key is the paste shortcut or the input value
421+
// text is selected and let the onPaste / onInput handler manage it
422+
if (isPasteShortcut || isTextSelection) {
420423
return;
421424
}
422425

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,30 @@ configs({ modes: ['ios'] }).forEach(({ title, config }) => {
263263
// Focus should remain on the 2nd input box
264264
await expect(inputBoxes.nth(1)).toBeFocused();
265265
});
266+
267+
test('should not shift values right when selecting the text in the 2nd input box', async ({ page }) => {
268+
await page.setContent(
269+
`
270+
<ion-input-otp value="123">Description</ion-input-otp>
271+
`,
272+
config
273+
);
274+
275+
const secondInput = page.locator('ion-input-otp input').nth(1);
276+
await secondInput.focus();
277+
await secondInput.selectText();
278+
279+
const inputOtp = page.locator('ion-input-otp');
280+
const inputBoxes = page.locator('ion-input-otp input');
281+
282+
await page.keyboard.type('9');
283+
284+
await expect(inputBoxes.nth(0)).toHaveValue('1');
285+
await expect(inputBoxes.nth(1)).toHaveValue('9');
286+
await expect(inputBoxes.nth(2)).toHaveValue('3');
287+
await expect(inputBoxes.nth(3)).toHaveValue('');
288+
await expect(inputOtp).toHaveJSProperty('value', '193');
289+
});
266290
});
267291

268292
test.describe(title('input-otp: focus functionality'), () => {

0 commit comments

Comments
 (0)