-
Notifications
You must be signed in to change notification settings - Fork 2
/
slider-with-value.component.ts
64 lines (54 loc) · 1.64 KB
/
slider-with-value.component.ts
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import {Component, effect, input, signal, WritableSignal} from '@angular/core';
import {ControlValueAccessor, NG_VALUE_ACCESSOR, ReactiveFormsModule} from "@angular/forms";
import {OnChange, OnTouched} from "../../types/value-accessor";
@Component({
selector: 'app-slider-with-value',
standalone: true,
imports: [
ReactiveFormsModule
],
templateUrl: './slider-with-value.component.html',
styleUrl: './slider-with-value.component.scss',
providers: [
{
provide: NG_VALUE_ACCESSOR,
multi: true,
useExisting: SliderWithValueComponent,
},
]
})
export class SliderWithValueComponent implements ControlValueAccessor {
protected readonly Number = Number;
private onChange: WritableSignal<OnChange<number> | null> = signal(null);
private onTouched: WritableSignal<OnTouched | null> = signal(null);
public inputId = input<string>();
public required = input(false);
public min = input<number | null>(null);
public max = input<number | null>(null);
public step = input<number | null>(null);
public disabled = signal(false);
public value = signal(0);
constructor() {
effect(() => {
if (this.onChange() === null) {
return;
}
(this.onChange()!)(this.value());
if (this.onTouched() !== null) {
(this.onTouched()!)();
}
});
}
public registerOnChange(fn: OnChange<number>): void {
this.onChange.set(fn);
}
public registerOnTouched(fn: OnTouched): void {
this.onTouched.set(fn);
}
public setDisabledState(isDisabled: boolean): void {
this.disabled.set(isDisabled);
}
public writeValue(obj: number): void {
this.value.set(obj);
}
}