Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Patch #907

Merged
merged 4 commits into from
Jul 30, 2021
Merged

Patch #907

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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: Function = () => {}
): 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 @@ -245,6 +245,14 @@ export class MaskDirective implements ControlValueAccessor, OnChanges, Validator
this._justPasted = true;
}

@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('input', ['$event'])
public onInput(e: CustomKeyboardEvent): void {
const el: HTMLInputElement = e.target as HTMLInputElement;
Expand Down Expand Up @@ -438,15 +446,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 @@ -70,6 +70,9 @@ export class MaskService extends MaskApplierService {
: null
: null
: (actualResult = []);
if (this.showMaskTyped) {
inputValue = this.removeMask(inputValue);
}
// tslint:enable no-unused-expression
newInputValue =
this.actualValue.length && actualResult.length <= inputValue.length
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
16 changes: 14 additions & 2 deletions src/app/bugs/bugs.component.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
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 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<void>();

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

ngOnInit(): void {}
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