Skip to content

Commit

Permalink
use the code attribute in KeyboardEvent
Browse files Browse the repository at this point in the history
related to issue #35
  • Loading branch information
changhuixu committed Jul 7, 2020
1 parent 07de8bf commit ff2086f
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 21 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@

- **v2.0.0**: add `tslib` v2.0 in the dependency, which is required by TypeScript 3.9 (as of Angular 10).

- **v2.0.1**: For better international support, both `mask` and `digitOnly` directives are updated to use the `code` attribute instead of the `key` attribute in `KeyboardEvent`.

---

## Installation
Expand Down
2 changes: 2 additions & 0 deletions projects/uiowa/digit-only/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@

- **v2.0.0**: add `tslib` v2.0 in the dependency, which is required by TypeScript 3.9 (as of Angular 10).

- **v2.0.1**: For better international support, both `mask` and `digitOnly` directives are updated to use the `code` attribute instead of the `key` attribute in `KeyboardEvent`.

---

## Installation
Expand Down
2 changes: 1 addition & 1 deletion projects/uiowa/digit-only/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@uiowa/digit-only",
"version": "2.0.0",
"version": "2.0.1",
"author": "Changhui Xu <changhui-xu@uiowa.edu>",
"description": "This package includes two Angular directives. The digitOnly directive only allows numbers in the input box when typing, pasting or drag/dropping. The mask directive checks the input pattern attribute.",
"keywords": [
Expand Down
26 changes: 14 additions & 12 deletions projects/uiowa/digit-only/src/lib/digit-only.directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,14 @@ export class DigitOnlyDirective implements OnChanges {
onKeyDown(e: KeyboardEvent): any {
if (
this.navigationKeys.indexOf(e.key) > -1 || // Allow: navigation keys: backspace, delete, arrows etc.
(e.key === 'a' && e.ctrlKey === true) || // Allow: Ctrl+A
(e.key === 'c' && e.ctrlKey === true) || // Allow: Ctrl+C
(e.key === 'v' && e.ctrlKey === true) || // Allow: Ctrl+V
(e.key === 'x' && e.ctrlKey === true) || // Allow: Ctrl+X
(e.key === 'a' && e.metaKey === true) || // Allow: Cmd+A (Mac)
(e.key === 'c' && e.metaKey === true) || // Allow: Cmd+C (Mac)
(e.key === 'v' && e.metaKey === true) || // Allow: Cmd+V (Mac)
(e.key === 'x' && e.metaKey === true) || // Allow: Cmd+X (Mac)
(e.code === 'KeyA' && e.ctrlKey === true) || // Allow: Ctrl+A
(e.code === 'KeyC' && e.ctrlKey === true) || // Allow: Ctrl+C
(e.code === 'KeyV' && e.ctrlKey === true) || // Allow: Ctrl+V
(e.code === 'KeyX' && e.ctrlKey === true) || // Allow: Ctrl+X
(e.code === 'KeyA' && e.metaKey === true) || // Allow: Cmd+A (Mac)
(e.code === 'KeyC' && e.metaKey === true) || // Allow: Cmd+C (Mac)
(e.code === 'KeyV' && e.metaKey === true) || // Allow: Cmd+V (Mac)
(e.code === 'KeyX' && e.metaKey === true) || // Allow: Cmd+X (Mac)
(this.decimal && e.key === this.decimalSeparator && !this.hasDecimalPoint) // Allow: only one decimal point
) {
// let it happen, don't do anything
Expand Down Expand Up @@ -142,8 +142,10 @@ export class DigitOnlyDirective implements OnChanges {
const startPos = myField.selectionStart;
const endPos = myField.selectionEnd;

myField.value = myField.value.substring(0, startPos) + myValue
+ myField.value.substring(endPos, myField.value.length);
myField.value =
myField.value.substring(0, startPos) +
myValue +
myField.value.substring(endPos, myField.value.length);

const pos = startPos + myValue.length;
myField.focus();
Expand Down Expand Up @@ -216,7 +218,7 @@ export class DigitOnlyDirective implements OnChanges {
return selection
? oldValue.replace(selection, key)
: oldValue.substring(0, selectionStart) +
key +
oldValue.substring(selectionStart);
key +
oldValue.substring(selectionStart);
}
}
16 changes: 8 additions & 8 deletions projects/uiowa/digit-only/src/lib/mask.directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@ export class MaskDirective {
onKeyDown(e: KeyboardEvent) {
if (
this.navigationKeys.indexOf(e.key) > -1 || // Allow: navigation keys: backspace, delete, arrows etc.
(e.key === 'a' && e.ctrlKey === true) || // Allow: Ctrl+A
(e.key === 'c' && e.ctrlKey === true) || // Allow: Ctrl+C
(e.key === 'v' && e.ctrlKey === true) || // Allow: Ctrl+V
(e.key === 'x' && e.ctrlKey === true) || // Allow: Ctrl+X
(e.key === 'a' && e.metaKey === true) || // Allow: Cmd+A (Mac)
(e.key === 'c' && e.metaKey === true) || // Allow: Cmd+C (Mac)
(e.key === 'v' && e.metaKey === true) || // Allow: Cmd+V (Mac)
(e.key === 'x' && e.metaKey === true) // Allow: Cmd+X (Mac)
(e.code === 'KeyA' && e.ctrlKey === true) || // Allow: Ctrl+A
(e.code === 'KeyC' && e.ctrlKey === true) || // Allow: Ctrl+C
(e.code === 'KeyV' && e.ctrlKey === true) || // Allow: Ctrl+V
(e.code === 'KeyX' && e.ctrlKey === true) || // Allow: Ctrl+X
(e.code === 'KeyA' && e.metaKey === true) || // Allow: Cmd+A (Mac)
(e.code === 'KeyC' && e.metaKey === true) || // Allow: Cmd+C (Mac)
(e.code === 'KeyV' && e.metaKey === true) || // Allow: Cmd+V (Mac)
(e.code === 'KeyX' && e.metaKey === true) // Allow: Cmd+X (Mac)
) {
// let it happen, don't do anything
return;
Expand Down

0 comments on commit ff2086f

Please sign in to comment.