Skip to content

Commit

Permalink
feat(react-composition): restore decoratable hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
Pavel910 committed Apr 3, 2024
1 parent 3912e6a commit 5607607
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 7 deletions.
28 changes: 27 additions & 1 deletion packages/react-composition/src/decorators.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import {
GenericComponent,
Decorator,
CanReturnNull,
GenericHook
GenericHook,
DecoratableHook
} from "~/types";

export interface ShouldDecorate<TDecorator = any, TComponent = any> {
Expand Down Expand Up @@ -63,6 +64,21 @@ export function createDecoratorFactory<TDecorator>() {
};
}

export function createHookDecoratorFactory<TDecorator>() {
return function from<TDecoratable extends DecoratableComponent>(decoratable: TDecoratable) {
return function createDecorator(decorator: Decorator<GetDecoratee<TDecoratable>>) {
return function DecoratorPlugin(props: TDecorator) {

Check warning on line 70 in packages/react-composition/src/decorators.tsx

View workflow job for this annotation

GitHub Actions / Static code analysis

'props' is defined but never used
return (
<Compose
function={decoratable}
with={decorator as unknown as Decorator<GenericHook>}
/>
);
};
};
};
}

export function withDecoratorFactory<TDecorator>() {
return function WithDecorator<TDecoratable extends DecoratableComponent>(
Component: TDecoratable,
Expand All @@ -75,3 +91,13 @@ export function withDecoratorFactory<TDecorator>() {
> & { createDecorator: typeof createDecorator };
};
}

export function withHookDecoratorFactory<TDecorator>() {
return function WithHookDecorator<TDecoratable extends DecoratableHook>(hook: TDecoratable) {
const createDecorator = createHookDecoratorFactory<TDecorator>()(hook);

return Object.assign(hook, { createDecorator }) as unknown as DecoratableHook<
GenericHook<GetDecorateeParams<GetDecoratee<TDecoratable>>>
> & { createDecorator: typeof createDecorator };
};
}
25 changes: 20 additions & 5 deletions packages/react-composition/src/makeDecoratable.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import React, { createContext, useContext, useMemo } from "react";
import { useComponent } from "./Context";
import { DecoratableComponent, DecoratableHook, GenericComponent, GenericHook } from "~/types";
import { withDecoratorFactory } from "~/decorators";
import {
CanReturnNull,
DecoratableComponent,
DecoratableHook,
GenericComponent,
GenericHook
} from "~/types";
import { withDecoratorFactory, withHookDecoratorFactory } from "~/decorators";

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

decoratableHook.original = hook;

return decoratableHook as DecoratableHook<T>;
return withHookDecoratorFactory()(decoratableHook as DecoratableHook<T>);
}

export function createVoidComponent<T>() {
Expand All @@ -69,6 +75,15 @@ export function createVoidComponent<T>() {
};
}

export function makeDecoratable<T extends GenericComponent>(name: string, Component: T) {
return makeDecoratableComponent(name, Component);
export function makeDecoratable<T extends GenericHook>(hook: T): DecoratableHook<T>;
export function makeDecoratable<T extends GenericComponent>(
name: string,
Component: T
): DecoratableComponent<CanReturnNull<T>>;
export function makeDecoratable(hookOrName: any, Component?: any) {
if (Component) {
return makeDecoratableComponent(hookOrName, Component);
}

return makeDecoratableHook(hookOrName);
}
2 changes: 1 addition & 1 deletion packages/react-composition/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from "react";

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

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

Expand Down

0 comments on commit 5607607

Please sign in to comment.