-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): Added CheckboxControlValueAccessor
relates #14
- Loading branch information
Showing
4 changed files
with
52 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
export { DefaultControlValueAccessorDirective } from './default-control-value-accessor.directive'; | ||
export { NumberControlValueAccessorDirective } from './number-control-value-accessor.directive'; | ||
export { RangeControlValueAccessorDirective } from './range-control-value-accessor.directive'; | ||
export { CheckboxControlValueAccessorDirective } from './checkbox-control-value-accessor.directive'; | ||
export { SelectControlValueAccessorDirective, NgqpSelectOptionDirective } from './select-control-value-accessor.directive'; |
48 changes: 48 additions & 0 deletions
48
projects/ngqp/core/src/lib/accessors/checkbox-control-value-accessor.directive.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,48 @@ | ||
import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms'; | ||
import { Directive, ElementRef, forwardRef, HostListener, Renderer2 } from '@angular/core'; | ||
|
||
export const NGQP_CHECKBOX_VALUE_ACCESSOR: any = { | ||
provide: NG_VALUE_ACCESSOR, | ||
useExisting: forwardRef(() => CheckboxControlValueAccessorDirective), | ||
multi: true | ||
}; | ||
|
||
@Directive({ | ||
selector: 'input[type=checkbox][queryParamName]', | ||
providers: [NGQP_CHECKBOX_VALUE_ACCESSOR], | ||
}) | ||
export class CheckboxControlValueAccessorDirective implements ControlValueAccessor { | ||
|
||
private fnChange = (_: boolean) => {}; | ||
private fnTouched = () => {}; | ||
|
||
@HostListener('change', ['$event']) | ||
public onInput(event: UIEvent) { | ||
this.fnChange((event.target as HTMLInputElement).checked); | ||
} | ||
|
||
@HostListener('blur') | ||
public onBlur() { | ||
this.fnTouched(); | ||
} | ||
|
||
constructor(private renderer: Renderer2, private elementRef: ElementRef) { | ||
} | ||
|
||
public writeValue(value: any) { | ||
this.renderer.setProperty(this.elementRef.nativeElement, 'checked', value); | ||
} | ||
|
||
public registerOnChange(fn: any) { | ||
this.fnChange = fn; | ||
} | ||
|
||
public registerOnTouched(fn: any) { | ||
this.fnTouched = fn; | ||
} | ||
|
||
public setDisabledState(isDisabled: boolean) { | ||
this.renderer.setProperty(this.elementRef.nativeElement, 'disabled', isDisabled); | ||
} | ||
|
||
} |
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
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