Skip to content

Commit e400c4a

Browse files
author
igor.nepipenko
committed
fix(ref:no-ref): add first tests for demo
1 parent 73b001a commit e400c4a

File tree

4 files changed

+1413
-10
lines changed

4 files changed

+1413
-10
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,5 @@ test-reports/
5555

5656
# Snyk
5757
.dccache
58+
59+
.output.txt

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

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,11 @@ export class NgxMaskDirective
110110
public constructor() {
111111
effect(() => {
112112
const signalValue = this.value();
113-
if (this._isSignalFormsMode() && signalValue !== untracked(() => this._inputValue())) {
113+
if (this._isSignalFormsMode()) {
114114
untracked(() => {
115-
this.writeValue(signalValue);
115+
if (String(signalValue) !== String(this._inputValue())) {
116+
this.writeValue(signalValue);
117+
}
116118
});
117119
}
118120
});
@@ -448,6 +450,7 @@ export class NgxMaskDirective
448450
@HostListener('focus')
449451
public onFocus(): void {
450452
this._isFocused.set(true);
453+
this._maskService._isFocused.set(true);
451454
}
452455

453456
@HostListener('ngModelChange', ['$event'])
@@ -781,18 +784,22 @@ export class NgxMaskDirective
781784

782785
el.value = el.value.includes(decimalMarker)
783786
? el.value +
784-
MaskExpression.NUMBER_ZERO.repeat(precision - decimalPart.length) +
787+
MaskExpression.NUMBER_ZERO.repeat(
788+
precision - (decimalPart?.length || 0)
789+
) +
785790
suffix
786791
: el.value +
787792
decimalMarker +
788793
MaskExpression.NUMBER_ZERO.repeat(precision) +
789794
suffix;
790795
this._maskService.actualValue = el.value;
796+
this.onChange(this._maskService.actualValue);
791797
}
792798
}
793799
this._maskService.clearIfNotMatchFn();
794800
}
795801
this._isFocused.set(false);
802+
this._maskService._isFocused.set(false);
796803
this.onTouch();
797804
}
798805

@@ -1072,6 +1079,16 @@ export class NgxMaskDirective
10721079
this._inputValue.set(inputValue);
10731080
this._setMask();
10741081

1082+
if (this._isSignalFormsMode()) {
1083+
untracked(() => {
1084+
const stringValue =
1085+
value === null || typeof value === 'undefined' ? '' : String(value);
1086+
if (String(this.value()) !== stringValue) {
1087+
this.value.set(stringValue);
1088+
}
1089+
});
1090+
}
1091+
10751092
if (
10761093
(inputValue && this._maskService.maskExpression) ||
10771094
(this._maskService.maskExpression &&
@@ -1087,11 +1104,18 @@ export class NgxMaskDirective
10871104
// Let the service know we've finished writing value
10881105
this._maskService.writingValue = false;
10891106
this._maskService.isInitialized = true;
1107+
if (this._isSignalFormsMode()) {
1108+
untracked(() => {
1109+
const actualValue = this._maskService.actualValue;
1110+
if (String(this.value()) !== actualValue) {
1111+
this.value.set(actualValue);
1112+
}
1113+
});
1114+
}
10901115
} else {
10911116
this._maskService.formElementProperty = ['value', inputValue];
10921117
this._maskService.isInitialized = true;
10931118
}
1094-
this._inputValue.set(inputValue);
10951119
} else {
10961120
// eslint-disable-next-line no-console
10971121
console.warn(
@@ -1110,9 +1134,11 @@ export class NgxMaskDirective
11101134
if (this._isSignalFormsMode()) {
11111135
const stringValue =
11121136
value === null || typeof value === 'undefined' ? '' : String(value);
1113-
if (this.value() !== stringValue) {
1114-
this.value.set(stringValue);
1115-
}
1137+
untracked(() => {
1138+
if (String(this.value()) !== stringValue) {
1139+
this.value.set(stringValue);
1140+
}
1141+
});
11161142
}
11171143
};
11181144
}

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ElementRef, inject, Injectable, Renderer2 } from '@angular/core';
1+
import { ElementRef, inject, Injectable, Renderer2, signal } from '@angular/core';
22
import { DOCUMENT } from '@angular/common';
33

44
import type { NgxMaskConfig } from './ngx-mask.config';
@@ -23,6 +23,8 @@ export class NgxMaskService extends NgxMaskApplierService {
2323
public writingValue = false;
2424
public isInitialized = false;
2525

26+
public _isFocused = signal<boolean>(false);
27+
2628
private _emitValue = false;
2729
private _start!: number;
2830
private _end!: number;
@@ -806,8 +808,11 @@ export class NgxMaskService extends NgxMaskApplierService {
806808
let value = separatorValue;
807809

808810
if (
809-
separatorExpression.indexOf('2') > 0 ||
810-
(this.leadZero && Number(separatorPrecision) > 0 && Number.isFinite(separatorPrecision))
811+
(separatorExpression.indexOf('2') > 0 && !this._isFocused()) ||
812+
(this.leadZero &&
813+
!this._isFocused() &&
814+
Number(separatorPrecision) > 0 &&
815+
Number.isFinite(separatorPrecision))
811816
) {
812817
if (this.decimalMarker === MaskExpression.COMMA && this.leadZero) {
813818
value = value.replace(',', '.');

0 commit comments

Comments
 (0)