-
Notifications
You must be signed in to change notification settings - Fork 477
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cdk):
DirectiveBinding
/ DirectiveListener
add utils
Signed-off-by: waterplea <[email protected]>
- Loading branch information
Showing
5 changed files
with
50 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 |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import type {ProviderToken, WritableSignal} from '@angular/core'; | ||
import {effect, inject, isSignal, signal} from '@angular/core'; | ||
|
||
export function tuiDirectiveBinding<T, G extends keyof T, R>( | ||
token: ProviderToken<T>, | ||
key: G, | ||
initial: T[G] extends WritableSignal<R> ? R : T[G], | ||
): WritableSignal<typeof initial> { | ||
const result = signal(initial); | ||
const directive = inject(token); | ||
|
||
effect( | ||
() => { | ||
const value: any = result(); | ||
|
||
if (isSignal(directive[key])) { | ||
(directive[key] as WritableSignal<T[G]>).set(value); | ||
} else { | ||
directive[key] = value; | ||
} | ||
}, | ||
{allowSignalWrites: true}, | ||
); | ||
|
||
return result; | ||
} |
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,20 @@ | ||
import type {ProviderToken, Signal} from '@angular/core'; | ||
import {inject, isSignal} from '@angular/core'; | ||
import {toSignal} from '@angular/core/rxjs-interop'; | ||
import type {Observable} from 'rxjs'; | ||
|
||
type OutputKeysOf<T> = { | ||
[K in keyof T]: T[K] extends Observable<any> | Signal<any> ? K : never; | ||
}[keyof T]; | ||
|
||
type OutputTypeOf<T> = | ||
T extends Signal<infer R> ? R : T extends Observable<infer O> ? O : never; | ||
|
||
export function tuiDirectiveListener<T, K extends OutputKeysOf<T>>( | ||
token: ProviderToken<T>, | ||
key: K, | ||
): Signal<OutputTypeOf<T[K]>> { | ||
const prop: any = inject(token)[key]; | ||
|
||
return isSignal(prop) ? prop : toSignal<any>(prop); | ||
} |
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