Skip to content

Commit

Permalink
Fix #13086 - KeyFilter.pattern type (#13087)
Browse files Browse the repository at this point in the history
Co-authored-by: Davide Menegatti <davide.menegatti@theoceanly.com>
  • Loading branch information
Menecats and Menecats committed Aug 26, 2023
1 parent a4c6175 commit a6a91b0
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 20 deletions.
14 changes: 14 additions & 0 deletions src/app/components/keyfilter/keyfilter.interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/**
* A builtin pattern included within the KeyFilter component.
* @see {@link KeyFilter.pKeyFilter}
*/
export type KeyFilterPattern =
| "pint"
| "int"
| "pnum"
| "money"
| "num"
| "hex"
| "email"
| "alpha"
| "alphanum";
38 changes: 18 additions & 20 deletions src/app/components/keyfilter/keyfilter.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,15 @@
import { NgModule, Directive, ElementRef, HostListener, Input, forwardRef, Output, EventEmitter, Inject, PLATFORM_ID } from '@angular/core';
import { NgModule, Directive, ElementRef, HostListener, Input, forwardRef, Output, EventEmitter, Inject, PLATFORM_ID, Provider } from '@angular/core';
import { CommonModule, DOCUMENT, isPlatformBrowser } from '@angular/common';
import { DomHandler } from 'primeng/dom';
import { Validator, AbstractControl, NG_VALIDATORS } from '@angular/forms';
import { KeyFilterPattern } from './keyfilter.interface';

export const KEYFILTER_VALIDATOR: any = {
export const KEYFILTER_VALIDATOR: Provider = {
provide: NG_VALIDATORS,
useExisting: forwardRef(() => KeyFilter),
multi: true
};

type DefaultMasks = {
pint: RegExp;
int: RegExp;
pnum: RegExp;
money: RegExp;
num: RegExp;
hex: RegExp;
email: RegExp;
alpha: RegExp;
alphanum: RegExp;
};

type SafariKeys = {
63234: number;
63235: number;
Expand All @@ -41,7 +30,7 @@ type Keys = {
DELETE: number;
};

const DEFAULT_MASKS: DefaultMasks = {
const DEFAULT_MASKS: Record<KeyFilterPattern, RegExp> = {
pint: /[\d]/,
int: /[\d\-]/,
pnum: /[\d\.]/,
Expand Down Expand Up @@ -93,23 +82,32 @@ export class KeyFilter implements Validator {
* Sets the pattern for key filtering.
* @group Props
*/
@Input('pKeyFilter') set pattern(_pattern: string | RegExp) {

@Input('pKeyFilter') set pattern(_pattern: RegExp | KeyFilterPattern | null | undefined) {
this._pattern = _pattern;
this.regex = (DEFAULT_MASKS as any)[this._pattern as string] || this._pattern;

if (_pattern instanceof RegExp) {
this.regex = _pattern;
} else if (_pattern in DEFAULT_MASKS) {
this.regex = DEFAULT_MASKS[_pattern];
} else {
this.regex = /./;
}
}
get pattern(): string | RegExp {
get pattern(): RegExp | KeyFilterPattern | null | undefined {
return this._pattern;
}

/**
* Emits a value whenever the ngModel of the component changes.
* @param {(string | number)} modelValue - Custom model change event.
* @group Emits
*/
@Output() ngModelChange: EventEmitter<string | number> = new EventEmitter<string | number>();

regex!: RegExp;
regex: RegExp = /./;

_pattern: string | RegExp;
_pattern: RegExp | KeyFilterPattern | null | undefined;

isAndroid: boolean;

Expand Down
1 change: 1 addition & 0 deletions src/app/components/keyfilter/public_api.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './keyfilter';
export * from './keyfilter.interface';

0 comments on commit a6a91b0

Please sign in to comment.