-
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 RangeControlValueAccessor
relates #14
- Loading branch information
Showing
4 changed files
with
53 additions
and
1 deletion.
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,3 +1,4 @@ | ||
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 { SelectControlValueAccessorDirective, NgqpSelectOptionDirective } from './select-control-value-accessor.directive'; |
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
49 changes: 49 additions & 0 deletions
49
projects/ngqp/core/src/lib/accessors/range-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,49 @@ | ||
import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms'; | ||
import { Directive, ElementRef, forwardRef, HostListener, Renderer2 } from '@angular/core'; | ||
|
||
export const NGQP_RANGE_VALUE_ACCESSOR: any = { | ||
provide: NG_VALUE_ACCESSOR, | ||
useExisting: forwardRef(() => RangeControlValueAccessorDirective), | ||
multi: true | ||
}; | ||
|
||
@Directive({ | ||
selector: 'input[type=range][queryParamName]', | ||
providers: [NGQP_RANGE_VALUE_ACCESSOR], | ||
}) | ||
export class RangeControlValueAccessorDirective implements ControlValueAccessor { | ||
|
||
private fnChange = (_: number) => {}; | ||
private fnTouched = () => {}; | ||
|
||
@HostListener('input', ['$event']) | ||
public onInput(event: UIEvent) { | ||
const value = (event.target as HTMLInputElement).value; | ||
this.fnChange(value === '' ? null : parseFloat(value)); | ||
} | ||
|
||
@HostListener('blur') | ||
public onBlur() { | ||
this.fnTouched(); | ||
} | ||
|
||
constructor(private renderer: Renderer2, private elementRef: ElementRef) { | ||
} | ||
|
||
public writeValue(value: any) { | ||
this.renderer.setProperty(this.elementRef.nativeElement, 'value', parseFloat(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