-
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 NumberControlValueAccessor
relates #14
- Loading branch information
Showing
6 changed files
with
69 additions
and
15 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,2 +1,3 @@ | ||
export { NgqpDefaultControlValueAccessorDirective } from './default-control-value-accessor.directive'; | ||
export { NgqpSelectControlValueAccessorDirective, NgqpSelectOptionDirective } from './select-control-value-accessor.directive'; | ||
export { DefaultControlValueAccessorDirective } from './default-control-value-accessor.directive'; | ||
export { NumberControlValueAccessorDirective } from './number-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
51 changes: 51 additions & 0 deletions
51
projects/ngqp/core/src/lib/accessors/number-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,51 @@ | ||
import { Directive, ElementRef, forwardRef, HostListener, Renderer2 } from '@angular/core'; | ||
import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms'; | ||
|
||
export const NGQP_NUMBER_VALUE_ACCESSOR: any = { | ||
provide: NG_VALUE_ACCESSOR, | ||
useExisting: forwardRef(() => NumberControlValueAccessorDirective), | ||
multi: true | ||
}; | ||
|
||
@Directive({ | ||
selector: 'input[type=number][queryParamName]', | ||
providers: [NGQP_NUMBER_VALUE_ACCESSOR], | ||
}) | ||
export class NumberControlValueAccessorDirective implements ControlValueAccessor { | ||
|
||
private fnChange = (_: number) => {}; | ||
private fnTouched = () => {}; | ||
|
||
@HostListener('change', ['$event']) | ||
@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: string) { | ||
const normalizedValue = value === null ? '' : value; | ||
this.renderer.setProperty(this.elementRef.nativeElement, 'value', normalizedValue); | ||
} | ||
|
||
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
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