Skip to content

Commit

Permalink
fix(1508): inputTransformFn doesn't change the input form status
Browse files Browse the repository at this point in the history
Handle null inputTransformFn and outputTransformFn in ngx-mask service.
  • Loading branch information
BGBRWR committed Feb 20, 2025
1 parent 11602ee commit ccb5e1e
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 14 deletions.
6 changes: 4 additions & 2 deletions projects/ngx-mask-lib/src/lib/ngx-mask-applier.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,11 @@ export class NgxMaskApplierService {

public apm: NgxMaskConfig['apm'] = this._config.apm;

public inputTransformFn: NgxMaskConfig['inputTransformFn'] = this._config.inputTransformFn;
public inputTransformFn: NgxMaskConfig['inputTransformFn'] | null =
this._config.inputTransformFn;

public outputTransformFn: NgxMaskConfig['outputTransformFn'] = this._config.outputTransformFn;
public outputTransformFn: NgxMaskConfig['outputTransformFn'] | null =
this._config.outputTransformFn;

public keepCharacterPositions: NgxMaskConfig['keepCharacterPositions'] =
this._config.keepCharacterPositions;
Expand Down
13 changes: 6 additions & 7 deletions projects/ngx-mask-lib/src/lib/ngx-mask.directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,10 @@ export class NgxMaskDirective implements ControlValueAccessor, OnChanges, Valida
return;
}
const el: HTMLInputElement = e.target as HTMLInputElement;
const transformedValue = this._maskService.inputTransformFn(el.value);

const transformedValue = this._maskService.inputTransformFn
? this._maskService.inputTransformFn(el.value)
: el.value;

if (el.type !== 'number') {
if (typeof transformedValue === 'string' || typeof transformedValue === 'number') {
Expand Down Expand Up @@ -1011,18 +1014,14 @@ export class NgxMaskDirective implements ControlValueAccessor, OnChanges, Valida
(this._maskService.prefix || this._maskService.showMaskTyped))
) {
// Let the service we know we are writing value so that triggering onChange function won't happen during applyMask
if (typeof inputTransformFn !== 'function') {
this._maskService.writingValue = true;
}
this._maskService.writingValue = true;

this._maskService.formElementProperty = [
'value',
this._maskService.applyMask(inputValue, this._maskService.maskExpression),
];
// Let the service know we've finished writing value
if (typeof inputTransformFn !== 'function') {
this._maskService.writingValue = false;
}
this._maskService.writingValue = false;
} else {
this._maskService.formElementProperty = ['value', inputValue];
}
Expand Down
11 changes: 6 additions & 5 deletions projects/ngx-mask-lib/src/lib/ngx-mask.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -596,15 +596,16 @@ export class NgxMaskService extends NgxMaskApplierService {
* @param inputValue the current form input value
*/
private formControlResult(inputValue: string): void {
const outputTransformFn = this.outputTransformFn ? this.outputTransformFn : (v: any) => v;
if (this.writingValue && !inputValue) {
this.onChange(this.outputTransformFn(null));
this.onChange(outputTransformFn(null));
return;
}
if (this.writingValue || (!this.triggerOnMaskChange && this.maskChanged)) {
// eslint-disable-next-line no-unused-expressions,@typescript-eslint/no-unused-expressions
this.triggerOnMaskChange && this.maskChanged
? this.onChange(
this.outputTransformFn(
outputTransformFn(
this._toNumber(
this._checkSymbols(this._removeSuffix(this._removePrefix(inputValue)))
)
Expand All @@ -617,7 +618,7 @@ export class NgxMaskService extends NgxMaskApplierService {
}
if (Array.isArray(this.dropSpecialCharacters)) {
this.onChange(
this.outputTransformFn(
outputTransformFn(
this._toNumber(
this._checkSymbols(
this._removeMask(
Expand All @@ -633,14 +634,14 @@ export class NgxMaskService extends NgxMaskApplierService {
(!this.dropSpecialCharacters && this.prefix === inputValue)
) {
this.onChange(
this.outputTransformFn(
outputTransformFn(
this._toNumber(
this._checkSymbols(this._removeSuffix(this._removePrefix(inputValue)))
)
)
);
} else {
this.onChange(this.outputTransformFn(this._toNumber(inputValue)));
this.onChange(outputTransformFn(this._toNumber(inputValue)));
}
}

Expand Down

0 comments on commit ccb5e1e

Please sign in to comment.