From d9fa5093a1bd73cd5afa0e4a74fc3fa6ca9366f3 Mon Sep 17 00:00:00 2001 From: TheDiaval Date: Fri, 13 Sep 2019 09:16:00 +0300 Subject: [PATCH] feature(core): add ng-model component --- npm/ng-packs/package.json | 4 -- .../packages/core/src/lib/abstracts/index.ts | 1 + .../src/lib/abstracts/ng-model.component.ts | 51 +++++++++++++++++++ .../packages/core/src/lib/core.module.ts | 3 ++ npm/ng-packs/packages/core/src/public-api.ts | 1 + 5 files changed, 56 insertions(+), 4 deletions(-) create mode 100644 npm/ng-packs/packages/core/src/lib/abstracts/index.ts create mode 100644 npm/ng-packs/packages/core/src/lib/abstracts/ng-model.component.ts diff --git a/npm/ng-packs/package.json b/npm/ng-packs/package.json index 62a9bb71c7..27e3664f8d 100644 --- a/npm/ng-packs/package.json +++ b/npm/ng-packs/package.json @@ -31,7 +31,6 @@ "@types/jasmine": "~3.3.8", "@types/jasminewd2": "~2.0.3", "@types/node": "~8.9.4", - "@volo/abp.ng.cli": "^1.0.1", "angular-oauth2-oidc": "^8.0.1", "bootstrap": "^4.3.1", "codelyzer": "^5.0.0", @@ -60,8 +59,5 @@ "zone.js": "~0.9.1", "just-clone": "3.1.0", "chart.js": "^2.8.0" - }, - "dependencies": { - "tslib": "^1.10.0" } } diff --git a/npm/ng-packs/packages/core/src/lib/abstracts/index.ts b/npm/ng-packs/packages/core/src/lib/abstracts/index.ts new file mode 100644 index 0000000000..a4e501a850 --- /dev/null +++ b/npm/ng-packs/packages/core/src/lib/abstracts/index.ts @@ -0,0 +1 @@ +export * from './ng-model.component'; diff --git a/npm/ng-packs/packages/core/src/lib/abstracts/ng-model.component.ts b/npm/ng-packs/packages/core/src/lib/abstracts/ng-model.component.ts new file mode 100644 index 0000000000..6cc720b011 --- /dev/null +++ b/npm/ng-packs/packages/core/src/lib/abstracts/ng-model.component.ts @@ -0,0 +1,51 @@ +import { ControlValueAccessor } from '@angular/forms'; +import { ChangeDetectorRef, Component, Injector, Input, Type } from '@angular/core'; + +@Component({ template: '' }) +export class AbstractNgModelComponent implements ControlValueAccessor { + @Input() + disabled: boolean; + + @Input() + set value(value: T) { + this._value = value; + this.notifyValueChange(); + } + + get value(): T { + return this._value; + } + + onChange: (value: T) => {}; + onTouched: () => {}; + + protected _value: T; + protected cdRef: ChangeDetectorRef; + + constructor(public injector: Injector) { + this.cdRef = injector.get(ChangeDetectorRef as Type); + } + + notifyValueChange(): void { + if (this.onChange) { + this.onChange(this.value); + } + } + + writeValue(value: T): void { + this._value = value; + setTimeout(() => this.cdRef.detectChanges(), 0); + } + + registerOnChange(fn: any): void { + this.onChange = fn; + } + + registerOnTouched(fn: any): void { + this.onTouched = fn; + } + + setDisabledState(isDisabled: boolean): void { + this.disabled = isDisabled; + } +} diff --git a/npm/ng-packs/packages/core/src/lib/core.module.ts b/npm/ng-packs/packages/core/src/lib/core.module.ts index c9c6ce9761..ed1a93b8e0 100644 --- a/npm/ng-packs/packages/core/src/lib/core.module.ts +++ b/npm/ng-packs/packages/core/src/lib/core.module.ts @@ -25,6 +25,7 @@ import { SessionState } from './states/session.state'; import { getInitialData, localeInitializer } from './utils/initial-utils'; import { ConfigPlugin, NGXS_CONFIG_PLUGIN_OPTIONS } from './plugins/config/config.plugin'; import { ForDirective } from './directives/for.directive'; +import { AbstractNgModelComponent } from './abstracts/ng-model.component'; @NgModule({ imports: [ @@ -49,6 +50,7 @@ import { ForDirective } from './directives/for.directive'; VisibilityDirective, InputEventDebounceDirective, ClickEventStopPropagationDirective, + AbstractNgModelComponent, ], exports: [ CommonModule, @@ -68,6 +70,7 @@ import { ForDirective } from './directives/for.directive'; InputEventDebounceDirective, LocalizationPipe, ClickEventStopPropagationDirective, + AbstractNgModelComponent, ], providers: [LocalizationPipe], entryComponents: [RouterOutletComponent, DynamicLayoutComponent], diff --git a/npm/ng-packs/packages/core/src/public-api.ts b/npm/ng-packs/packages/core/src/public-api.ts index 0209f124de..4178d9e13f 100644 --- a/npm/ng-packs/packages/core/src/public-api.ts +++ b/npm/ng-packs/packages/core/src/public-api.ts @@ -3,6 +3,7 @@ */ // export * from './lib/handlers'; +export * from './lib/abstracts'; export * from './lib/actions'; export * from './lib/components'; export * from './lib/constants';