Skip to content

p-inputmask does not update "p-filled" class after doing a FormGroup.patchValue() #17527

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

Closed
1 of 4 tasks
clydebates opened this issue Jan 29, 2025 · 1 comment · Fixed by #18124
Closed
1 of 4 tasks
Assignees
Labels
Type: Bug Issue contains a bug related to a specific component. Something about the component is not working
Milestone

Comments

@clydebates
Copy link

clydebates commented Jan 29, 2025

Describe the bug

Using .patchValue() on a <input pInputText formControlName="myControl" /> works as expected. It inserts a "p-filled" class in the DOM, which allows FloatLabel to work correctly.

However, this expected behavior does NOT work with <p-inputmask>. Since the "p-filled" class is not inserted on the input element's classList, the FloatLabel overlaps the value.

HTML:

<form [formGroup]="myFormGroup">
  <div>
    <p-floatlabel variant="on">
      <p-inputmask
        aria-label="Zip Code"
        mask="99999"
        type="text"
        id="zipCode"
        formControlName="myControl"
      >
      </p-inputmask>
      <label for="zipCode"
        >Zip Code
        <span class="asterisk" aria-hidden="true">*</span>
      </label>
    </p-floatlabel>
  </div>
</form>

TS:

  ngOnInit() {
    this.myFormGroup = this.formBuilder.group({
      myControl: [''],
    });
  }

  onClick() {
    this.myFormGroup.patchValue({ myControl: '77777' });
  }

Result:

Image

Workaround:

myFormGroup.patchValue({myControl: '77777'});
addPrimeNgFilledClass('zipCode');

...

function addPrimeNgFilledClass(elementId: string) {
    const inputElementToFill: HTMLInputElement = document.getElementById(elementId)?.firstChild as HTMLInputElement;
    inputElementToFill?.classList.add('p-filled');
}

Workaround result:

Image

Pull Request Link

No response

Reason for not contributing a PR

  • Lack of time
  • Unsure how to implement the fix/feature
  • Difficulty understanding the codebase
  • Other

Other Reason

No response

Reproducer

https://stackblitz.com/edit/github-5jz2m89t-m7nk9x27?file=src%2Fapp%2Fapp.component.ts,src%2Fapp%2Fapp.component.html

Environment

develop, check the reproducer link

Angular version

19.0.6

PrimeNG version

v19

Node version

23.6.1

Browser(s)

No response

Steps to reproduce the behavior

Open StackBlitz: https://stackblitz.com/edit/github-5jz2m89t-m7nk9x27?file=src%2Fapp%2Fapp.component.ts,src%2Fapp%2Fapp.component.html

Click the "Patch Value" button. This will fire a .patchValue() on the form and update the value in the p-inputmask to "77777".

Notice the label does not move to the "on" position, instead overlays the value.

This is because the inputmask has not updated it's "filled" state.

Open the DevTools, manually add the class "p-filled" to the input, notice that the issue is fixed.

Expected behavior

inputmask should update its "filled" state when a .patchValue() is sent from a Reactive Form.

FloatLabel should move to the "on" position once there is a value inserted.

@clydebates clydebates added the Status: Needs Triage Issue will be reviewed by Core Team and a relevant label will be added as soon as possible label Jan 29, 2025
@mehmetcetin01140 mehmetcetin01140 added Type: Bug Issue contains a bug related to a specific component. Something about the component is not working and removed Status: Needs Triage Issue will be reviewed by Core Team and a relevant label will be added as soon as possible labels Apr 16, 2025
@mehmetcetin01140 mehmetcetin01140 added this to the 19.1.1 milestone Apr 16, 2025
@github-project-automation github-project-automation bot moved this to Review in PrimeNG Apr 16, 2025
@bmstambaugh
Copy link

I just ran into this same issue. This only appears to happen with Reactive Forms and when setting the value outside of the form control declaration. So from an API call or something similar. This is not a problem if using two-way binding [(ngModel)].

I've created a simple directive to workaround the issue.

DEMO: https://stackblitz.com/edit/nkmvzgep?file=src%2Fapp%2Fimports.ts

import {
  Directive,
  AfterViewInit,
  OnDestroy,
  Renderer2,
  ElementRef,
  Optional,
} from '@angular/core';
import { NgControl } from '@angular/forms';
import { takeWhile } from 'rxjs';

@Directive({
  selector: 'p-inputmask',
})
export class InputMaskDirective implements AfterViewInit, OnDestroy {
  IsComponentAlive = true;

  constructor(
    private el: ElementRef,
    private renderer: Renderer2,
    @Optional() private ngControl: NgControl
  ) {}

  ngAfterViewInit(): void {
    //If used with reactive forms subscribe to form control value changes
    if (this.ngControl?.valueChanges) {
      this.ngControl.valueChanges
        .pipe(takeWhile(() => this.IsComponentAlive))
        .subscribe(() => {
          // Add a CSS class to the host element's first child on each value change
          this.renderer.addClass(this.el.nativeElement.firstChild, 'p-filled');
        });
    }
  }

  ngOnDestroy(): void {
    this.IsComponentAlive = false;
  }
}

@mehmetcetin01140 mehmetcetin01140 self-assigned this Apr 21, 2025
cagataycivici added a commit that referenced this issue Apr 29, 2025
fix: #17527 - p-inputmask does not update p-filled class after doing …
@github-project-automation github-project-automation bot moved this from Review to Done in PrimeNG Apr 29, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Type: Bug Issue contains a bug related to a specific component. Something about the component is not working
Projects
Status: Done
Development

Successfully merging a pull request may close this issue.

3 participants