Skip to content

Commit 5607607

Browse files
committed
feat(react-composition): restore decoratable hooks
1 parent 3912e6a commit 5607607

File tree

3 files changed

+48
-7
lines changed

3 files changed

+48
-7
lines changed

packages/react-composition/src/decorators.tsx

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ import {
66
GenericComponent,
77
Decorator,
88
CanReturnNull,
9-
GenericHook
9+
GenericHook,
10+
DecoratableHook
1011
} from "~/types";
1112

1213
export interface ShouldDecorate<TDecorator = any, TComponent = any> {
@@ -63,6 +64,21 @@ export function createDecoratorFactory<TDecorator>() {
6364
};
6465
}
6566

67+
export function createHookDecoratorFactory<TDecorator>() {
68+
return function from<TDecoratable extends DecoratableComponent>(decoratable: TDecoratable) {
69+
return function createDecorator(decorator: Decorator<GetDecoratee<TDecoratable>>) {
70+
return function DecoratorPlugin(props: TDecorator) {
71+
return (
72+
<Compose
73+
function={decoratable}
74+
with={decorator as unknown as Decorator<GenericHook>}
75+
/>
76+
);
77+
};
78+
};
79+
};
80+
}
81+
6682
export function withDecoratorFactory<TDecorator>() {
6783
return function WithDecorator<TDecoratable extends DecoratableComponent>(
6884
Component: TDecoratable,
@@ -75,3 +91,13 @@ export function withDecoratorFactory<TDecorator>() {
7591
> & { createDecorator: typeof createDecorator };
7692
};
7793
}
94+
95+
export function withHookDecoratorFactory<TDecorator>() {
96+
return function WithHookDecorator<TDecoratable extends DecoratableHook>(hook: TDecoratable) {
97+
const createDecorator = createHookDecoratorFactory<TDecorator>()(hook);
98+
99+
return Object.assign(hook, { createDecorator }) as unknown as DecoratableHook<
100+
GenericHook<GetDecorateeParams<GetDecoratee<TDecoratable>>>
101+
> & { createDecorator: typeof createDecorator };
102+
};
103+
}

packages/react-composition/src/makeDecoratable.tsx

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
import React, { createContext, useContext, useMemo } from "react";
22
import { useComponent } from "./Context";
3-
import { DecoratableComponent, DecoratableHook, GenericComponent, GenericHook } from "~/types";
4-
import { withDecoratorFactory } from "~/decorators";
3+
import {
4+
CanReturnNull,
5+
DecoratableComponent,
6+
DecoratableHook,
7+
GenericComponent,
8+
GenericHook
9+
} from "~/types";
10+
import { withDecoratorFactory, withHookDecoratorFactory } from "~/decorators";
511

612
const ComposableContext = createContext<string[]>([]);
713
ComposableContext.displayName = "ComposableContext";
@@ -59,7 +65,7 @@ export function makeDecoratableHook<T extends GenericHook>(hook: T) {
5965

6066
decoratableHook.original = hook;
6167

62-
return decoratableHook as DecoratableHook<T>;
68+
return withHookDecoratorFactory()(decoratableHook as DecoratableHook<T>);
6369
}
6470

6571
export function createVoidComponent<T>() {
@@ -69,6 +75,15 @@ export function createVoidComponent<T>() {
6975
};
7076
}
7177

72-
export function makeDecoratable<T extends GenericComponent>(name: string, Component: T) {
73-
return makeDecoratableComponent(name, Component);
78+
export function makeDecoratable<T extends GenericHook>(hook: T): DecoratableHook<T>;
79+
export function makeDecoratable<T extends GenericComponent>(
80+
name: string,
81+
Component: T
82+
): DecoratableComponent<CanReturnNull<T>>;
83+
export function makeDecoratable(hookOrName: any, Component?: any) {
84+
if (Component) {
85+
return makeDecoratableComponent(hookOrName, Component);
86+
}
87+
88+
return makeDecoratableHook(hookOrName);
7489
}

packages/react-composition/src/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import React from "react";
22

3-
export type GenericHook = (...args: any) => any;
3+
export type GenericHook<TParams = any, TReturn = any> = (...args: TParams[]) => TReturn;
44

55
export type GenericComponent<T = any> = React.FunctionComponent<T>;
66

0 commit comments

Comments
 (0)