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

Add placeholder underscores #1

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@myfarms/mf-mask",
"version": "1.0.5",
"version": "1.1.0",
"scripts": {
"build": "gulp build",
"build:watch": "gulp",
Expand Down
26 changes: 12 additions & 14 deletions src/mf-mask.directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { IConfig, MFMASK_CONFIG } from './config';
export class MfMaskDirective implements ControlValueAccessor, OnChanges {
@Input()
public mfMask: string = '(999) 999-9999';
public mfPlaceholder = '_';

private inputValue: string = '';

Expand Down Expand Up @@ -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;
}
Expand All @@ -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.mfPlaceholder;
maskCursor++;
}
} else if (inputChar === this.mfMask[maskCursor]) {
newValue += inputChar;
Expand All @@ -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;
}
}
2 changes: 1 addition & 1 deletion src/package.json
Original file line number Diff line number Diff line change
@@ -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"
Expand Down