-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Expand file tree
/
Copy pathnumeric-value-accessor.ts
More file actions
34 lines (31 loc) · 1.12 KB
/
numeric-value-accessor.ts
File metadata and controls
34 lines (31 loc) · 1.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import { Directive, HostListener, ElementRef, Injector } from '@angular/core';
import { NG_VALUE_ACCESSOR } from '@angular/forms';
import { ValueAccessor } from '@ionic/angular/common';
@Directive({
selector: 'ion-input[type=number],ion-input-otp:not([type=text]),ion-range',
providers: [
{
provide: NG_VALUE_ACCESSOR,
useExisting: NumericValueAccessorDirective,
multi: true,
},
],
})
export class NumericValueAccessorDirective extends ValueAccessor {
constructor(injector: Injector, private el: ElementRef<HTMLInputElement | HTMLIonRangeElement>) {
super(injector, el);
}
@HostListener('ionInput', ['$event.target'])
handleInputEvent(el: HTMLIonInputElement | HTMLIonInputOtpElement | HTMLIonRangeElement): void {
this.handleValueChange(el, el.value);
}
registerOnChange(fn: (_: number | null) => void): void {
if (this.el.nativeElement.tagName === 'ION-INPUT' || this.el.nativeElement.tagName === 'ION-INPUT-OTP') {
super.registerOnChange((value: string) => {
fn(value === '' ? null : parseFloat(value));
});
} else {
super.registerOnChange(fn);
}
}
}