Skip to content

Commit 731bdf0

Browse files
author
igor.nepipenko
committed
feat(ref:no-ref): ng21 v6
1 parent d2703f9 commit 731bdf0

File tree

4 files changed

+89
-4
lines changed

4 files changed

+89
-4
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
### fix
44

55
- Fix ([#1590](https://github.com/JsDaddy/ngx-mask/issues/1590))
6+
- Fix ([#1591](https://github.com/JsDaddy/ngx-mask/issues/1591))
67

78
# 20.0.3(2025-08-01)
89

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -272,8 +272,17 @@ export class NgxMaskService extends NgxMaskApplierService {
272272
}
273273

274274
// Propagate the input value back to the Angular model
275-
// eslint-disable-next-line no-unused-expressions,@typescript-eslint/no-unused-expressions
276-
this._emitValue ? this.formControlResult(result) : '';
275+
// Only emit if:
276+
// 1. _emitValue is true (value changed), AND
277+
// 2. Either mask didn't change, OR triggerOnMaskChange is true
278+
const shouldEmit = this._emitValue && (!this.maskChanged || this.triggerOnMaskChange);
279+
if (shouldEmit) {
280+
this.formControlResult(result);
281+
}
282+
// Reset maskChanged flag even if we didn't emit
283+
if (this.maskChanged && !this.triggerOnMaskChange) {
284+
this.maskChanged = false;
285+
}
277286

278287
// Handle hidden input and showMaskTyped
279288
if (!this.showMaskTyped || (this.showMaskTyped && this.hiddenInput)) {

projects/ngx-mask-lib/src/test/separator-3.spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,7 @@ describe('Separator: Mask', () => {
455455
component.mask.set('separator.2');
456456
component.thousandSeparator.set(' ');
457457
component.decimalMarker.set(',');
458+
fixture.detectChanges();
458459

459460
const inputElement = fixture.nativeElement.querySelector('input');
460461
inputElement.value = '100000,00';

projects/ngx-mask-lib/src/test/trigger-on-mask-change.spec.ts

Lines changed: 76 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,10 @@ describe('Directive: Mask (Trigger on mask change)', () => {
6262
fixture.detectChanges();
6363
await fixture.whenStable();
6464
inputEl = fixture.debugElement.query(By.css('input'));
65+
// Display is formatted with new mask
6566
expect(inputEl.nativeElement.value).equal('79 123 45 67');
66-
expect(component.form.value).equal('791234567');
67+
// But form value should NOT be updated when triggerOnMaskChange is false
68+
expect(component.form.value).equal('7912345678');
6769
});
6870

6971
it('should trigger form value update if mask is changed when triggerOnMaskChange is true', async () => {
@@ -97,6 +99,78 @@ describe('Directive: Mask (Trigger on mask change)', () => {
9799

98100
component.mask.set('S0S 0S0');
99101
await equal(inputTarget.value, '', fixture, true);
100-
expect(component.form.value).equal('');
102+
// Form value should NOT be updated when triggerOnMaskChange is false
103+
expect(component.form.value).equal('1234');
104+
});
105+
106+
it('should not mark form as dirty when mask changes and triggerOnMaskChange is false', async () => {
107+
component.mask.set('0000');
108+
component.triggerOnMaskChange.set(false);
109+
fixture.detectChanges();
110+
111+
// Set value programmatically
112+
component.form.setValue('1234');
113+
fixture.detectChanges();
114+
await fixture.whenStable();
115+
116+
// Reset to pristine state (setValue may trigger onChange in some cases)
117+
component.form.markAsPristine();
118+
expect(component.form.pristine).equal(true);
119+
120+
// Change the mask - this should NOT mark form as dirty
121+
component.mask.set('00-00');
122+
fixture.detectChanges();
123+
await fixture.whenStable();
124+
125+
// Form should still be pristine after mask change when triggerOnMaskChange is false
126+
expect(component.form.pristine).equal(true);
127+
expect(component.form.dirty).equal(false);
128+
});
129+
130+
it('should not mark form as dirty when mask changes from empty to pattern and triggerOnMaskChange is false', async () => {
131+
component.mask.set('');
132+
component.triggerOnMaskChange.set(false);
133+
fixture.detectChanges();
134+
135+
// Set value programmatically
136+
component.form.setValue('12345678');
137+
fixture.detectChanges();
138+
await fixture.whenStable();
139+
140+
// Reset to pristine state
141+
component.form.markAsPristine();
142+
expect(component.form.pristine).equal(true);
143+
144+
// Apply a mask to the existing value - should NOT mark as dirty
145+
component.mask.set('0000-0000');
146+
fixture.detectChanges();
147+
await fixture.whenStable();
148+
149+
// Form should still be pristine
150+
expect(component.form.pristine).equal(true);
151+
expect(component.form.dirty).equal(false);
152+
});
153+
154+
it('should not mark form as dirty when mask changes with phone pattern and triggerOnMaskChange is false', async () => {
155+
component.mask.set('(000) 000-0000');
156+
component.triggerOnMaskChange.set(false);
157+
fixture.detectChanges();
158+
159+
component.form.setValue('1234567890');
160+
fixture.detectChanges();
161+
await fixture.whenStable();
162+
163+
// Reset to pristine state
164+
component.form.markAsPristine();
165+
expect(component.form.pristine).equal(true);
166+
167+
// Change to a different phone mask format - should NOT mark as dirty
168+
component.mask.set('000-000-0000');
169+
fixture.detectChanges();
170+
await fixture.whenStable();
171+
172+
// Should remain pristine when triggerOnMaskChange is false
173+
expect(component.form.pristine).equal(true);
174+
expect(component.form.dirty).equal(false);
101175
});
102176
});

0 commit comments

Comments
 (0)