Skip to content

Commit d172110

Browse files
fix(input-otp): prevent deletion when readonly is true
1 parent 2be39da commit d172110

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

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

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -535,10 +535,17 @@ export class InputOTP implements ComponentInterface {
535535
* - Tab: Allows normal tab navigation between components
536536
*/
537537
private onKeyDown = (index: number) => (event: KeyboardEvent) => {
538-
const { length } = this;
538+
const { length, readonly } = this;
539539
const rtl = isRTL(this.el);
540540
const input = event.target as HTMLInputElement;
541541

542+
if (readonly) {
543+
if (event.key === 'Backspace' || event.key === 'Delete') {
544+
event.preventDefault();
545+
}
546+
return;
547+
}
548+
542549
// Meta shortcuts are used to copy, paste, and select text
543550
// We don't want to handle these keys here
544551
const metaShortcuts = ['a', 'c', 'v', 'x', 'r', 'z', 'y'];
@@ -603,6 +610,11 @@ export class InputOTP implements ComponentInterface {
603610
* 5. Single character replacement
604611
*/
605612
private onInput = (index: number) => (event: InputEvent) => {
613+
if (this.readonly) {
614+
event.preventDefault();
615+
return;
616+
}
617+
606618
const { length, validKeyPattern } = this;
607619
const input = event.target as HTMLInputElement;
608620
const value = input.value;
@@ -735,6 +747,11 @@ export class InputOTP implements ComponentInterface {
735747
* the next empty input after pasting.
736748
*/
737749
private onPaste = (event: ClipboardEvent) => {
750+
if (this.readonly) {
751+
event.preventDefault();
752+
return;
753+
}
754+
738755
const { inputRefs, length, validKeyPattern } = this;
739756

740757
event.preventDefault();

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -690,6 +690,21 @@ configs({ modes: ['ios'] }).forEach(({ title, config }) => {
690690
});
691691

692692
test.describe(title('input-otp: backspace functionality'), () => {
693+
test('should not modify values with Backspace or Delete when readonly', async ({ page }) => {
694+
await page.setContent(`<ion-input-otp value="1234" readonly>Description</ion-input-otp>`, config);
695+
696+
const inputOtp = page.locator('ion-input-otp');
697+
const ionInput = await page.spyOnEvent('ionInput');
698+
699+
await page.keyboard.press('Tab');
700+
await page.keyboard.press('Backspace');
701+
await page.keyboard.press('Delete');
702+
await page.keyboard.type('5');
703+
704+
await verifyInputValues(inputOtp, ['1', '2', '3', '4']);
705+
await expect(ionInput).not.toHaveReceivedEvent();
706+
});
707+
693708
test('should backspace the first input box when backspace is pressed twice from the 2nd input box and the first input box has a value', async ({
694709
page,
695710
}) => {

0 commit comments

Comments
 (0)