-
Notifications
You must be signed in to change notification settings - Fork 300
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
Formly + ngx mask - label not floating when initial input value is set #1305
Comments
try using |
Any solutions? |
same issue with |
I don't use formly, just angular material directly, but see the same issue. I think the problem is because the value is being set this way:
It's done in a So I think either the value needs to be written right away, or maybe it could call onChange so that Angular material knows there is a new value and do its markForCheck, which will re-check if the label needs to be floated or not (https://github.com/angular/components/blob/bd84c2a67476b688a0c775de8566a4ff4b3b2ce0/src/material/form-field/form-field.ts#L447) |
+1 on this, can repro with Angular, Material, and ngx-mask. Steps to reproduce:
import { NgxMaskDirective } from 'ngx-mask';
import {
ChangeDetectionStrategy,
ChangeDetectorRef,
Component,
inject,
} from '@angular/core';
import {
FormControl,
FormGroup,
FormsModule,
ReactiveFormsModule,
} from '@angular/forms';
import { MatFormFieldModule } from '@angular/material/form-field';
import { MatInputModule } from '@angular/material/input';
@Component({
selector: 'app-root',
standalone: true,
imports: [
MatFormFieldModule,
FormsModule,
NgxMaskDirective,
MatInputModule,
MatFormFieldModule,
ReactiveFormsModule,
],
templateUrl: './app.component.html',
styleUrl: './app.component.css',
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class AppComponent {
budget = new FormControl(1500);
form = new FormGroup({
budget: this.budget,
});
/* - fixes the bug
private cdr = inject(ChangeDetectorRef);
ngAfterViewInit() {
setTimeout(() => this.cdr.markForCheck(), 0);
}
*/
} <form [formGroup]="form">
<mat-form-field class="mb-24">
<mat-label class="ui-text-md">Budget amount</mat-label>
<input matInput mask="separator.2" thousandSeparator="," numericOnly formControlName="budget" />
</mat-form-field>
</form> |
+1 same issue with angular material form field component |
🐞 bug report
Is this a regression?
Unsure
Description
I have an Angular app (using the standalone API) that uses ngx-formly for creating forms. I am currently adding ngx-mask to be used in masking user input. I have created a custom ngx-formly component type that implements ngx-mask called 'masked-input'. This component type is simply a
mat-form-field
with amat-label
and aninput
(which uses theMatInput
class).Everything seems to work as expected until I set an initial value with
defaultValue
. When I have this set, the label and value will be overlapping like the following example:Clicking the input will cause the label to behave normally again.
What I've Tried
Change Detection
Initially felt like a change detection issue. I've tried changing detection strategy to
OnPush
and virtually no change. I've also tried callingmarkForCheck()
anddetectChanges()
at various points in the component's lifecycle to no avail. I tried wrappingmarkForCheck()
in asetTimeout
with a 0 second delay in myngOnInit()
which actually worked! Though, this feels like a hack and is not a solution I would like to actually ship.Input Attributes
I've tried removing and adding attributes on the
input
element to see where exactly it breaks. The issue begins the moment I add themask
attribute. I also played around with thevalue
attribute a bit. I found that I could provide it with a default value of just a space,value=" "
, and that would also cause the problem to go away. Though, again this feels like a hack and not something I feel comfortable shipping if there is a better way.provideAnimations()
I noticed while playing around with the StackBlitz demo that the label will behave normally if I remove the call to provideAnimations() in the bootstrapApplication() call, though I would begin to get a ton of errors in the console. Maybe that is at least a clue?
🔬 Minimal Reproduction
https://stackblitz.com/edit/stackblitz-starters-u7zpzf?file=src%2Fmain.ts
🔥 Exception or Error
None
🌍 Your Environment
My app runs on the following:
Though I was able to reproduce with more up to date versions (see the StackBlitz).
The text was updated successfully, but these errors were encountered: