Skip to content

Commit 78e5380

Browse files
committed
feat(forms): update interface and add example implementation
1 parent 62b167b commit 78e5380

5 files changed

Lines changed: 43 additions & 14 deletions

File tree

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import {
2+
Directive,
3+
EventEmitter,
4+
Injectable,
5+
Input,
6+
Output,
7+
} from '@angular/core';
8+
import { toSignal } from '@angular/core/rxjs-interop';
9+
import { merge, Subject, SubjectLike, Subscribable } from 'rxjs';
10+
11+
@Injectable()
12+
export abstract class ComponentValueAccessorHost<T = any> {
13+
abstract valueInput$: SubjectLike<T>;
14+
abstract valueChange$: Subscribable<T>;
15+
abstract disabled$: SubjectLike<boolean>;
16+
abstract touched$: Subscribable<void>;
17+
}
18+
19+
@Directive()
20+
export class SimpleComponentValueAccessorHost<T>
21+
implements ComponentValueAccessorHost<T>
22+
{
23+
// prettier-ignore
24+
@Input('value') set valueInput(v: T) { this.valueInput$.next(v) }
25+
valueInput$ = new Subject<T>();
26+
27+
@Output('valueChange')
28+
valueChange$ = new EventEmitter<T>();
29+
30+
// prettier-ignore
31+
@Input('disabled') set disabledInput(v: boolean) { this.disabled$.next(v) }
32+
disabled$ = new Subject<boolean>();
33+
34+
@Output('touched')
35+
touched$ = new EventEmitter<void>();
36+
37+
value = toSignal(merge(this.valueInput$, this.valueChange$));
38+
disabled = toSignal(this.disabled$);
39+
}

packages/forms/src/value-accessing/accessor.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { inject, Injectable } from '@angular/core';
22
import { ControlValueAccessor } from '@angular/forms';
33

4-
import { ComponentValueAccessorHost } from './host';
4+
import { ComponentValueAccessorHost } from './accessor-host';
55

66
@Injectable()
77
export class ComponentValueAccessor implements ControlValueAccessor {
@@ -16,6 +16,6 @@ export class ComponentValueAccessor implements ControlValueAccessor {
1616
this.host.touched$.subscribe({ next: fn });
1717
}
1818
setDisabledState(disabled: boolean): void {
19-
this.host.disabled$?.next(disabled);
19+
this.host.disabled$.next(disabled);
2020
}
2121
}

packages/forms/src/value-accessing/host.ts

Lines changed: 0 additions & 10 deletions
This file was deleted.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
export * from './accessor';
2-
export * from './host';
2+
export * from './accessor-host';
33
export * from './provide';

packages/forms/src/value-accessing/provide.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { NG_VALUE_ACCESSOR } from '@angular/forms';
33
import { provide, provideMulti } from '@angularity/core';
44

55
import { ComponentValueAccessor } from './accessor';
6-
import { ComponentValueAccessorHost } from './host';
6+
import { ComponentValueAccessorHost } from './accessor-host';
77

88
export function provideComponentValueAccessor(
99
host: ProviderToken<ComponentValueAccessorHost>,

0 commit comments

Comments
 (0)