Skip to content

Commit d4a91f3

Browse files
committed
fix(forms): Propagate masked value to form control value.
Ensure that the masked value is propagated to the form control value when "inputTransformFn" is not an instance variable in the component.
1 parent 915a4a1 commit d4a91f3

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

projects/ngx-mask-lib/src/lib/ngx-mask.directive.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -927,7 +927,7 @@ export class NgxMaskDirective implements ControlValueAccessor, OnChanges, Valida
927927
/** It writes the value in the input */
928928
public async writeValue(controlValue: unknown): Promise<void> {
929929
let value = controlValue;
930-
const inputTransformFn = this.inputTransformFn();
930+
const inputTransformFn = this._maskService.inputTransformFn;
931931
if (typeof value === 'object' && value !== null && 'value' in value) {
932932
if ('disable' in value) {
933933
this.setDisabledState(Boolean(value.disable));
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { Component } from '@angular/core';
2+
import type { ComponentFixture } from '@angular/core/testing';
3+
import { TestBed } from '@angular/core/testing';
4+
import { FormControl, ReactiveFormsModule } from '@angular/forms';
5+
import { NgxMaskDirective, provideNgxMask } from 'ngx-mask';
6+
7+
@Component({
8+
selector: 'jsdaddy-open-source-test',
9+
imports: [ReactiveFormsModule, NgxMaskDirective],
10+
template: `<input mask="0000" [formControl]="form" />`,
11+
})
12+
class TestMaskComponent {
13+
public form: FormControl = new FormControl('');
14+
}
15+
16+
describe('Directive: Forms', () => {
17+
let fixture: ComponentFixture<TestMaskComponent>;
18+
let component: TestMaskComponent;
19+
20+
beforeEach(() => {
21+
TestBed.configureTestingModule({
22+
imports: [NgxMaskDirective],
23+
providers: [provideNgxMask()],
24+
});
25+
fixture = TestBed.createComponent(TestMaskComponent);
26+
component = fixture.componentInstance;
27+
fixture.detectChanges();
28+
});
29+
30+
it('should propagate masked value to the form control value', () => {
31+
component.form.setValue('A1234Z');
32+
expect(component.form.value).toBe('1234');
33+
});
34+
35+
it('should propagate masked value to the form control valueChanges observable', () => {
36+
component.form.valueChanges.subscribe((newValue) => {
37+
expect(newValue).toEqual('1234');
38+
});
39+
40+
component.form.setValue('A1234Z');
41+
});
42+
});

0 commit comments

Comments
 (0)