-
Notifications
You must be signed in to change notification settings - Fork 357
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(mixin): add ngModel mixin for reuse (control value accessor) (#…
…1024) * feat(mixin): add ngModel mixin for reuse (control value accessor) * feat(): use ngModel mixin where possible
- Loading branch information
1 parent
ba2b5ed
commit 7a30cb5
Showing
7 changed files
with
194 additions
and
218 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
src/platform/core/common/behaviors/control-value-accesor.mixin.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { mixinControlValueAccessor } from './control-value-accesor.mixin'; | ||
import { ChangeDetectorRef } from '@angular/core'; | ||
|
||
describe('ControlValueAccessorMixin', () => { | ||
|
||
it('should augment an existing class with a writeValue property', () => { | ||
const classWithControlValueAccess: any = mixinControlValueAccessor(TestClass); | ||
const instance: any = new classWithControlValueAccess(); | ||
|
||
expect(instance.value) | ||
.toBeUndefined(); | ||
expect(instance.writeValue) | ||
.toBeTruthy(); | ||
}); | ||
|
||
it('should agument and set an initial empty array', () => { | ||
const classWithControlValueAccess: any = mixinControlValueAccessor(TestClass, []); | ||
const instance: any = new classWithControlValueAccess(); | ||
|
||
expect(instance.value instanceof Array).toBeTruthy(); | ||
expect(instance.value.length === 0).toBeTruthy(); | ||
}); | ||
}); | ||
|
||
class TestClass { | ||
/** Fake instance of an ChangeDetectorRef. */ | ||
_changeDetectorRef: ChangeDetectorRef = { | ||
markForCheck: function(): void { | ||
/* empty */ | ||
}, | ||
detach: function(): void { | ||
/* empty */ | ||
}, | ||
detectChanges: function(): void { | ||
/* empty */ | ||
}, | ||
checkNoChanges: function (): void { | ||
/* empty */ | ||
}, | ||
reattach: function (): void { | ||
/* empty */ | ||
}, | ||
}; | ||
} |
66 changes: 66 additions & 0 deletions
66
src/platform/core/common/behaviors/control-value-accesor.mixin.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import { Constructor } from './constructor'; | ||
import { ChangeDetectorRef } from '@angular/core'; | ||
import { ControlValueAccessor } from '@angular/forms'; | ||
|
||
import { Observable } from 'rxjs/Observable'; | ||
import { Subject } from 'rxjs/Subject'; | ||
|
||
const noop: any = () => { | ||
// empty method | ||
}; | ||
|
||
export interface IControlValueAccessor extends ControlValueAccessor { | ||
value: any; | ||
valueChanges: Observable<any>; | ||
onChange: (_: any) => any; | ||
onTouched: () => any; | ||
} | ||
|
||
export interface IHasChangeDetectorRef { | ||
_changeDetectorRef: ChangeDetectorRef; | ||
} | ||
|
||
/** Mixin to augment a component with ngModel support. */ | ||
export function mixinControlValueAccessor<T extends Constructor<IHasChangeDetectorRef>> | ||
(base: T, initialValue?: any): Constructor<IControlValueAccessor> & T { | ||
return class extends base { | ||
private _value: any = initialValue; | ||
private _subjectValueChanges: Subject<any>; | ||
valueChanges: Observable<any>; | ||
|
||
constructor(...args: any[]) { | ||
super(...args); | ||
this._subjectValueChanges = new Subject<any>(); | ||
this.valueChanges = this._subjectValueChanges.asObservable(); | ||
} | ||
|
||
set value(v: any) { | ||
if (v !== this._value) { | ||
this._value = v; | ||
this.onChange(v); | ||
this._changeDetectorRef.markForCheck(); | ||
this._subjectValueChanges.next(v); | ||
} | ||
} | ||
get value(): any { | ||
return this._value; | ||
} | ||
|
||
writeValue(value: any): void { | ||
this.value = value; | ||
this._changeDetectorRef.markForCheck(); | ||
} | ||
|
||
registerOnChange(fn: any): void { | ||
this.onChange = fn; | ||
} | ||
|
||
registerOnTouched(fn: any): void { | ||
this.onTouched = fn; | ||
} | ||
|
||
onChange = (_: any) => noop; | ||
onTouched = () => noop; | ||
|
||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.