Skip to content

Commit

Permalink
#34 support for a negative values - make sure that new feature will n…
Browse files Browse the repository at this point in the history
…ot break existing applications
  • Loading branch information
Krzysztof Zych committed Jun 21, 2021
1 parent 32fd80c commit c82d876
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 9 deletions.
12 changes: 6 additions & 6 deletions cypress/integration/clipboard-events.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -274,18 +274,18 @@ describe('Copy & Paste', () => {
cancelable: true,
});

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

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

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

cy.get('#digit-only').clear();
cy.get('#negative-digit-only').clear();
});
});
7 changes: 4 additions & 3 deletions projects/uiowa/digit-only/src/lib/digit-only.directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export class DigitOnlyDirective implements OnChanges {

@Input() decimal = false;
@Input() decimalSeparator = '.';
@Input() allowNegatives= false;
@Input() negativeSign = '-';
@Input() min = -Infinity;
@Input() max = Infinity;
Expand Down Expand Up @@ -61,7 +62,7 @@ export class DigitOnlyDirective implements OnChanges {
onBeforeInput(e: InputEvent): any {
if (isNaN(Number(e.data))) {
if (e.data === this.decimalSeparator
|| e.data === this.negativeSign) {
|| (e.data === this.negativeSign && this.allowNegatives)) {
return; // go on
}
e.preventDefault();
Expand Down Expand Up @@ -100,7 +101,7 @@ export class DigitOnlyDirective implements OnChanges {
}
}

if (e.key === this.negativeSign && this.min < 0) {
if (e.key === this.negativeSign && this.allowNegatives) {
newValue = this.forecastValue(e.key);
if (newValue.charAt(0) !== this.negativeSign || newValue.split(this.negativeSign).length > 2) {
e.preventDefault();
Expand Down Expand Up @@ -244,7 +245,7 @@ export class DigitOnlyDirective implements OnChanges {
}

private getNegativeSignRegExp() : string {
return !this.hasNegativeSign || this.getSelection().includes(this.negativeSign) ? `(?!^${this.negativeSign})` : '';
return this.allowNegatives && (!this.hasNegativeSign || this.getSelection().includes(this.negativeSign)) ? `(?!^${this.negativeSign})` : '';
}

private isValidDecimal(string: string): boolean {
Expand Down
3 changes: 3 additions & 0 deletions src/app/digit-only-demos/digit-only-demos.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ <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"/>
</section>

<section>
Expand Down

0 comments on commit c82d876

Please sign in to comment.