From b8d808ebe86bf917911ab6f2891131d2e3783f4b Mon Sep 17 00:00:00 2001 From: Simon Smith Date: Thu, 27 May 2021 11:47:25 -0400 Subject: [PATCH 1/3] Add placeholder support --- src/mf-mask.directive.ts | 26 ++++++++++++-------------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/src/mf-mask.directive.ts b/src/mf-mask.directive.ts index ed5f6a3..1e495db 100644 --- a/src/mf-mask.directive.ts +++ b/src/mf-mask.directive.ts @@ -24,6 +24,7 @@ import { IConfig, MFMASK_CONFIG } from './config'; export class MfMaskDirective implements ControlValueAccessor, OnChanges { @Input() public mfMask: string = '(999) 999-9999'; + public placeholder = '_'; private inputValue: string = ''; @@ -99,7 +100,8 @@ export class MfMaskDirective implements ControlValueAccessor, OnChanges { inputArray = inputValue.split(''); let maskCursor = 0; let newValue = ''; - for (let i = 0; i < inputArray.length; i++) { + let newCursorPos = 0; + for (let i = 0; i < this.mfMask.length; i++) { if (maskCursor === this.mfMask.length) { break; } @@ -110,8 +112,10 @@ export class MfMaskDirective implements ControlValueAccessor, OnChanges { if (patternRegex.test(inputChar)) { newValue += inputChar; maskCursor++; - } else { - break; + newCursorPos = maskCursor; + } else if (i >= inputArray.length) { + newValue += this.placeholder; + maskCursor++; } } else if (inputChar === this.mfMask[maskCursor]) { newValue += inputChar; @@ -123,18 +127,12 @@ export class MfMaskDirective implements ControlValueAccessor, OnChanges { } } - for (let i = newValue.length - 1; i >= 0; i--) { - const val = newValue.charAt(i); - const maskPattern = this.mfMask[i]; - const patternRegex = this.config.patterns[maskPattern]; - if (patternRegex && patternRegex.test(val)) { - break; - } else { - newValue = newValue.slice(0, newValue.length - 1); - } - } + setTimeout(() => { + this.onChange(newValue) - setTimeout(() => this.onChange(newValue)); + // Needs to be in setTimeout to work + this.elementRef.nativeElement.setSelectionRange(newCursorPos, newCursorPos); + }); this.elementRef.nativeElement.value = newValue; } } From a5022eb36ebb7546d27de88d8a4e508ffa3c3222 Mon Sep 17 00:00:00 2001 From: Simon Smith Date: Thu, 27 May 2021 12:00:43 -0400 Subject: [PATCH 2/3] prefix placeholder input with mf --- src/mf-mask.directive.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/mf-mask.directive.ts b/src/mf-mask.directive.ts index 1e495db..1236c22 100644 --- a/src/mf-mask.directive.ts +++ b/src/mf-mask.directive.ts @@ -24,7 +24,7 @@ import { IConfig, MFMASK_CONFIG } from './config'; export class MfMaskDirective implements ControlValueAccessor, OnChanges { @Input() public mfMask: string = '(999) 999-9999'; - public placeholder = '_'; + public mfPlaceholder = '_'; private inputValue: string = ''; @@ -114,7 +114,7 @@ export class MfMaskDirective implements ControlValueAccessor, OnChanges { maskCursor++; newCursorPos = maskCursor; } else if (i >= inputArray.length) { - newValue += this.placeholder; + newValue += this.mfPlaceholder; maskCursor++; } } else if (inputChar === this.mfMask[maskCursor]) { From e2f3664fbc42c560b43e76cfdf9e62d69ed27671 Mon Sep 17 00:00:00 2001 From: Simon Smith Date: Thu, 27 May 2021 11:50:05 -0400 Subject: [PATCH 3/3] 1.1.0 --- package-lock.json | 2 +- package.json | 2 +- src/package.json | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/package-lock.json b/package-lock.json index 46b6210..2852e70 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "@myfarms/mf-mask", - "version": "1.0.5", + "version": "1.1.0", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index b37e00c..187089a 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@myfarms/mf-mask", - "version": "1.0.5", + "version": "1.1.0", "scripts": { "build": "gulp build", "build:watch": "gulp", diff --git a/src/package.json b/src/package.json index 5c75af3..0511042 100644 --- a/src/package.json +++ b/src/package.json @@ -1,6 +1,6 @@ { "name": "@myfarms/mf-mask", - "version": "1.0.5", + "version": "1.1.0", "repository": { "type": "git", "url": "https://github.com/myfarms/mf-mask"