Skip to content

Commit

Permalink
feat: add package ngx-mask
Browse files Browse the repository at this point in the history
* to enable masked inputs with needed pattern
  • Loading branch information
SGrueber committed Apr 18, 2024
1 parent 0b24445 commit ddf650f
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 3 deletions.
2 changes: 1 addition & 1 deletion docs/guides/formly.md
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ Refer to the tables below for an overview of these parts.

| Name | Description | Relevant props |
| --------------------------- | -------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| ish-text-input-field | Basic input field, supports all text types | type: 'text' (default),'email','tel','password' |
| ish-text-input-field | Basic input field, supports all text types | `type`: 'text (default),'email','tel','password'. `mask`: input mask for a needed pattern (see [ngx-mask](https://www.npmjs.com/package/ngx-mask) for more information) |
| ish-select-field | Basic select field | `options`: `{ value: any; label: string}[]` or Observable. `placeholder`: Translation key or string for the default selection |
| ish-textarea-field | Basic textarea field | `cols` & `rows`: Specifies the dimensions of the textarea |
| ish-checkbox-field | Basic checkbox input | `title`: Title for a checkbox |
Expand Down
14 changes: 14 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@
"morgan": "^1.10.0",
"ng-recaptcha": "^12.0.2",
"ngx-infinite-scroll": "^16.0.0",
"ngx-mask": "^16.4.2",
"ngx-toastr": "^18.0.0",
"pm2": "^5.3.1",
"rxjs": "~7.8.1",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,7 @@
[ngClass]="props.inputClass"
[attr.data-testing-id]="field.key"
[attr.aria-label]="props.ariaLabel"
[mask]="props.mask"
[decimalMarker]="decimalMarker"
[thousandSeparator]="thousandSeparator"
/>
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { FormGroup } from '@angular/forms';
import { FormlyFieldConfig, FormlyModule } from '@ngx-formly/core';
import { TranslateModule } from '@ngx-translate/core';

import { FormlyTestingComponentsModule } from 'ish-shared/formly/dev/testing/formly-testing-components.module';
import { FormlyTestingContainerComponent } from 'ish-shared/formly/dev/testing/formly-testing-container/formly-testing-container.component';
Expand All @@ -24,6 +25,7 @@ describe('Text Input Field Component', () => {
],
}),
FormlyTestingComponentsModule,
TranslateModule.forRoot(),
],
}).compileComponents();
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,32 @@
import { ChangeDetectionStrategy, Component } from '@angular/core';
import { NumberSymbol, formatNumber, getLocaleNumberSymbol } from '@angular/common';
import { ChangeDetectionStrategy, Component, OnInit } from '@angular/core';
import { FieldType, FieldTypeConfig, FormlyFieldConfig } from '@ngx-formly/core';
import { TranslateService } from '@ngx-translate/core';
import { provideNgxMask } from 'ngx-mask';

/**
* Type for a basic input field
*
* @props **ariaLabel** adds an aria-label to the component for better accessibility, recommended if there is no associated label
* @props **type** supports all text types; 'text' (default), 'email', 'password', 'tel'
* @props **mask** adds a ngx-mask mask (https://www.npmjs.com/package/ngx-mask) to mask the input with a given pattern
*
* @defaultWrappers form-field-horizontal & validation
*/
@Component({
selector: 'ish-text-input-field',
templateUrl: './text-input-field.component.html',
changeDetection: ChangeDetectionStrategy.OnPush,
providers: [provideNgxMask()],
})
export class TextInputFieldComponent extends FieldType<FieldTypeConfig> {
export class TextInputFieldComponent extends FieldType<FieldTypeConfig> implements OnInit {
textInputFieldTypes = ['text', 'email', 'password', 'tel'];
thousandSeparator: string;
decimalMarker: ',' | '.';

constructor(private translateService: TranslateService) {
super();
}

onPopulate(field: FormlyFieldConfig) {
if (!field.props?.type) {
Expand All @@ -29,4 +40,19 @@ export class TextInputFieldComponent extends FieldType<FieldTypeConfig> {
);
}
}

ngOnInit(): void {
if (this.props?.mask?.startsWith('separator')) {
if (this.props?.mask === 'separator.2' && this.formControl?.value) {
this.formControl.setValue(formatNumber(this.formControl?.value, 'en_US', '1.2-2').replace(',', ''));
}
this.thousandSeparator = getLocaleNumberSymbol(
this.translateService.currentLang,
NumberSymbol.CurrencyGroup
).trim();
this.decimalMarker = getLocaleNumberSymbol(this.translateService.currentLang, NumberSymbol.CurrencyDecimal) as
| '.'
| ',';
}
}
}
4 changes: 4 additions & 0 deletions src/app/shared/formly/types/types.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
import { FormlyModule as FormlyBaseModule } from '@ngx-formly/core';
import { FormlySelectModule } from '@ngx-formly/core/select';
import { TranslateModule, TranslateService } from '@ngx-translate/core';
import { NgxMaskDirective, NgxMaskPipe, provideNgxMask } from 'ngx-mask';
import { CaptchaExportsModule } from 'src/app/extensions/captcha/exports/captcha-exports.module';

import { DirectivesModule } from 'ish-core/directives.module';
Expand Down Expand Up @@ -53,6 +54,8 @@ const fieldComponents = [
FormlySelectModule,
IconModule,
NgbDatepickerModule,
NgxMaskDirective,
NgxMaskPipe,
ReactiveFormsModule,
TranslateModule,

Expand Down Expand Up @@ -167,6 +170,7 @@ const fieldComponents = [
}),
],
providers: [
provideNgxMask(),
{ provide: NgbDateParserFormatter, useClass: LocalizedParserFormatter, deps: [TranslateService] },
{ provide: NgbDateAdapter, useClass: NgbDateNativeAdapter },
{ provide: NgbDatepickerI18n, useClass: IshDatepickerI18n },
Expand Down

0 comments on commit ddf650f

Please sign in to comment.