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

fix(hidden-input) input programmatically value set without phantom values #864

Merged
merged 4 commits into from
Mar 19, 2021
Merged
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
9 changes: 6 additions & 3 deletions projects/ngx-mask-lib/src/lib/mask.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export class MaskService extends MaskApplierService {
}
const getSymbol: string = !!inputValue && typeof this.selStart === 'number' ? inputValue[this.selStart] : '';
let newInputValue = '';
if (this.hiddenInput !== undefined) {
if (this.hiddenInput && !this.writingValue) {
let actualResult: string[] = this.actualValue.split('');
// tslint:disable no-unused-expression
inputValue !== '' && actualResult.length
Expand All @@ -70,7 +70,10 @@ export class MaskService extends MaskApplierService {
: null
: (actualResult = []);
// tslint:enable no-unused-expression
newInputValue = this.actualValue.length ? this.shiftTypedSymbols(actualResult.join('')) : inputValue;
newInputValue =
this.actualValue.length && actualResult.length <= inputValue.length
? this.shiftTypedSymbols(actualResult.join(''))
: inputValue;
}
newInputValue = Boolean(newInputValue) && newInputValue.length ? newInputValue : inputValue;
const result: string = super.applyMask(newInputValue, maskExpression, position, justPasted, backspaced, cb);
Expand Down Expand Up @@ -333,7 +336,7 @@ export class MaskService extends MaskApplierService {
}

private _toNumber(value: string | number | undefined | null) {
if (!this.isNumberValue) {
if (!this.isNumberValue || value === '') {
return value;
}
const num = Number(value);
Expand Down
30 changes: 29 additions & 1 deletion projects/ngx-mask-lib/src/test/secure-mask.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { ReactiveFormsModule } from '@angular/forms';

import { NgxMaskModule } from '../lib/ngx-mask.module';
import { TestMaskComponent } from './utils/test-component.component';
import { equal } from './utils/test-functions.component';
import { equal, typeTest } from './utils/test-functions.component';

describe('Directive: Mask (Secure)', () => {
let fixture: ComponentFixture<TestMaskComponent>;
Expand Down Expand Up @@ -60,4 +60,32 @@ describe('Directive: Mask (Secure)', () => {
equal('123456789', '1234-*6-***', fixture);
expect(component.form.value).toBe('123456789');
});

it('it checks secure input functionality on reset', () => {
component.mask = 'XXX/X0/0000';
component.hiddenInput = true;
typeTest('54321', fixture);
component.form.reset('98765');
fixture.whenStable().then(() => {
expect(fixture.nativeElement.querySelector('input').value).toBe('***/*5');
});
});

it('it checks secure input functionality on reset then typed', () => {
component.mask = 'XXX/X0/0000';
component.hiddenInput = true;
typeTest('54321', fixture);
component.form.reset();
equal('98765', '***/*5', fixture);
});

it('it checks secure input functionality on setValue(longer string)', () => {
component.mask = 'XXX/X0/0000';
component.hiddenInput = true;
typeTest('54321', fixture);
component.form.setValue('1234567');
fixture.whenStable().then(() => {
expect(fixture.nativeElement.querySelector('input').value).toBe('***/*5/67');
});
});
});
25 changes: 25 additions & 0 deletions src/app/bugs/bugs.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,31 @@
</div>
</div>

<div class="container box">
<div class="row">
<div class="col-12">
<label for="SecureInput">Form reset on hidden mask</label>
<div>
<p>
<input
type="text"
placeholder="Secure input"
[hiddenInput]="true"
mask="XXX-X0-0000"
formControlName="SecureInput"
id="SecureInput"
/>
</p>
<p>
Valid: {{ bugsForm.controls.SecureInput.valid }}<br />
Value: {{ bugsForm.controls.SecureInput.value }}<br />
Errors: {{ bugsForm.controls.SecureInput.errors | json }}
</p>
</div>
</div>
</div>
</div>

<div class="container box">
<div class="row">
<div class="col-12">
Expand Down
1 change: 1 addition & 0 deletions src/app/bugs/bugs.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export class BugsComponent implements OnInit, OnDestroy {
DecMarkerComma: [],
DecMarkerDot: [],
CorrectRemovingSpace: [1200300.99],
SecureInput: [987654321],
});
}

Expand Down