diff options
Diffstat (limited to 'packages/web-util/src/components/utils.ts')
-rw-r--r-- | packages/web-util/src/components/utils.ts | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/packages/web-util/src/components/utils.ts b/packages/web-util/src/components/utils.ts index 34693f7d7..75c3fc0fe 100644 --- a/packages/web-util/src/components/utils.ts +++ b/packages/web-util/src/components/utils.ts @@ -12,6 +12,7 @@ export function compose<SType extends { status: string }, PType>( hook: (p: PType) => RecursiveState<SType>, viewMap: StateViewMap<SType>, ): (p: PType) => VNode { + function withHook(stateHook: () => RecursiveState<SType>): () => VNode { function ComposedComponent(): VNode { const state = stateHook(); @@ -35,6 +36,33 @@ export function compose<SType extends { status: string }, PType>( }; } +export function recursive<PType>( + hook: (p: PType) => RecursiveState<VNode>, +): (p: PType) => VNode { + + function withHook(stateHook: () => RecursiveState<VNode>): () => VNode { + function ComposedComponent(): VNode { + const state = stateHook(); + + if (typeof state === "function") { + const subComponent = withHook(state); + return createElement(subComponent, {}); + } + + return state; + } + + return ComposedComponent; + } + + return (p: PType) => { + const h = withHook(() => hook(p)); + return h(); + }; +} + + + /** * * @param obj VNode |