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

release #1479

Merged
merged 1 commit into from
Dec 13, 2024
Merged

release #1479

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
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
# 19.0.4(2024-12-13)

### Feature

- add input property instantPrefix

### Fix

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

# 19.0.3(2024-12-05)

### Contributing Fix
Expand Down
16 changes: 16 additions & 0 deletions USAGE.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,12 +132,28 @@ pattern = {

You can add prefix to you masked value


#### Usage

```html
<input type="text" prefix="+7" mask="(000) 000 00 00" />
```

### instantPrefix

The input property instantPrefix controls the display behavior of a prefix in the input.

When set to true, the prefix is displayed even if the model is empty.
When set to false, the prefix only becomes visible when a value is present in the model.

#### Usage

```html
<input type="text" prefix="+7" instantPrefix="false" mask="(000) 000 00 00" />
<input type="text" prefix="+7" instantPrefix="true" mask="(000) 000 00 00" />
```


### suffix (string)

You can add suffix to you masked value
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ngx-mask",
"version": "19.0.3",
"version": "19.0.4",
"description": "Awesome ngx mask",
"license": "MIT",
"engines": {
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": "19.0.3",
"version": "19.0.4",
"description": "awesome ngx mask",
"keywords": [
"ng2-mask",
Expand Down
4 changes: 3 additions & 1 deletion projects/ngx-mask-lib/src/lib/ngx-mask-applier.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ export class NgxMaskApplierService {
public keepCharacterPositions: NgxMaskConfig['keepCharacterPositions'] =
this._config.keepCharacterPositions;

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

private _shift = new Set<number>();

public plusOnePosition = false;
Expand Down Expand Up @@ -777,7 +779,7 @@ export class NgxMaskApplierService {
}`;

if (result.length === 0) {
res = !this.dropSpecialCharacters ? `${this.prefix}${result}` : `${result}`;
res = this.instantPrefix ? `${this.prefix}${result}` : `${result}`;
}

const isSpecialCharacterMaskFirstSymbol =
Expand Down
2 changes: 2 additions & 0 deletions projects/ngx-mask-lib/src/lib/ngx-mask.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export type NgxMaskConfig = {
dropSpecialCharacters: boolean | string[] | readonly string[];
hiddenInput: boolean;
validation: boolean;
instantPrefix: boolean;
separatorLimit: string;
apm: boolean;
allowNegativeNumbers: boolean;
Expand Down Expand Up @@ -50,6 +51,7 @@ export const initialConfig: NgxMaskConfig = {
decimalMarker: ['.', ','],
clearIfNotMatch: false,
showMaskTyped: false,
instantPrefix: false,
placeHolderCharacter: '_',
dropSpecialCharacters: true,
hiddenInput: false,
Expand Down
8 changes: 7 additions & 1 deletion projects/ngx-mask-lib/src/lib/ngx-mask.directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ export class NgxMaskDirective implements ControlValueAccessor, OnChanges, Valida
public inputTransformFn = input<NgxMaskConfig['inputTransformFn'] | null>(null);
public outputTransformFn = input<NgxMaskConfig['outputTransformFn'] | null>(null);
public keepCharacterPositions = input<NgxMaskConfig['keepCharacterPositions'] | null>(null);
public instantPrefix = input<NgxMaskConfig['instantPrefix'] | null>(null);

public maskFilled = output<void>();

Expand Down Expand Up @@ -107,6 +108,7 @@ export class NgxMaskDirective implements ControlValueAccessor, OnChanges, Valida
inputTransformFn,
outputTransformFn,
keepCharacterPositions,
instantPrefix,
} = changes;
if (mask) {
if (mask.currentValue !== mask.previousValue && !mask.firstChange) {
Expand Down Expand Up @@ -147,6 +149,10 @@ export class NgxMaskDirective implements ControlValueAccessor, OnChanges, Valida
if (apm && apm.currentValue) {
this._maskService.apm = apm.currentValue;
}

if (instantPrefix) {
this._maskService.instantPrefix = instantPrefix.currentValue;
}
if (prefix) {
this._maskService.prefix = prefix.currentValue;
}
Expand Down Expand Up @@ -966,7 +972,7 @@ export class NgxMaskDirective implements ControlValueAccessor, OnChanges, Valida
this._maskService.isNumberValue = true;
}

if (typeof inputValue !== 'string') {
if (typeof inputValue !== 'string' || value === null || typeof value === 'undefined') {
inputValue = '';
}

Expand Down
48 changes: 48 additions & 0 deletions projects/ngx-mask-lib/src/test/add-prefix.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,4 +159,52 @@ describe('Directive: Mask (Add prefix)', () => {
equal('KZ123123', 'KZ123 123', fixture);
expect(component.form.value).toBe('KZ123 123');
});

it('should show prefix if value is empty mask 0000', () => {
component.mask.set('0000');
component.prefix.set('+38 ');
component.instantPrefix.set(true);

equal('', '+38 ', fixture);
equal('1', '+38 1', fixture);
equal('12', '+38 12', fixture);
equal('123', '+38 123', fixture);
equal('1234', '+38 1234', fixture);
});

it('should show prefix if value is empty mask 0000 with dropSpecialCharacter false', () => {
component.mask.set('0000');
component.prefix.set('+38 ');
component.dropSpecialCharacters.set(false);
component.instantPrefix.set(true);

equal('', '+38 ', fixture);
equal('1', '+38 1', fixture);
equal('12', '+38 12', fixture);
equal('123', '+38 123', fixture);
equal('1234', '+38 1234', fixture);
});

it('should doesnt show prefix if value is empty mask 0000', () => {
component.mask.set('0000');
component.prefix.set('+38 ');

equal('', '', fixture);
equal('1', '+38 1', fixture);
equal('12', '+38 12', fixture);
equal('123', '+38 123', fixture);
equal('1234', '+38 1234', fixture);
});

it('should doesnt show prefix if value is empty mask 0000 with dropSpecialCharacter false', () => {
component.mask.set('0000');
component.prefix.set('+38 ');
component.dropSpecialCharacters.set(false);

equal('', '', fixture);
equal('1', '+38 1', fixture);
equal('12', '+38 12', fixture);
equal('123', '+38 123', fixture);
equal('1234', '+38 1234', fixture);
});
});
11 changes: 10 additions & 1 deletion projects/ngx-mask-lib/src/test/basic-logic.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -648,7 +648,7 @@ describe('Directive: Mask', () => {
expect(component.form.value).toBe('foo/257');
expect(component.form.valid).toBeFalse();

equal('', 'foo/', fixture);
equal('', '', fixture);
expect(component.form.value).toBe('');
});

Expand Down Expand Up @@ -1028,4 +1028,13 @@ describe('Directive: Mask', () => {
equal('122', '12.2', fixture);
equal('12.22', '12.2.', fixture);
});

it('should show default state after reset control 0000', () => {
component.mask.set('0000');

equal('1234', '1234', fixture);
component.form.reset();
expect(component.form.dirty).toBe(false);
expect(component.form.pristine).toBe(true);
});
});
31 changes: 31 additions & 0 deletions projects/ngx-mask-lib/src/test/separator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1871,4 +1871,35 @@ describe('Separator: Mask', () => {
equal('12345678910111215.9999', '12,345,678,910,111,215.99', fixture);
expect(component.form.value).toBe('12345678910111215.99');
});

it('should show default state after reset control separator.2', () => {
component.mask.set('separator.2');
component.thousandSeparator.set(',');

equal('1234', '1,234', fixture);
component.form.reset();
expect(component.form.dirty).toBe(false);
expect(component.form.pristine).toBe(true);
});

it('should show default state after reset control separator.0', () => {
component.mask.set('separator.0');
component.thousandSeparator.set(',');

equal('1234', '1,234', fixture);
component.form.reset();
expect(component.form.dirty).toBe(false);
expect(component.form.pristine).toBe(true);
});

it('should show default state after reset control separator.2 and leadZero', () => {
component.mask.set('separator.2');
component.thousandSeparator.set(',');
component.leadZero.set(true);

equal('1234', '1,234', fixture);
component.form.reset();
expect(component.form.dirty).toBe(false);
expect(component.form.pristine).toBe(true);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import { NgxMaskDirective } from 'ngx-mask';
[apm]="apm()"
[validation]="validation()"
[inputTransformFn]="inputTransformFn()"
[instantPrefix]="instantPrefix()"
[outputTransformFn]="outputTransformFn()"
[triggerOnMaskChange]="triggerOnMaskChange()" />
`,
Expand Down Expand Up @@ -84,4 +85,5 @@ export class TestMaskComponent {
public triggerOnMaskChange = signal<NgxMaskConfig['triggerOnMaskChange']>(
this._config.triggerOnMaskChange
);
public instantPrefix = signal<NgxMaskConfig['instantPrefix']>(this._config.instantPrefix);
}
Loading