Skip to content

Commit 73c2a2b

Browse files
author
Your Name
committed
au-mask
1 parent 4383496 commit 73c2a2b

File tree

1 file changed

+38
-3
lines changed

1 file changed

+38
-3
lines changed

au-mask/src/app/au-mask/au-mask.directive.ts

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,19 @@ export class AuMaskDirective implements OnInit {
5959
this.handleRightArrow(cursorPos);
6060

6161
return;
62+
63+
64+
case BACKSPACE:
65+
66+
this.handleBackspace(cursorPos);
67+
68+
return;
69+
70+
case DELETE:
71+
72+
this.handleDelete(cursorPos);
73+
74+
return;
6275
}
6376

6477
const maskDigit = this.mask.charAt(cursorPos),
@@ -73,17 +86,39 @@ export class AuMaskDirective implements OnInit {
7386
}
7487
}
7588

89+
handleBackspace(cursorPos) {
90+
91+
const previousPos = this.calculatePreviousCursorPos(cursorPos);
92+
93+
if (previousPos >= 0) {
94+
overWriteCharAtPosition(this.input, previousPos, '_');
95+
this.input.setSelectionRange(previousPos, previousPos);
96+
}
97+
}
98+
99+
handleDelete(cursorPos) {
100+
overWriteCharAtPosition(this.input, cursorPos, '_');
101+
this.input.setSelectionRange(cursorPos, cursorPos);
102+
}
103+
104+
76105
handleLeftArrow(cursorPos) {
77-
const valueBeforeCursor = this.input.value.slice(0, cursorPos);
78106

79-
const previousPos = findLastIndex(valueBeforeCursor,
80-
char => ! includes(SPECIAL_CHARACTERS, char) );
107+
const previousPos = this.calculatePreviousCursorPos(cursorPos);
81108

82109
if (previousPos >= 0) {
83110
this.input.setSelectionRange(previousPos, previousPos);
84111
}
85112
}
86113

114+
calculatePreviousCursorPos(cursorPos) {
115+
const valueBeforeCursor = this.input.value.slice(0, cursorPos);
116+
117+
return findLastIndex(valueBeforeCursor,
118+
char => ! includes(SPECIAL_CHARACTERS, char) );
119+
120+
}
121+
87122
handleRightArrow(cursorPos) {
88123
const valueAfterCursor = this.input.value.slice(cursorPos + 1);
89124

0 commit comments

Comments
 (0)