From 40380909e98adf45143c5720faab85194846c7b5 Mon Sep 17 00:00:00 2001 From: asoftwareworld Date: Wed, 2 Nov 2022 17:42:49 +0530 Subject: [PATCH] Fixed issue #42 #43 --- package.json | 2 +- .../calculation/calculation-dialog.html | 1 + .../calculation/calculation-dialog.ts | 9 +++ .../form-control/number/number-dialog.html | 2 +- .../form-control/number/number-dialog.ts | 1 + .../form-control/number/number.html | 2 +- src/components/form-control/number/number.ts | 1 + .../number/numbers-only.directive.ts | 70 ++++++++++++++++--- src/components/package.json | 2 +- 9 files changed, 78 insertions(+), 12 deletions(-) diff --git a/package.json b/package.json index a059ee1..f66370e 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@asoftwareworld/form-builder", - "version": "5.0.4", + "version": "5.0.5", "author": "Anish Sharma", "license": "MIT", "scripts": { diff --git a/src/components/form-control/calculation/calculation-dialog.html b/src/components/form-control/calculation/calculation-dialog.html index 975c286..b9cb9cd 100644 --- a/src/components/form-control/calculation/calculation-dialog.html +++ b/src/components/form-control/calculation/calculation-dialog.html @@ -100,6 +100,7 @@

Edit Property

Operator {{index+1}} {{control.label}} diff --git a/src/components/form-control/calculation/calculation-dialog.ts b/src/components/form-control/calculation/calculation-dialog.ts index ee13d1f..c7c993c 100644 --- a/src/components/form-control/calculation/calculation-dialog.ts +++ b/src/components/form-control/calculation/calculation-dialog.ts @@ -104,4 +104,13 @@ export class AswCalculationDialog implements OnInit { operation.controls.label.setValidators(Validators.required); } } + + onOperatorChange(event: MatSelectChange, operator: any): void { + this.data.numberControls.forEach((operation: any) => { + if (event.value === operation.label) { + operator.value.id = operation.guid; + operator.value.control = operation; + } + }); + } } diff --git a/src/components/form-control/number/number-dialog.html b/src/components/form-control/number/number-dialog.html index f8bd547..cf82244 100644 --- a/src/components/form-control/number/number-dialog.html +++ b/src/components/form-control/number/number-dialog.html @@ -64,7 +64,7 @@

Edit Property

diff --git a/src/components/form-control/number/number-dialog.ts b/src/components/form-control/number/number-dialog.ts index 51a9a2d..64aebed 100644 --- a/src/components/form-control/number/number-dialog.ts +++ b/src/components/form-control/number/number-dialog.ts @@ -17,6 +17,7 @@ import { NumberControl } from './number-control'; templateUrl: './number-dialog.html' }) export class AswNumberDialog implements OnInit { + decimals = 100; constants: any = Constants; aswEditNumberForm!: FormGroup; status!: boolean; diff --git a/src/components/form-control/number/number.html b/src/components/form-control/number/number.html index 5732f06..0b98606 100644 --- a/src/components/form-control/number/number.html +++ b/src/components/form-control/number/number.html @@ -7,7 +7,7 @@ [(ngModel)]="control.value" #input="ngModel" (ngModelChange)="onChange(control)" - (keypress)="objectUtils.keyPressNumbersWithDecimal($event)" + [decimals]="decimals" [matTooltip]="control.tooltip" [maxlength]="control.maxlength" [minlength]="control.minlength" diff --git a/src/components/form-control/number/number.ts b/src/components/form-control/number/number.ts index df9b2b2..f47fd24 100644 --- a/src/components/form-control/number/number.ts +++ b/src/components/form-control/number/number.ts @@ -21,6 +21,7 @@ export class AswNumber implements OnInit, AfterViewInit { constants: any = Constants; objectUtils = ObjectUtils; + decimals = 100; /** * Number control */ diff --git a/src/components/form-control/number/numbers-only.directive.ts b/src/components/form-control/number/numbers-only.directive.ts index f267ec8..4e7b6c4 100644 --- a/src/components/form-control/number/numbers-only.directive.ts +++ b/src/components/form-control/number/numbers-only.directive.ts @@ -6,20 +6,74 @@ * found in the LICENSE file */ -import { Directive, ElementRef, HostListener } from '@angular/core'; +import { Directive, ElementRef, HostListener, Input } from '@angular/core'; @Directive({ - selector: 'input[aswNumbersOnly]' + selector: '[aswNumbersOnly]' }) export class AswNumberDirective { + // tslint:disable-next-line:no-input-rename + @Input('decimals') decimals = 0; + // tslint:disable-next-line:no-input-rename + @Input('negative') negative = 0; - constructor(public element: ElementRef) { } + private checkAllowNegative(value: string): any { + if (this.decimals <= 0) { + return String(value).match(new RegExp(/^-?\d+$/)); + } else { + const regExpString = + '^-?\\s*((\\d+(\\.\\d{0,' + + this.decimals + + '})?)|((\\d*(\\.\\d{1,' + + this.decimals + + '}))))\\s*$'; + return String(value).match(new RegExp(regExpString)); + } + } - @HostListener('input', ['$event']) onInputChange(event: any): void { - const initalValue = this.element.nativeElement.value; - this.element.nativeElement.value = initalValue.replace(/[^0-9]*/g, ''); - if (initalValue !== this.element.nativeElement.value) { - event.stopPropagation(); + private check(value: string): any { + if (this.decimals <= 0) { + return String(value).match(new RegExp(/^\d+$/)); + } else { + const regExpString = + '^\\s*((\\d+(\\.\\d{0,' + + this.decimals + + '})?)|((\\d*(\\.\\d{1,' + + this.decimals + + '}))))\\s*$'; + return String(value).match(new RegExp(regExpString)); } } + + private run(oldValue: any): void { + setTimeout(() => { + const currentValue: string = this.el.nativeElement.value; + const allowNegative = this.negative > 0 ? true : false; + + if (allowNegative) { + if ( + !['', '-'].includes(currentValue) && + !this.checkAllowNegative(currentValue) + ) { + this.el.nativeElement.value = oldValue; + } + } else { + if (currentValue !== '' && !this.check(currentValue)) { + this.el.nativeElement.value = oldValue; + } + } + }); + } + + constructor(private el: ElementRef) { } + + @HostListener('keydown', ['$event']) + onKeyDown(event: KeyboardEvent): void { + this.run(this.el.nativeElement.value); + } + + @HostListener('paste', ['$event']) + onPaste(event: ClipboardEvent): void { + this.run(this.el.nativeElement.value); + } } diff --git a/src/components/package.json b/src/components/package.json index 161fb44..afe5f77 100644 --- a/src/components/package.json +++ b/src/components/package.json @@ -1,6 +1,6 @@ { "name": "@asoftwareworld/form-builder", - "version": "5.0.4", + "version": "5.0.5", "author": "Anish Sharma", "license": "MIT", "peerDependencies": {