Skip to content

Commit

Permalink
pick pull request #57, fixes #56; Release v2.4.0
Browse files Browse the repository at this point in the history
  • Loading branch information
changhuixu committed Sep 7, 2021
1 parent 959a4f5 commit b3d8966
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 17 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

## CHANGELOG

- **v2.4.0**: `digitOnly` directive now supports disabling paste events (merges a pull request [#57](/../../pull/49), fixes [#56](/../../issues/56)).

- **v2.3.0**: `digitOnly` directive now supports negative values (merges a pull request [#49](/../../pull/49)).

- **v2.2.3**: fix an issue ([#50](/../../issues/50)) in the `mask` directive: support dynamic pattern attribute binding.
Expand Down
21 changes: 20 additions & 1 deletion cypress/integration/clipboard-events.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ describe('Copy & Paste', () => {
cy.get('#digit-only-decimal').clear();
});

it.only('s', ()=> {
it('should allow only one decimal place in a decimal input element', () => {
const dt = new DataTransfer();
dt.setData('text/plain', 'abc1.0s.1');
const pasteEvent = new ClipboardEvent('paste', {
Expand Down Expand Up @@ -264,4 +264,23 @@ describe('Copy & Paste', () => {

cy.get('#decimal-number').clear();
});

it('should disable paste when [allowPaste] is false', () => {
const dt = new DataTransfer();
dt.setData('text/plain', 'a-123');
const pasteEvent = new ClipboardEvent('paste', {
clipboardData: dt,
bubbles: true,
cancelable: true,
});

cy.get('#digit-only-disable-paste').clear();

cy.get('#digit-only-disable-paste').then(($el) => {
$el[0].dispatchEvent(pasteEvent);
cy.get('#digit-only-disable-paste').should('have.value', '');
});

cy.get('#digit-only-disable-paste').clear();
});
});
2 changes: 2 additions & 0 deletions projects/uiowa/digit-only/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

## CHANGELOG

- **v2.4.0**: `digitOnly` directive now supports disabling paste events (merges a pull request [#57](/../../pull/49), fixes [#56](/../../issues/56)).

- **v2.3.0**: `digitOnly` directive now supports negative values (merges a pull request [#49](/../../pull/49)).

- **v2.2.3**: fix an issue ([#50](/../../issues/50)) in the `mask` directive: support dynamic pattern attribute binding.
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.3.0",
"version": "2.4.0",
"author": "Changhui Xu <[email protected]>",
"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
31 changes: 20 additions & 11 deletions projects/uiowa/digit-only/src/lib/digit-only.directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export class DigitOnlyDirective implements OnChanges {
@Input() decimal = false;
@Input() decimalSeparator = '.';
@Input() allowNegatives= false;
@Input() allowPaste = true;
@Input() negativeSign = '-';
@Input() min = -Infinity;
@Input() max = Infinity;
Expand Down Expand Up @@ -132,21 +133,29 @@ export class DigitOnlyDirective implements OnChanges {
e.preventDefault();
}
}

@HostListener('paste', ['$event'])
onPaste(event: any): void {
let pastedInput: string;
if (window['clipboardData']) {
// Browser is IE
pastedInput = window['clipboardData'].getData('text');
} else if (event.clipboardData && event.clipboardData.getData) {
// Other browsers
pastedInput = event.clipboardData.getData('text/plain');
}
if (this.allowPaste === true) {
let pastedInput: string = '';
if ((window as { [key: string]: any })['clipboardData']) {
// Browser is IE
pastedInput = (window as { [key: string]: any })['clipboardData'].getData(
'text'
);
} else if (event.clipboardData && event.clipboardData.getData) {
// Other browsers
pastedInput = event.clipboardData.getData('text/plain');
}

this.pasteData(pastedInput);
event.preventDefault();
this.pasteData(pastedInput);
event.preventDefault();
} else { // this prevents the paste
event.preventDefault();
event.stopPropagation();
}
}


@HostListener('drop', ['$event'])
onDrop(event: DragEvent): void {
Expand Down
30 changes: 26 additions & 4 deletions src/app/digit-only-demos/digit-only-demos.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,17 @@ <h3><code>digitOnly</code> directive</h3>
placeholder="000"
maxlength="3"
/>

<label for="negative-digit-only">Digit Only input box that can be negative</label>
<input id="negative-digit-only" type="text" digitOnly [allowNegatives]="true" placeholder="-123"/>

<label for="negative-digit-only">
Digit Only input box that can be negative
</label>
<input
id="negative-digit-only"
type="text"
digitOnly
[allowNegatives]="true"
placeholder="-123"
/>
</section>

<section>
Expand Down Expand Up @@ -100,7 +108,7 @@ <h3><code>pattern</code></h3>
[(ngModel)]="pattern"
name="change-pattern"
placeholder="Type your pattern"
style="margin-left: 0.5rem;"
style="margin-left: 0.5rem"
/>

<label for="currency">
Expand Down Expand Up @@ -185,4 +193,18 @@ <h3>regular text input</h3>
placeholder="000"
/>
</section>

<section>
<h3><code>digitOnly</code> directive</h3>
<label for="digit-only-disable-paste">
Digit Only input box (disable paste)
</label>
<input
id="digit-only-disable-paste"
type="text"
digitOnly
[allowPaste]="false"
placeholder="00000"
/>
</section>
</div>

0 comments on commit b3d8966

Please sign in to comment.