Skip to content
This repository has been archived by the owner on Nov 3, 2022. It is now read-only.

Commit

Permalink
Patch (JsDaddy#907)
Browse files Browse the repository at this point in the history
* fix(JsDaddy#892): check if input is a string

* fix(JsDaddy#882): remove mask before getting newInputValue

* fix(form-reset): update actualValue on form reset

* fix(css): no more absolute divs blocking showcase
  • Loading branch information
BGBRWR authored and Manuel Meister committed Jan 3, 2022
1 parent 679ec2b commit 1b5932e
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 9 deletions.
4 changes: 2 additions & 2 deletions projects/ngx-mask-lib/src/lib/mask-applier.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,14 @@ export class MaskApplierService {
}

public applyMask(
inputValue: string,
inputValue: string | object | boolean | null | undefined,
maskExpression: string,
position: number = 0,
justPasted: boolean = false,
backspaced: boolean = false,
cb: (shift: number, backspaceShift: boolean) => void = () => {}
): string {
if (inputValue === undefined || inputValue === null || maskExpression === undefined) {
if (!maskExpression || typeof inputValue !== 'string') {
return '';
}
let cursor = 0;
Expand Down
15 changes: 12 additions & 3 deletions projects/ngx-mask-lib/src/lib/mask.directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,14 @@ export class MaskDirective implements ControlValueAccessor, OnChanges, Validator
this._maskService.selEnd = el.selectionEnd;
}

@HostListener('ngModelChange', ['$event'])
public onModelChange(value: any): void {
// on form reset we need to update the actualValue
if (!value && this._maskService.actualValue) {
this._maskService.actualValue = this._maskService.getActualValue('');
}
}

@HostListener('paste')
public onPaste() {
this._justPasted = true;
Expand Down Expand Up @@ -439,15 +447,16 @@ export class MaskDirective implements ControlValueAccessor, OnChanges, Validator
inputValue = inputValue.value;
}

if (inputValue === undefined) {
inputValue = '';
}
if (typeof inputValue === 'number') {
inputValue = String(inputValue);
inputValue = this.decimalMarker !== '.' ? inputValue.replace('.', this.decimalMarker) : inputValue;
this._maskService.isNumberValue = true;
}

if (typeof inputValue !== 'string') {
inputValue = '';
}

this._inputValue = inputValue;
this._setMask();

Expand Down
3 changes: 3 additions & 0 deletions projects/ngx-mask-lib/src/lib/mask.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ export class MaskService extends MaskApplierService {
} else {
actualResult = [];
}
if (this.showMaskTyped) {
inputValue = this.removeMask(inputValue);
}
newInputValue =
this.actualValue.length && actualResult.length <= inputValue.length
? this.shiftTypedSymbols(actualResult.join(''))
Expand Down
22 changes: 22 additions & 0 deletions projects/ngx-mask-lib/src/test/secure-mask.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,4 +122,26 @@ describe('Directive: Mask (Secure)', () => {
expect(fixture.nativeElement.querySelector('input').value).toBe('123/45/6789');
});
});

it('should not keep shadow copy when form reset', () => {
component.hiddenInput = true;
component.mask = 'XXX/X0/0000';
equal('54321', '***/*1', fixture);
typeTest('1', fixture);
expect(component.form.value).toBe('1');
component.form.reset();
expect(component.form.value).toBe(null);
equal('2', '*', fixture);
expect(component.form.value).toBe('2');
});

it('mask changes should work with null input', () => {
component.hiddenInput = true;
component.mask = '000/00/0000';
equal('987654321', '987/65/4321', fixture);
component.form.reset();
component.mask = 'XXX/X0/0000';
equal('54321', '***/*1', fixture);
expect(component.form.value).toBe('54321');
});
});
2 changes: 1 addition & 1 deletion src/app/bugs/bugs.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@
type="text"
placeholder="Secure input"
[hiddenInput]="true"
mask="XXX-X0-0000"
[mask]="mask"
formControlName="secureInput"
id="SecureInput"
/>
Expand Down
20 changes: 17 additions & 3 deletions src/app/bugs/bugs.component.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import { Component, OnDestroy } from '@angular/core';
import { Component, OnInit, OnDestroy } from '@angular/core';
import { FormBuilder, FormGroup } from '@angular/forms';
import { Subject } from 'rxjs';
import { takeUntil } from 'rxjs/operators';

@Component({
templateUrl: './bugs.component.html',
})
export class BugsComponent implements OnDestroy {
export class BugsComponent implements OnInit, OnDestroy {
public bugsForm: FormGroup;
public submitted: boolean = false;

public mask = 'XXX-XX-XXXX';
// Can be used as a takeUntil for any observables this component may subscribe to. e.g. a form control valueChanges
private onDestroy$ = new Subject();

Expand All @@ -28,6 +29,19 @@ export class BugsComponent implements OnDestroy {
this.onDestroy$.complete();
}

ngOnInit(): void {
this.bugsForm
.get('SecureInput')
?.valueChanges.pipe(takeUntil(this.onDestroy$))
.subscribe((value) => {
if (this.bugsForm.get('SecureInput')?.valid) {
this.mask = '000-00-0000';
} else {
this.mask = 'XXX-X0-0000';
}
});
}

submitForm(): void {
this.submitted = true;
}
Expand Down
1 change: 1 addition & 0 deletions src/styles.scss
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ body {

.mat-grid-list,
.mat-grid-tile,
.mat-grid-tile-content,
.mat-grid-tile .mat-figure {
position: inherit !important;
}
Expand Down

0 comments on commit 1b5932e

Please sign in to comment.