Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

InputOtp : the content is not deleted #16515

Open
mchuc opened this issue Oct 10, 2024 · 2 comments
Open

InputOtp : the content is not deleted #16515

mchuc opened this issue Oct 10, 2024 · 2 comments
Labels
Resolution: Help Wanted Issue or pull request requires extra help and feedback
Milestone

Comments

@mchuc
Copy link

mchuc commented Oct 10, 2024

Describe the bug

when I want to programmatically change the content of the input, the component does not change - i.e. the letters already entered remain and are not deleted

Environment

mac/ angular 18
chrome (newest)

Reproducer

No response

Angular version

18.2.8

PrimeNG version

17.18.11

Build / Runtime

Angular CLI App

Language

TypeScript

Node version (for AoT issues node --version)

v22.9.0

Browser(s)

chrome

Steps to reproduce the behavior

run the program
type anything into the form

try to delete it programmatically - code as below

start code:

@ViewChild('otpInput') otpInput!: ElementRef;
odpowiedz: string = "";
...

try this after:

this.otpInput.nativeElement.value = '';
this.odpowiedz = "";
console.log(this.odpowiedz);
this.cd.detectChanges();

html /element

<p-inputOtp #otpInput [(ngModel)]="odpowiedz" [length]="dlugosc_slowa()" [autofocus]="true" pAutoFocus>
     <ng-template pTemplate="input" let-token let-index="index" let-events="events">
       <input
         pInputText
         class="custom-otp-input"
         (input)="events.input($event)"
         (keydown)="events.keydown($event)"
         (keyup)="handleKeyUp($event)"
         type="text"
         [attr.value]="token"
         [ngClass]="{'correct': litera_poprawna(index-1) , 'incorrect': !litera_poprawna(index-1)}"
         [maxLength]="1"
       />


     </ng-template>
   </p-inputOtp>

Expected behavior

currently if I change the word length parameter, a different number of boxes appears, but the boxes are already filled with a value that does not want to be programmatically changed or deleted, regardless of how I do it - as in the above code - summing up:

I want to have a clean, unfilled element and focus in the first box after removing the content from 'odpowiedz'

or add a function - clear()

@mchuc mchuc added the Status: Needs Triage Issue will be reviewed by Core Team and a relevant label will be added as soon as possible label Oct 10, 2024
@wS2Zth
Copy link
Contributor

wS2Zth commented Oct 13, 2024

Hi, you can use ViewChildren to access custom input elements then you can reset them for now.

In HTML
<input #customOtpInputElement pInputText class="custom-otp-input" (input)="events.input($event)" (keydown)="events.keydown($event)" type="text" [attr.value]="token" [maxLength]="1" />

In TS

@ViewChildren('customOtpInputElement') otpCustomInputElements!: QueryList<ElementRef<HTMLInputElement>>;

//Let's say you have a reset function;
resetOtp() {
    this.otpCustomInputElements.forEach((customInput) => {
        customInput.nativeElement.value = '';
    });
}

@mertsincan mertsincan added Resolution: Help Wanted Issue or pull request requires extra help and feedback and removed Status: Needs Triage Issue will be reviewed by Core Team and a relevant label will be added as soon as possible labels Nov 19, 2024
@mertsincan mertsincan added this to the Future milestone Nov 19, 2024
Copy link

Due to PrimeNG team's busy roadmap, this issue is available for anyone to work on. Make sure to reference this issue in your pull request. ✨ Thank you for your contribution! ✨

@github-project-automation github-project-automation bot moved this to Review in PrimeNG Dec 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Resolution: Help Wanted Issue or pull request requires extra help and feedback
Projects
Status: Review
Development

No branches or pull requests

3 participants