Skip to content

Commit 4038090

Browse files
Fixed issue #42 #43
1 parent 69f9c1f commit 4038090

File tree

9 files changed

+78
-12
lines changed

9 files changed

+78
-12
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@asoftwareworld/form-builder",
3-
"version": "5.0.4",
3+
"version": "5.0.5",
44
"author": "Anish Sharma",
55
"license": "MIT",
66
"scripts": {

src/components/form-control/calculation/calculation-dialog.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ <h4 mat-dialog-title>Edit Property</h4>
100100
<mat-form-field appearance="outline" class="asw-mat-form-field">
101101
<mat-label>Operator {{index+1}}</mat-label>
102102
<mat-select formControlName="label"
103+
(selectionChange)="onOperatorChange($event, op)"
103104
matTooltip="Select operator {{index+1}}" required>
104105
<mat-option *ngFor="let control of data.numberControls" [value]="control.label">
105106
{{control.label}}

src/components/form-control/calculation/calculation-dialog.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,4 +104,13 @@ export class AswCalculationDialog implements OnInit {
104104
operation.controls.label.setValidators(Validators.required);
105105
}
106106
}
107+
108+
onOperatorChange(event: MatSelectChange, operator: any): void {
109+
this.data.numberControls.forEach((operation: any) => {
110+
if (event.value === operation.label) {
111+
operator.value.id = operation.guid;
112+
operator.value.control = operation;
113+
}
114+
});
115+
}
107116
}

src/components/form-control/number/number-dialog.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ <h4 mat-dialog-title>Edit Property</h4>
6464
<input matInput type="text" aswNumbersOnly
6565
name="value"
6666
placeholder="Enter value"
67-
(keypress)="objectUtils.keyPressNumbersWithDecimal($event)"
67+
[decimals]="decimals"
6868
matTooltip="Enter value"
6969
formControlName="value">
7070
</mat-form-field>

src/components/form-control/number/number-dialog.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import { NumberControl } from './number-control';
1717
templateUrl: './number-dialog.html'
1818
})
1919
export class AswNumberDialog implements OnInit {
20+
decimals = 100;
2021
constants: any = Constants;
2122
aswEditNumberForm!: FormGroup;
2223
status!: boolean;

src/components/form-control/number/number.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
[(ngModel)]="control.value"
88
#input="ngModel"
99
(ngModelChange)="onChange(control)"
10-
(keypress)="objectUtils.keyPressNumbersWithDecimal($event)"
10+
[decimals]="decimals"
1111
[matTooltip]="control.tooltip"
1212
[maxlength]="control.maxlength"
1313
[minlength]="control.minlength"

src/components/form-control/number/number.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ export class AswNumber implements OnInit, AfterViewInit {
2121

2222
constants: any = Constants;
2323
objectUtils = ObjectUtils;
24+
decimals = 100;
2425
/**
2526
* Number control
2627
*/

src/components/form-control/number/numbers-only.directive.ts

Lines changed: 62 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,74 @@
66
* found in the LICENSE file
77
*/
88

9-
import { Directive, ElementRef, HostListener } from '@angular/core';
9+
import { Directive, ElementRef, HostListener, Input } from '@angular/core';
1010

1111
@Directive({
12-
selector: 'input[aswNumbersOnly]'
12+
selector: '[aswNumbersOnly]'
1313
})
1414
export class AswNumberDirective {
15+
// tslint:disable-next-line:no-input-rename
16+
@Input('decimals') decimals = 0;
17+
// tslint:disable-next-line:no-input-rename
18+
@Input('negative') negative = 0;
1519

16-
constructor(public element: ElementRef) { }
20+
private checkAllowNegative(value: string): any {
21+
if (this.decimals <= 0) {
22+
return String(value).match(new RegExp(/^-?\d+$/));
23+
} else {
24+
const regExpString =
25+
'^-?\\s*((\\d+(\\.\\d{0,' +
26+
this.decimals +
27+
'})?)|((\\d*(\\.\\d{1,' +
28+
this.decimals +
29+
'}))))\\s*$';
30+
return String(value).match(new RegExp(regExpString));
31+
}
32+
}
1733

18-
@HostListener('input', ['$event']) onInputChange(event: any): void {
19-
const initalValue = this.element.nativeElement.value;
20-
this.element.nativeElement.value = initalValue.replace(/[^0-9]*/g, '');
21-
if (initalValue !== this.element.nativeElement.value) {
22-
event.stopPropagation();
34+
private check(value: string): any {
35+
if (this.decimals <= 0) {
36+
return String(value).match(new RegExp(/^\d+$/));
37+
} else {
38+
const regExpString =
39+
'^\\s*((\\d+(\\.\\d{0,' +
40+
this.decimals +
41+
'})?)|((\\d*(\\.\\d{1,' +
42+
this.decimals +
43+
'}))))\\s*$';
44+
return String(value).match(new RegExp(regExpString));
2345
}
2446
}
47+
48+
private run(oldValue: any): void {
49+
setTimeout(() => {
50+
const currentValue: string = this.el.nativeElement.value;
51+
const allowNegative = this.negative > 0 ? true : false;
52+
53+
if (allowNegative) {
54+
if (
55+
!['', '-'].includes(currentValue) &&
56+
!this.checkAllowNegative(currentValue)
57+
) {
58+
this.el.nativeElement.value = oldValue;
59+
}
60+
} else {
61+
if (currentValue !== '' && !this.check(currentValue)) {
62+
this.el.nativeElement.value = oldValue;
63+
}
64+
}
65+
});
66+
}
67+
68+
constructor(private el: ElementRef) { }
69+
70+
@HostListener('keydown', ['$event'])
71+
onKeyDown(event: KeyboardEvent): void {
72+
this.run(this.el.nativeElement.value);
73+
}
74+
75+
@HostListener('paste', ['$event'])
76+
onPaste(event: ClipboardEvent): void {
77+
this.run(this.el.nativeElement.value);
78+
}
2579
}

src/components/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@asoftwareworld/form-builder",
3-
"version": "5.0.4",
3+
"version": "5.0.5",
44
"author": "Anish Sharma",
55
"license": "MIT",
66
"peerDependencies": {

0 commit comments

Comments
 (0)