-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
/
Copy pathflex-render-component.ts
119 lines (111 loc) · 3.28 KB
/
flex-render-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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import {
ComponentMirror,
Injector,
InputSignal,
OutputEmitterRef,
reflectComponentType,
Type,
} from '@angular/core'
type Inputs<T> = {
[K in keyof T as T[K] extends InputSignal<infer R>
? K
: never]?: T[K] extends InputSignal<infer R> ? R : never
}
type Outputs<T> = {
[K in keyof T as T[K] extends OutputEmitterRef<infer R>
? K
: never]?: T[K] extends OutputEmitterRef<infer R>
? OutputEmitterRef<R>['emit']
: never
}
type OptionalKeys<T, K = keyof T> = K extends keyof T
? T[K] extends Required<T>[K]
? undefined extends T[K]
? K
: never
: K
: never
interface FlexRenderRequiredOptions<
TInputs extends Record<string, any>,
TOutputs extends Record<string, any>,
> {
/**
* Component instance inputs. They will be set via [componentRef.setInput API](https://angular.dev/api/core/ComponentRef#setInput)
*/
inputs: TInputs
/**
* Component instance outputs.
*/
outputs?: TOutputs
/**
* Optional {@link Injector} that will be used when rendering the component
*/
injector?: Injector
}
interface FlexRenderOptions<
TInputs extends Record<string, any>,
TOutputs extends Record<string, any>,
> {
/**
* Component instance inputs. They will be set via [componentRef.setInput API](https://angular.dev/api/core/ComponentRef#setInput)
*/
inputs?: TInputs
/**
* Component instance outputs.
*/
outputs?: TOutputs
/**
* Optional {@link Injector} that will be used when rendering the component
*/
injector?: Injector
}
/**
* Helper function to create a [@link FlexRenderComponent] instance, with better type-safety.
*
* - options object must be passed when the given component instance contains at least one required signal input.
* - options/inputs is typed with the given component inputs
* - options/outputs is typed with the given component outputs
*/
export function flexRenderComponent<
TComponent = any,
TInputs extends Inputs<TComponent> = Inputs<TComponent>,
TOutputs extends Outputs<TComponent> = Outputs<TComponent>,
>(
component: Type<TComponent>,
...options: OptionalKeys<TInputs> extends never
? [FlexRenderOptions<TInputs, TOutputs>?]
: [FlexRenderRequiredOptions<TInputs, TOutputs>]
) {
const { inputs, injector, outputs } = options?.[0] ?? {}
return new FlexRenderComponent(component, inputs, injector, outputs)
}
/**
* Wrapper class for a component that will be used as content for {@link FlexRenderDirective}
*
* Prefer {@link flexRenderComponent} helper for better type-safety
*/
export class FlexRenderComponent<TComponent = any> {
readonly mirror: ComponentMirror<TComponent>
readonly allowedInputNames: Array<string> = []
readonly allowedOutputNames: Array<string> = []
constructor(
readonly component: Type<TComponent>,
readonly inputs?: Inputs<TComponent>,
readonly injector?: Injector,
readonly outputs?: Outputs<TComponent>,
) {
const mirror = reflectComponentType(component)
if (!mirror) {
throw new Error(
`[@tanstack-table/angular] The provided symbol is not a component`,
)
}
this.mirror = mirror
for (const input of this.mirror.inputs) {
this.allowedInputNames.push(input.propName)
}
for (const output of this.mirror.outputs) {
this.allowedOutputNames.push(output.propName)
}
}
}