Last active
March 14, 2022 17:14
-
-
Save Stradivario/eefb4deb844511ff9b918afe2a93bc26 to your computer and use it in GitHub Desktop.
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
/* eslint-disable @typescript-eslint/no-unused-vars */ | |
import {EventEmitter, forwardRef} from '@angular/core'; | |
import { | |
ControlValueAccessor as Control, | |
NG_VALUE_ACCESSOR, | |
} from '@angular/forms'; | |
export const VALUE_ACCESSOR = <T>(clazz: T) => ({ | |
provide: NG_VALUE_ACCESSOR, | |
useExisting: forwardRef(() => clazz), | |
multi: true, | |
}); | |
export class ControlValueAccessor<T = any> implements Control { | |
value?: T; | |
valueChange: EventEmitter<T>; | |
onChange = (value: T) => undefined; | |
onTouched = (value: T) => undefined; | |
writeValue(value: T): void { | |
this.value = value || this.value; | |
} | |
pushChanges(value: T) { | |
this.onChange(value); | |
} | |
registerOnChange(fn: (value: T) => undefined): void { | |
this.onChange = fn; | |
} | |
registerOnTouched(fn: (value: T) => undefined): void { | |
this.onTouched = fn; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment