Skip to content

Commit

Permalink
Merge pull request #48 from krzysztof-zych/bugs/prevent-dead-keys
Browse files Browse the repository at this point in the history
Use beforeinput event to prevent input the Dead keys
  • Loading branch information
changhuixu authored Mar 9, 2021
2 parents d6eb388 + 3b7a6e2 commit 65e6dc0
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
7 changes: 7 additions & 0 deletions cypress/integration/keyboard-events.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ describe('Keyboard Typing', () => {
.clear();
});

it('should prevent dead keys', () => {
cy.get('#digit-only')
.type('~~ ~! ~@ ~^')
.should('have.value', '')
.clear();
});

it('should accept a decimal point', () => {
cy.get('#digit-only-decimal')
.type('1s2d4d*(,.3')
Expand Down
11 changes: 11 additions & 0 deletions projects/uiowa/digit-only/src/lib/digit-only.directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,17 @@ export class DigitOnlyDirective implements OnChanges {
}
}

@HostListener('beforeinput', ['$event'])
onBeforeInput(e: InputEvent): any {
if (isNaN(Number(e.data))) {
if(e.data === this.decimalSeparator) {
return; // go on
}
e.preventDefault();
e.stopPropagation();
}
}

@HostListener('keydown', ['$event'])
onKeyDown(e: KeyboardEvent): any {
if (
Expand Down

0 comments on commit 65e6dc0

Please sign in to comment.