diff --git a/.gitignore b/.gitignore
index 7e96d4b..fd2b216 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,4 +1,4 @@
node_modules/
src/**/*.js
src/**/*.js.map
-!src/**/systemjs*.js
\ No newline at end of file
+!src/**/systemjs*.js
diff --git a/.idea/codeStyleSettings.xml b/.idea/codeStyleSettings.xml
new file mode 100644
index 0000000..3680791
--- /dev/null
+++ b/.idea/codeStyleSettings.xml
@@ -0,0 +1,30 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
Value:
+{{ result }}
\ No newline at end of file
diff --git a/src/app/app.component.ts b/src/app/app.component.ts
index 7b11a65..7da5e04 100644
--- a/src/app/app.component.ts
+++ b/src/app/app.component.ts
@@ -2,15 +2,9 @@ import { Component } from '@angular/core';
@Component({
selector: 'my-app',
- template: `
-
- form is valid: {{ form.valid ? 'true' : 'false' }}
- -Value:
-{{ result | json }}`,
+ templateUrl: './app.component.html'
})
+
export class AppComponent {
public result = 10;
}
diff --git a/src/app/app.module.ts b/src/app/app.module.ts
index 96f0cff..0a88eb5 100644
--- a/src/app/app.module.ts
+++ b/src/app/app.module.ts
@@ -2,7 +2,7 @@ import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppComponent } from './app.component';
-import {JsonInputModule} from "./json-input.module";
+import {JsonInputModule} from "./counter-input.module";
import {FormsModule, ReactiveFormsModule} from "@angular/forms";
@NgModule({
diff --git a/src/app/counter-input.component.ts b/src/app/counter-input.component.ts
new file mode 100644
index 0000000..5deb73e
--- /dev/null
+++ b/src/app/counter-input.component.ts
@@ -0,0 +1,50 @@
+import { Component, forwardRef } from '@angular/core';
+import { NG_VALUE_ACCESSOR, NG_VALIDATORS, FormControl, ValidationErrors } from '@angular/forms';
+import {CustomInputComponent} from "./custom-input.component";
+
+@Component({
+ selector: 'counter-input',
+ template:
+ `
+
+ {{value}}
+
+ `,
+ providers: [
+ {
+ provide: NG_VALUE_ACCESSOR,
+ useExisting: forwardRef(() => CounterInputComponent),
+ multi: true,
+ },
+ {
+ provide: NG_VALIDATORS,
+ useExisting: forwardRef(() => CounterInputComponent),
+ multi: true,
+ }]
+})
+export class CounterInputComponent extends CustomInputComponent {
+ // set initial value
+ public writeValue(obj: any): void {
+ if (obj) {
+ this.value = parseInt(obj);
+ this.parseError = isNaN(this.value);
+ }
+ }
+
+ // validates the form, returns null when valid else the validation object
+ public validate(c: FormControl): ValidationErrors {
+ return (!this.parseError) ? null : {
+ numberParseError: {
+ valid: false,
+ },
+ };
+ }
+
+ // on button click
+ protected onChange(number: number): void {
+ this.value = this.value + number;
+ this.parseError = isNaN(this.value);
+
+ super.onChange(number);
+ }
+}
\ No newline at end of file
diff --git a/src/app/json-input.module.ts b/src/app/counter-input.module.ts
similarity index 70%
rename from src/app/json-input.module.ts
rename to src/app/counter-input.module.ts
index b0ed74d..e157eea 100644
--- a/src/app/json-input.module.ts
+++ b/src/app/counter-input.module.ts
@@ -1,7 +1,7 @@
import {FormsModule} from '@angular/forms';
import { CommonModule } from '@angular/common';
import { NgModule } from '@angular/core';
-import { JsonInputComponent } from './json-input.component';
+import { CounterInputComponent } from './counter-input.component';
@NgModule({
@@ -10,10 +10,10 @@ import { JsonInputComponent } from './json-input.component';
FormsModule,
],
exports: [
- JsonInputComponent,
+ CounterInputComponent,
],
declarations: [
- JsonInputComponent,
+ CounterInputComponent,
],
providers: [],
})
diff --git a/src/app/custom-input.component.ts b/src/app/custom-input.component.ts
new file mode 100644
index 0000000..cea0a40
--- /dev/null
+++ b/src/app/custom-input.component.ts
@@ -0,0 +1,34 @@
+import {ControlValueAccessor, FormControl, ValidationErrors, Validator} from '@angular/forms';
+
+export abstract class CustomInputComponent implements ControlValueAccessor, Validator {
+ protected value: number;
+ protected parseError: boolean;
+ private propagateChange: (_: any) => void = () => {};
+ private propagateTouch: () => void = () => {};
+
+ // set initial value
+ public abstract writeValue(obj: any): void;
+
+ // register function to notify on change
+ public registerOnChange(fn: (_: any) => void): void {
+ this.propagateChange = fn;
+ }
+
+ // register function to notify on touch
+ public registerOnTouched(fn: () => void): void {
+ this.propagateTouch = fn;
+ }
+
+ // validates the form, returns null when valid else the validation object
+ public abstract validate(c: FormControl): ValidationErrors;
+
+ // on change
+ protected onChange(value: any): void {
+ this.propagateChange(this.value);
+ }
+
+ // on touch
+ protected onBlur(): void {
+ this.propagateTouch();
+ }
+}
\ No newline at end of file
diff --git a/src/app/json-input.component.ts b/src/app/json-input.component.ts
deleted file mode 100644
index eadb75d..0000000
--- a/src/app/json-input.component.ts
+++ /dev/null
@@ -1,73 +0,0 @@
-import { Component, Input, forwardRef } from '@angular/core';
-import { ControlValueAccessor, NG_VALUE_ACCESSOR, NG_VALIDATORS, FormControl, Validator } from '@angular/forms';
-
-@Component({
- selector: 'json-input',
- template:
- `
-
-