Skip to content

Commit

Permalink
fix(1358): add trailing zero when mask="separator.1" and leadZero="tr…
Browse files Browse the repository at this point in the history
…ue" (#1359)

Currently, no trailing zero is added when mask="separator.1" and leadZero="true" and input loses focus. This change adds the trailing zero in that case.
  • Loading branch information
nathanielbuck authored May 8, 2024
1 parent a58224e commit 144323f
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 3 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# 17.0.8(2024-04-30)

### Fix

- Fix ([#1358](https://github.com/JsDaddy/ngx-mask/issues/1358))

# 17.0.7(2024-03-28)

### Fix
Expand Down
2 changes: 1 addition & 1 deletion projects/ngx-mask-lib/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ngx-mask",
"version": "17.0.7",
"version": "17.0.8",
"description": "awesome ngx mask",
"keywords": [
"ng2-mask",
Expand Down
2 changes: 1 addition & 1 deletion projects/ngx-mask-lib/src/lib/ngx-mask.directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -692,7 +692,7 @@ export class NgxMaskDirective implements ControlValueAccessor, OnChanges, Valida
maskExpression.length
)
);
if (precision > 1) {
if (precision > 0) {
el.value = this.suffix ? el.value.split(this.suffix).join('') : el.value;
const decimalPart = el.value.split(this.decimalMarker)[1] as string;
el.value = el.value.includes(this.decimalMarker)
Expand Down
2 changes: 1 addition & 1 deletion projects/ngx-mask-lib/src/lib/ngx-mask.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -713,7 +713,7 @@ export class NgxMaskService extends NgxMaskApplierService {
const separatorPrecision = separatorExpression.slice(10, 11);
if (
separatorExpression.indexOf('2') > 0 ||
(this.leadZero && Number(separatorPrecision) > 1)
(this.leadZero && Number(separatorPrecision) > 0)
) {
if (this.decimalMarker === MaskExpression.COMMA && this.leadZero) {
// eslint-disable-next-line no-param-reassign
Expand Down
24 changes: 24 additions & 0 deletions projects/ngx-mask-lib/src/test/emit-events.cy-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,30 @@ describe('Directive: Mask (emit-events)', () => {
cy.get('#pre').should('have.text', '7');
});

it('should add trailing zero when mask="separator.1" and leadZero="true"', () => {
cy.mount(CypressTestMaskComponent, {
componentProperties: {
mask: 'separator.1',
leadZero: true,
},
imports: [CypressTestMaskModule],
});

cy.get('#masked').type('9').blur().should('have.value', '9.0');
});

it('should keep trailing decimal when mask="separator.1" and leadZero="true"', () => {
cy.mount(CypressTestMaskComponent, {
componentProperties: {
mask: 'separator.1',
leadZero: true,
},
imports: [CypressTestMaskModule],
});

cy.get('#masked').type('7.7').blur().should('have.value', '7.7');
});

it('should emit event only when mask is correct with suffix separator2', () => {
cy.mount(CypressTestMaskComponent, {
componentProperties: {
Expand Down
51 changes: 51 additions & 0 deletions projects/ngx-mask-lib/src/test/separator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -588,6 +588,57 @@ describe('Separator: Mask', () => {
equal('0@', '0', fixture);
});

it('should add trailing zero when separator.1 and leadZero = true', fakeAsync(() => {
component.mask = 'separator.1';
component.leadZero = true;
const debugElement: DebugElement = fixture.debugElement.query(By.css('input'));
const inputTarget: HTMLInputElement = debugElement.nativeElement as HTMLInputElement;
spyOnProperty(document, 'activeElement').and.returnValue(inputTarget);
fixture.detectChanges();

component.form.setValue(0);
tick();
expect(inputTarget.value).toBe('0.0');

component.form.setValue(1);
tick();
expect(inputTarget.value).toBe('1.0');

component.form.setValue(88);
tick();
expect(inputTarget.value).toBe('88.0');

component.form.setValue(99.);

Check failure on line 611 in projects/ngx-mask-lib/src/test/separator.spec.ts

View workflow job for this annotation

GitHub Actions / quality-check

Delete `.`
tick();
expect(inputTarget.value).toBe('99.0');
}));

it('should not modify value with one decimal when separator.1 and leadZero = true', fakeAsync(() => {
component.mask = 'separator.1';
component.leadZero = true;
const debugElement: DebugElement = fixture.debugElement.query(By.css('input'));
const inputTarget: HTMLInputElement = debugElement.nativeElement as HTMLInputElement;
spyOnProperty(document, 'activeElement').and.returnValue(inputTarget);
fixture.detectChanges();

component.form.setValue(0.0);
tick();
expect(inputTarget.value).toBe('0.0');

component.form.setValue(1.0);
tick();
expect(inputTarget.value).toBe('1.0');

component.form.setValue(88.0);
tick();
expect(inputTarget.value).toBe('88.0');

component.form.setValue(99.9);
tick();
expect(inputTarget.value).toBe('99.9');
}));

Check failure on line 639 in projects/ngx-mask-lib/src/test/separator.spec.ts

View workflow job for this annotation

GitHub Actions / quality-check

Delete `⏎`


it('should display zeros at the end separator2', fakeAsync(() => {
component.mask = 'separator.2';
component.leadZero = true;
Expand Down

0 comments on commit 144323f

Please sign in to comment.