@webiny/react-composition 0.0.0-unstable.79032b23a5 → 0.0.0-unstable.7be00a75a9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/Compose.d.ts +6 -12
- package/Compose.js +54 -20
- package/Compose.js.map +1 -1
- package/CompositionScope.d.ts +17 -0
- package/CompositionScope.js +25 -0
- package/CompositionScope.js.map +1 -0
- package/Context.d.ts +24 -27
- package/Context.js +48 -102
- package/Context.js.map +1 -1
- package/README.md +9 -6
- package/createDecorator.d.ts +19 -0
- package/createDecorator.js +17 -0
- package/createDecorator.js.map +1 -0
- package/decorators.d.ts +15 -0
- package/decorators.js +63 -0
- package/decorators.js.map +1 -0
- package/domain/CompositionStore.d.ts +21 -0
- package/domain/CompositionStore.js +89 -0
- package/domain/CompositionStore.js.map +1 -0
- package/index.d.ts +9 -4
- package/index.js +8 -49
- package/makeComposable.d.ts +43 -3
- package/makeComposable.js +9 -55
- package/makeComposable.js.map +1 -1
- package/makeDecoratable.d.ts +31 -0
- package/makeDecoratable.js +90 -0
- package/makeDecoratable.js.map +1 -0
- package/package.json +17 -22
- package/types.d.ts +35 -0
- package/types.js +0 -0
- package/createComponentPlugin.d.ts +0 -8
- package/createComponentPlugin.js +0 -24
- package/createComponentPlugin.js.map +0 -1
- package/index.js.map +0 -1
package/Compose.d.ts
CHANGED
|
@@ -1,15 +1,9 @@
|
|
|
1
1
|
import React from "react";
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
original: React.FC<TProps>;
|
|
5
|
-
originalName: string;
|
|
6
|
-
}
|
|
2
|
+
import type { DecoratableTypes } from "./Context.js";
|
|
3
|
+
import type { ComposeWith } from "./types.js";
|
|
7
4
|
export interface ComposeProps {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
*/
|
|
12
|
-
component: ComposableFC<any>;
|
|
13
|
-
with: HigherOrderComponent | HigherOrderComponent[];
|
|
5
|
+
function?: DecoratableTypes;
|
|
6
|
+
component?: DecoratableTypes;
|
|
7
|
+
with: ComposeWith;
|
|
14
8
|
}
|
|
15
|
-
export declare const Compose: React.
|
|
9
|
+
export declare const Compose: (props: ComposeProps) => React.JSX.Element | null;
|
package/Compose.js
CHANGED
|
@@ -1,22 +1,56 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
return;
|
|
1
|
+
import react, { useEffect, useRef } from "react";
|
|
2
|
+
import { useCompositionStore } from "./Context.js";
|
|
3
|
+
import { useCompositionScope } from "./CompositionScope.js";
|
|
4
|
+
const Compose = (props)=>{
|
|
5
|
+
const store = useCompositionStore();
|
|
6
|
+
const { scope, inherit } = useCompositionScope();
|
|
7
|
+
const targetFn = props.function ?? props.component;
|
|
8
|
+
if (!targetFn) {
|
|
9
|
+
console.warn("You must provide a function or a component to compose with!", props);
|
|
10
|
+
return null;
|
|
11
|
+
}
|
|
12
|
+
if (void 0 === targetFn.original) {
|
|
13
|
+
console.warn(`You must make your function "${targetFn.originalName ?? targetFn.name}" composable, by using the makeDecoratable() function!`);
|
|
14
|
+
return null;
|
|
16
15
|
}
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
16
|
+
const decorators = Array.isArray(props.with) ? props.with : [
|
|
17
|
+
props.with
|
|
18
|
+
];
|
|
19
|
+
const currentScope = scope[scope.length - 1] ?? "*";
|
|
20
|
+
store.register(targetFn.original, decorators, currentScope, inherit, true);
|
|
21
|
+
return /*#__PURE__*/ react.createElement(ComposeEffects, {
|
|
22
|
+
store: store,
|
|
23
|
+
target: targetFn.original,
|
|
24
|
+
decorators: decorators,
|
|
25
|
+
scope: currentScope,
|
|
26
|
+
inherit: inherit
|
|
27
|
+
});
|
|
21
28
|
};
|
|
22
|
-
|
|
29
|
+
function ComposeEffects({ store, target, decorators, scope, inherit }) {
|
|
30
|
+
const prevRef = useRef(null);
|
|
31
|
+
const liveRef = useRef(decorators);
|
|
32
|
+
if (liveRef.current !== decorators) {
|
|
33
|
+
store.register(target, decorators, scope, inherit, true, liveRef.current);
|
|
34
|
+
liveRef.current = decorators;
|
|
35
|
+
}
|
|
36
|
+
useEffect(()=>{
|
|
37
|
+
const prev = prevRef.current;
|
|
38
|
+
if (prev && (prev.decorators !== decorators || prev.scope !== scope)) store.notify();
|
|
39
|
+
prevRef.current = {
|
|
40
|
+
decorators,
|
|
41
|
+
scope
|
|
42
|
+
};
|
|
43
|
+
return ()=>{
|
|
44
|
+
store.unregister(target, decorators, scope);
|
|
45
|
+
};
|
|
46
|
+
}, [
|
|
47
|
+
store,
|
|
48
|
+
target,
|
|
49
|
+
decorators,
|
|
50
|
+
scope
|
|
51
|
+
]);
|
|
52
|
+
return null;
|
|
53
|
+
}
|
|
54
|
+
export { Compose };
|
|
55
|
+
|
|
56
|
+
//# sourceMappingURL=Compose.js.map
|
package/Compose.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"
|
|
1
|
+
{"version":3,"file":"Compose.js","sources":["../src/Compose.tsx"],"sourcesContent":["import React, { useEffect, useRef } from \"react\";\nimport type { DecoratableTypes } from \"./Context.js\";\nimport { useCompositionStore } from \"./Context.js\";\nimport { useCompositionScope } from \"~/CompositionScope.js\";\nimport type {\n ComposeWith,\n Decoratable,\n Decorator,\n GenericComponent,\n GenericHook\n} from \"./types.js\";\n\nexport interface ComposeProps {\n function?: DecoratableTypes;\n component?: DecoratableTypes;\n with: ComposeWith;\n}\n\nexport const Compose = (props: ComposeProps) => {\n const store = useCompositionStore();\n const { scope, inherit } = useCompositionScope();\n\n const targetFn = (props.function ?? props.component) as Decoratable;\n\n if (!targetFn) {\n console.warn(\"You must provide a function or a component to compose with!\", props);\n return null;\n }\n\n if (typeof targetFn.original === \"undefined\") {\n console.warn(\n `You must make your function \"${\n targetFn.originalName ?? targetFn.name\n }\" composable, by using the makeDecoratable() function!`\n );\n return null;\n }\n\n const decorators = (Array.isArray(props.with) ? props.with : [props.with]) as Decorator<\n GenericComponent | GenericHook\n >[];\n const currentScope = scope[scope.length - 1] ?? \"*\";\n\n // Register synchronously during render so decorators are available immediately.\n // Pass silent=true to avoid notifying listeners mid-render (which would trigger\n // setState in other components and cause React warnings).\n store.register(targetFn.original, decorators, currentScope, inherit, true);\n\n return (\n <ComposeEffects\n store={store}\n target={targetFn.original}\n decorators={decorators}\n scope={currentScope}\n inherit={inherit}\n />\n );\n};\n\n/**\n * Separate component for the effect to avoid re-running the cleanup on every render.\n * This component handles cleanup on unmount and when props change.\n */\nfunction ComposeEffects({\n store,\n target,\n decorators,\n scope,\n inherit\n}: {\n store: ReturnType<typeof useCompositionStore>;\n target: any;\n decorators: Decorator<GenericComponent | GenericHook>[];\n scope: string;\n inherit: boolean;\n}) {\n const prevRef = useRef<{\n decorators: Decorator<GenericComponent | GenericHook>[];\n scope: string;\n } | null>(null);\n\n // Tracks the decorators currently live in the store as of the last render.\n // Updated synchronously during render so it always reflects the most recently\n // registered decorators, allowing the atomic swap below to remove the right ones.\n const liveRef = useRef<Decorator<GenericComponent | GenericHook>[]>(decorators);\n\n // On re-render with new decorators: atomically replace the old ones in the store\n // during render (before React commits). This ensures the store never transiently\n // holds both old and new HOCs — which would cause useSyncExternalStore subscribers\n // to render with a doubly-wrapped component and mount inner components twice.\n if (liveRef.current !== decorators) {\n store.register(target, decorators, scope, inherit, true, liveRef.current);\n liveRef.current = decorators;\n }\n\n useEffect(() => {\n const prev = prevRef.current;\n\n // On prop change: the render-phase atomic swap already updated the store.\n // Emit a non-silent notification now that effects have settled so subscribers\n // re-render with the final, clean composition (old HOCs fully gone).\n if (prev && (prev.decorators !== decorators || prev.scope !== scope)) {\n store.notify();\n }\n\n prevRef.current = { decorators, scope };\n\n // Cleanup on unmount.\n return () => {\n store.unregister(target, decorators, scope);\n };\n }, [store, target, decorators, scope]);\n\n return null;\n}\n"],"names":["Compose","props","store","useCompositionStore","scope","inherit","useCompositionScope","targetFn","console","decorators","Array","currentScope","ComposeEffects","target","prevRef","useRef","liveRef","useEffect","prev"],"mappings":";;;AAkBO,MAAMA,UAAU,CAACC;IACpB,MAAMC,QAAQC;IACd,MAAM,EAAEC,KAAK,EAAEC,OAAO,EAAE,GAAGC;IAE3B,MAAMC,WAAYN,MAAM,QAAQ,IAAIA,MAAM,SAAS;IAEnD,IAAI,CAACM,UAAU;QACXC,QAAQ,IAAI,CAAC,+DAA+DP;QAC5E,OAAO;IACX;IAEA,IAAI,AAA6B,WAAtBM,SAAS,QAAQ,EAAkB;QAC1CC,QAAQ,IAAI,CACR,CAAC,6BAA6B,EAC1BD,SAAS,YAAY,IAAIA,SAAS,IAAI,CACzC,sDAAsD,CAAC;QAE5D,OAAO;IACX;IAEA,MAAME,aAAcC,MAAM,OAAO,CAACT,MAAM,IAAI,IAAIA,MAAM,IAAI,GAAG;QAACA,MAAM,IAAI;KAAC;IAGzE,MAAMU,eAAeP,KAAK,CAACA,MAAM,MAAM,GAAG,EAAE,IAAI;IAKhDF,MAAM,QAAQ,CAACK,SAAS,QAAQ,EAAEE,YAAYE,cAAcN,SAAS;IAErE,OAAO,WAAP,GACI,oBAACO,gBAAcA;QACX,OAAOV;QACP,QAAQK,SAAS,QAAQ;QACzB,YAAYE;QACZ,OAAOE;QACP,SAASN;;AAGrB;AAMA,SAASO,eAAe,EACpBV,KAAK,EACLW,MAAM,EACNJ,UAAU,EACVL,KAAK,EACLC,OAAO,EAOV;IACG,MAAMS,UAAUC,OAGN;IAKV,MAAMC,UAAUD,OAAoDN;IAMpE,IAAIO,QAAQ,OAAO,KAAKP,YAAY;QAChCP,MAAM,QAAQ,CAACW,QAAQJ,YAAYL,OAAOC,SAAS,MAAMW,QAAQ,OAAO;QACxEA,QAAQ,OAAO,GAAGP;IACtB;IAEAQ,UAAU;QACN,MAAMC,OAAOJ,QAAQ,OAAO;QAK5B,IAAII,QAASA,CAAAA,KAAK,UAAU,KAAKT,cAAcS,KAAK,KAAK,KAAKd,KAAI,GAC9DF,MAAM,MAAM;QAGhBY,QAAQ,OAAO,GAAG;YAAEL;YAAYL;QAAM;QAGtC,OAAO;YACHF,MAAM,UAAU,CAACW,QAAQJ,YAAYL;QACzC;IACJ,GAAG;QAACF;QAAOW;QAAQJ;QAAYL;KAAM;IAErC,OAAO;AACX"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import React from "react";
|
|
2
|
+
export interface CompositionScopeContext {
|
|
3
|
+
inherit: boolean;
|
|
4
|
+
scope: string[];
|
|
5
|
+
}
|
|
6
|
+
interface CompositionScopeProps {
|
|
7
|
+
name: string;
|
|
8
|
+
/**
|
|
9
|
+
* Use this prop on components that are used to register decorators.
|
|
10
|
+
* Components are inherited at the time of registration, and then cached.
|
|
11
|
+
*/
|
|
12
|
+
inherit?: boolean;
|
|
13
|
+
children: React.ReactNode;
|
|
14
|
+
}
|
|
15
|
+
export declare const CompositionScope: ({ name, inherit, children }: CompositionScopeProps) => React.JSX.Element;
|
|
16
|
+
export declare function useCompositionScope(): CompositionScopeContext;
|
|
17
|
+
export {};
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import react from "react";
|
|
2
|
+
const CompositionScopeContext = /*#__PURE__*/ react.createContext(void 0);
|
|
3
|
+
const CompositionScope = ({ name, inherit = false, children })=>{
|
|
4
|
+
const parentScope = useCompositionScope();
|
|
5
|
+
return /*#__PURE__*/ react.createElement(CompositionScopeContext.Provider, {
|
|
6
|
+
value: {
|
|
7
|
+
scope: [
|
|
8
|
+
...parentScope.scope,
|
|
9
|
+
name
|
|
10
|
+
],
|
|
11
|
+
inherit
|
|
12
|
+
}
|
|
13
|
+
}, children);
|
|
14
|
+
};
|
|
15
|
+
function useCompositionScope() {
|
|
16
|
+
const context = react.useContext(CompositionScopeContext);
|
|
17
|
+
if (!context) return {
|
|
18
|
+
scope: [],
|
|
19
|
+
inherit: false
|
|
20
|
+
};
|
|
21
|
+
return context;
|
|
22
|
+
}
|
|
23
|
+
export { CompositionScope, useCompositionScope };
|
|
24
|
+
|
|
25
|
+
//# sourceMappingURL=CompositionScope.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"CompositionScope.js","sources":["../src/CompositionScope.tsx"],"sourcesContent":["import React from \"react\";\n\nexport interface CompositionScopeContext {\n inherit: boolean;\n scope: string[];\n}\n\nconst CompositionScopeContext = React.createContext<CompositionScopeContext | undefined>(undefined);\n\ninterface CompositionScopeProps {\n name: string;\n /**\n * Use this prop on components that are used to register decorators.\n * Components are inherited at the time of registration, and then cached.\n */\n inherit?: boolean;\n children: React.ReactNode;\n}\n\nexport const CompositionScope = ({ name, inherit = false, children }: CompositionScopeProps) => {\n const parentScope = useCompositionScope();\n\n return (\n <CompositionScopeContext.Provider value={{ scope: [...parentScope.scope, name], inherit }}>\n {children}\n </CompositionScopeContext.Provider>\n );\n};\n\nexport function useCompositionScope() {\n const context = React.useContext(CompositionScopeContext);\n\n if (!context) {\n return { scope: [], inherit: false };\n }\n\n return context;\n}\n"],"names":["CompositionScopeContext","React","undefined","CompositionScope","name","inherit","children","parentScope","useCompositionScope","context"],"mappings":";AAOA,MAAMA,0BAA0B,WAAHA,GAAGC,MAAAA,aAAmB,CAAsCC;AAYlF,MAAMC,mBAAmB,CAAC,EAAEC,IAAI,EAAEC,UAAU,KAAK,EAAEC,QAAQ,EAAyB;IACvF,MAAMC,cAAcC;IAEpB,OAAO,WAAP,GACI,oBAACR,wBAAwB,QAAQ;QAAC,OAAO;YAAE,OAAO;mBAAIO,YAAY,KAAK;gBAAEH;aAAK;YAAEC;QAAQ;OACnFC;AAGb;AAEO,SAASE;IACZ,MAAMC,UAAUR,MAAAA,UAAgB,CAACD;IAEjC,IAAI,CAACS,SACD,OAAO;QAAE,OAAO,EAAE;QAAE,SAAS;IAAM;IAGvC,OAAOA;AACX"}
|
package/Context.d.ts
CHANGED
|
@@ -1,38 +1,35 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
*/
|
|
7
|
-
component: ComponentType<unknown>;
|
|
8
|
-
/**
|
|
9
|
-
* HOCs used to compose the original component.
|
|
10
|
-
*/
|
|
11
|
-
hocs: HigherOrderComponent[];
|
|
12
|
-
}
|
|
1
|
+
import type { ComponentType } from "react";
|
|
2
|
+
import React from "react";
|
|
3
|
+
import { CompositionStore } from "./domain/CompositionStore.js";
|
|
4
|
+
import type { ComposeWith, Decoratable, DecoratableComponent, DecoratableHook, Decorator, Enumerable, GenericComponent, GenericHook } from "./types.js";
|
|
5
|
+
export declare function compose<T>(...fns: Decorator<T>[]): (decoratee: T) => T;
|
|
13
6
|
/**
|
|
14
|
-
*
|
|
15
|
-
* You can pass any HigherOrderComponent as a prop, regardless of its TInputProps type. The only way to allow that is
|
|
16
|
-
* to let it be `any` in this interface.
|
|
7
|
+
* @deprecated Use `Decorator` instead.
|
|
17
8
|
*/
|
|
18
|
-
export interface HigherOrderComponent<
|
|
19
|
-
(Component:
|
|
9
|
+
export interface HigherOrderComponent<TProps = any, TOutput = TProps> {
|
|
10
|
+
(Component: GenericComponent<TProps>): GenericComponent<TOutput>;
|
|
11
|
+
}
|
|
12
|
+
export type DecoratableTypes = DecoratableComponent | DecoratableHook;
|
|
13
|
+
export type DecoratorsTuple = [Decoratable, Decorator<any>[]];
|
|
14
|
+
export type DecoratorsCollection = Array<DecoratorsTuple>;
|
|
15
|
+
interface CompositionProviderProps {
|
|
16
|
+
decorators?: DecoratorsCollection;
|
|
17
|
+
children: React.ReactNode;
|
|
20
18
|
}
|
|
21
|
-
declare
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
19
|
+
export declare const CompositionProvider: ({ decorators, children }: CompositionProviderProps) => React.JSX.Element;
|
|
20
|
+
export declare function useCompositionStore(): CompositionStore;
|
|
21
|
+
export declare function useOptionalCompositionStore(): CompositionStore | undefined;
|
|
22
|
+
export declare function useComponent<T>(baseFunction: T): T;
|
|
23
|
+
interface CompositionContextValue {
|
|
24
|
+
composeComponent(component: ComponentType<unknown>, hocs: Enumerable<ComposeWith>, scope?: string, inherit?: boolean): () => void;
|
|
25
|
+
getComponent(component: ComponentType<unknown>, scope: string[]): GenericComponent | GenericHook | undefined;
|
|
26
26
|
}
|
|
27
|
-
declare const CompositionContext: React.Context<CompositionContext | undefined>;
|
|
28
|
-
export declare const CompositionProvider: React.FC;
|
|
29
|
-
export declare function useComponent(Component: ComponentType<any>): React.ComponentType<any>;
|
|
30
27
|
/**
|
|
31
28
|
* This hook will throw an error if composition context doesn't exist.
|
|
32
29
|
*/
|
|
33
|
-
export declare function useComposition():
|
|
30
|
+
export declare function useComposition(): CompositionContextValue;
|
|
34
31
|
/**
|
|
35
32
|
* This hook will not throw an error if composition context doesn't exist.
|
|
36
33
|
*/
|
|
37
|
-
export declare function useOptionalComposition():
|
|
34
|
+
export declare function useOptionalComposition(): CompositionContextValue | undefined;
|
|
38
35
|
export {};
|
package/Context.js
CHANGED
|
@@ -1,108 +1,54 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
value: true
|
|
7
|
-
});
|
|
8
|
-
exports.CompositionProvider = void 0;
|
|
9
|
-
exports.compose = compose;
|
|
10
|
-
exports.useComponent = useComponent;
|
|
11
|
-
exports.useComposition = useComposition;
|
|
12
|
-
exports.useOptionalComposition = useOptionalComposition;
|
|
13
|
-
var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
|
|
14
|
-
var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
|
|
15
|
-
var _react = _interopRequireWildcard(require("react"));
|
|
16
|
-
function compose() {
|
|
17
|
-
for (var _len = arguments.length, fns = new Array(_len), _key = 0; _key < _len; _key++) {
|
|
18
|
-
fns[_key] = arguments[_key];
|
|
19
|
-
}
|
|
20
|
-
return function ComposedComponent(Base) {
|
|
21
|
-
return fns.reduceRight(function (Component, hoc) {
|
|
22
|
-
return hoc(Component);
|
|
23
|
-
}, Base);
|
|
24
|
-
};
|
|
1
|
+
import react, { createContext, useContext, useRef, useSyncExternalStore } from "react";
|
|
2
|
+
import { useCompositionScope } from "./CompositionScope.js";
|
|
3
|
+
import { CompositionStore } from "./domain/CompositionStore.js";
|
|
4
|
+
function compose(...fns) {
|
|
5
|
+
return (decoratee)=>fns.reduceRight((decoratee, decorator)=>decorator(decoratee), decoratee);
|
|
25
6
|
}
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
component: null,
|
|
38
|
-
hocs: []
|
|
39
|
-
};
|
|
40
|
-
var newHocs = [].concat((0, _toConsumableArray2.default)(recipe.hocs || []), (0, _toConsumableArray2.default)(hocs));
|
|
41
|
-
components.set(component, {
|
|
42
|
-
component: compose.apply(void 0, (0, _toConsumableArray2.default)((0, _toConsumableArray2.default)(newHocs).reverse()))(component),
|
|
43
|
-
hocs: newHocs
|
|
44
|
-
});
|
|
45
|
-
return components;
|
|
46
|
-
});
|
|
47
|
-
|
|
48
|
-
// Return a function that will remove the added HOCs.
|
|
49
|
-
return function () {
|
|
50
|
-
setComponents(function (prevComponents) {
|
|
51
|
-
var components = new Map(prevComponents);
|
|
52
|
-
var recipe = components.get(component) || {
|
|
53
|
-
component: null,
|
|
54
|
-
hocs: []
|
|
55
|
-
};
|
|
56
|
-
var newHOCs = (0, _toConsumableArray2.default)(recipe.hocs).filter(function (hoc) {
|
|
57
|
-
return !hocs.includes(hoc);
|
|
58
|
-
});
|
|
59
|
-
var NewComponent = compose.apply(void 0, (0, _toConsumableArray2.default)((0, _toConsumableArray2.default)(newHOCs).reverse()))(component);
|
|
60
|
-
components.set(component, {
|
|
61
|
-
component: NewComponent,
|
|
62
|
-
hocs: newHOCs
|
|
63
|
-
});
|
|
64
|
-
return components;
|
|
65
|
-
});
|
|
66
|
-
};
|
|
67
|
-
}, [setComponents]);
|
|
68
|
-
var getComponent = (0, _react.useCallback)(function (Component) {
|
|
69
|
-
var composedComponent = components.get(Component);
|
|
70
|
-
return composedComponent ? composedComponent.component : undefined;
|
|
71
|
-
}, [components]);
|
|
72
|
-
var context = (0, _react.useMemo)(function () {
|
|
73
|
-
return {
|
|
74
|
-
getComponent: getComponent,
|
|
75
|
-
composeComponent: composeComponent,
|
|
76
|
-
components: components
|
|
77
|
-
};
|
|
78
|
-
}, [components, composeComponent]);
|
|
79
|
-
return /*#__PURE__*/_react.default.createElement(CompositionContext.Provider, {
|
|
80
|
-
value: context
|
|
81
|
-
}, children);
|
|
7
|
+
const CompositionStoreContext = /*#__PURE__*/ createContext(void 0);
|
|
8
|
+
const CompositionProvider = ({ decorators = [], children })=>{
|
|
9
|
+
const storeRef = useRef(null);
|
|
10
|
+
if (null === storeRef.current) {
|
|
11
|
+
const store = new CompositionStore();
|
|
12
|
+
for (const [decoratable, hocs] of decorators)store.register(decoratable.original, hocs);
|
|
13
|
+
storeRef.current = store;
|
|
14
|
+
}
|
|
15
|
+
return /*#__PURE__*/ react.createElement(CompositionStoreContext.Provider, {
|
|
16
|
+
value: storeRef.current
|
|
17
|
+
}, children);
|
|
82
18
|
};
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
return Component;
|
|
88
|
-
}
|
|
89
|
-
return context.getComponent(Component) || Component;
|
|
19
|
+
function useCompositionStore() {
|
|
20
|
+
const store = useContext(CompositionStoreContext);
|
|
21
|
+
if (!store) throw new Error("You're missing a <CompositionProvider> higher up in your component hierarchy!");
|
|
22
|
+
return store;
|
|
90
23
|
}
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
24
|
+
function useOptionalCompositionStore() {
|
|
25
|
+
return useContext(CompositionStoreContext);
|
|
26
|
+
}
|
|
27
|
+
function useComponent(baseFunction) {
|
|
28
|
+
const store = useOptionalCompositionStore();
|
|
29
|
+
const scope = useCompositionScope();
|
|
30
|
+
useSyncExternalStore(store ? store.subscribe : noopSubscribe, store ? store.getSnapshot : noopGetSnapshot);
|
|
31
|
+
if (!store) return baseFunction;
|
|
32
|
+
const result = store.getComponent(baseFunction, scope.scope) || baseFunction;
|
|
33
|
+
return result;
|
|
34
|
+
}
|
|
35
|
+
const noopSubscribe = ()=>()=>{};
|
|
36
|
+
const noopGetSnapshot = ()=>0;
|
|
95
37
|
function useComposition() {
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
38
|
+
const store = useCompositionStore();
|
|
39
|
+
return {
|
|
40
|
+
composeComponent: (component, hocs, scope = "*", inherit = false)=>store.register(component, hocs, scope, inherit),
|
|
41
|
+
getComponent: (component, scope)=>store.getComponent(component, scope)
|
|
42
|
+
};
|
|
101
43
|
}
|
|
102
|
-
|
|
103
|
-
/**
|
|
104
|
-
* This hook will not throw an error if composition context doesn't exist.
|
|
105
|
-
*/
|
|
106
44
|
function useOptionalComposition() {
|
|
107
|
-
|
|
108
|
-
|
|
45
|
+
const store = useOptionalCompositionStore();
|
|
46
|
+
if (!store) return;
|
|
47
|
+
return {
|
|
48
|
+
composeComponent: (component, hocs, scope = "*", inherit = false)=>store.register(component, hocs, scope, inherit),
|
|
49
|
+
getComponent: (component, scope)=>store.getComponent(component, scope)
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
export { CompositionProvider, compose, useComponent, useComposition, useCompositionStore, useOptionalComposition, useOptionalCompositionStore };
|
|
53
|
+
|
|
54
|
+
//# sourceMappingURL=Context.js.map
|
package/Context.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"
|
|
1
|
+
{"version":3,"file":"Context.js","sources":["../src/Context.tsx"],"sourcesContent":["import type { ComponentType } from \"react\";\nimport React, { createContext, useContext, useRef, useSyncExternalStore } from \"react\";\nimport { useCompositionScope } from \"~/CompositionScope.js\";\nimport { CompositionStore } from \"~/domain/CompositionStore.js\";\n\nimport type {\n ComposeWith,\n Decoratable,\n DecoratableComponent,\n DecoratableHook,\n Decorator,\n Enumerable,\n GenericComponent,\n GenericHook\n} from \"~/types.js\";\n\nexport function compose<T>(...fns: Decorator<T>[]) {\n return (decoratee: T): T => {\n return fns.reduceRight((decoratee, decorator) => decorator(decoratee), decoratee) as T;\n };\n}\n\n/**\n * @deprecated Use `Decorator` instead.\n */\nexport interface HigherOrderComponent<TProps = any, TOutput = TProps> {\n (Component: GenericComponent<TProps>): GenericComponent<TOutput>;\n}\n\nexport type DecoratableTypes = DecoratableComponent | DecoratableHook;\n\nconst CompositionStoreContext = createContext<CompositionStore | undefined>(undefined);\n\nexport type DecoratorsTuple = [Decoratable, Decorator<any>[]];\nexport type DecoratorsCollection = Array<DecoratorsTuple>;\n\ninterface CompositionProviderProps {\n decorators?: DecoratorsCollection;\n children: React.ReactNode;\n}\n\nexport const CompositionProvider = ({ decorators = [], children }: CompositionProviderProps) => {\n const storeRef = useRef<CompositionStore | null>(null);\n if (storeRef.current === null) {\n const store = new CompositionStore();\n // Pre-register decorators from props.\n for (const [decoratable, hocs] of decorators) {\n store.register(decoratable.original as ComponentType<unknown>, hocs);\n }\n storeRef.current = store;\n }\n\n return (\n <CompositionStoreContext.Provider value={storeRef.current}>\n {children}\n </CompositionStoreContext.Provider>\n );\n};\n\nexport function useCompositionStore(): CompositionStore {\n const store = useContext(CompositionStoreContext);\n if (!store) {\n throw new Error(\n `You're missing a <CompositionProvider> higher up in your component hierarchy!`\n );\n }\n return store;\n}\n\nexport function useOptionalCompositionStore(): CompositionStore | undefined {\n return useContext(CompositionStoreContext);\n}\n\nexport function useComponent<T>(baseFunction: T) {\n const store = useOptionalCompositionStore();\n const scope = useCompositionScope();\n\n // Subscribe to store changes so we re-render when compositions change.\n useSyncExternalStore(\n store ? store.subscribe : noopSubscribe,\n store ? store.getSnapshot : noopGetSnapshot\n );\n\n if (!store) {\n return baseFunction;\n }\n\n const result = store.getComponent(baseFunction as any, scope.scope) || baseFunction;\n\n return result as T;\n}\n\nconst noopSubscribe = () => () => {};\nconst noopGetSnapshot = () => 0;\n\n// Legacy compatibility — kept for any external consumers.\n\ninterface CompositionContextValue {\n composeComponent(\n component: ComponentType<unknown>,\n hocs: Enumerable<ComposeWith>,\n scope?: string,\n inherit?: boolean\n ): () => void;\n getComponent(\n component: ComponentType<unknown>,\n scope: string[]\n ): GenericComponent | GenericHook | undefined;\n}\n\n/**\n * This hook will throw an error if composition context doesn't exist.\n */\nexport function useComposition(): CompositionContextValue {\n const store = useCompositionStore();\n\n return {\n composeComponent: (component, hocs, scope = \"*\", inherit = false) => {\n return store.register(component, hocs as any[], scope, inherit);\n },\n getComponent: (component, scope) => {\n return store.getComponent(component, scope);\n }\n };\n}\n\n/**\n * This hook will not throw an error if composition context doesn't exist.\n */\nexport function useOptionalComposition(): CompositionContextValue | undefined {\n const store = useOptionalCompositionStore();\n if (!store) {\n return undefined;\n }\n\n return {\n composeComponent: (component, hocs, scope = \"*\", inherit = false) => {\n return store.register(component, hocs as any[], scope, inherit);\n },\n getComponent: (component, scope) => {\n return store.getComponent(component, scope);\n }\n };\n}\n"],"names":["compose","fns","decoratee","decorator","CompositionStoreContext","createContext","undefined","CompositionProvider","decorators","children","storeRef","useRef","store","CompositionStore","decoratable","hocs","useCompositionStore","useContext","Error","useOptionalCompositionStore","useComponent","baseFunction","scope","useCompositionScope","useSyncExternalStore","noopSubscribe","noopGetSnapshot","result","useComposition","component","inherit","useOptionalComposition"],"mappings":";;;AAgBO,SAASA,QAAW,GAAGC,GAAmB;IAC7C,OAAO,CAACC,YACGD,IAAI,WAAW,CAAC,CAACC,WAAWC,YAAcA,UAAUD,YAAYA;AAE/E;AAWA,MAAME,0BAA0B,WAAHA,GAAGC,cAA4CC;AAUrE,MAAMC,sBAAsB,CAAC,EAAEC,aAAa,EAAE,EAAEC,QAAQ,EAA4B;IACvF,MAAMC,WAAWC,OAAgC;IACjD,IAAID,AAAqB,SAArBA,SAAS,OAAO,EAAW;QAC3B,MAAME,QAAQ,IAAIC;QAElB,KAAK,MAAM,CAACC,aAAaC,KAAK,IAAIP,WAC9BI,MAAM,QAAQ,CAACE,YAAY,QAAQ,EAA4BC;QAEnEL,SAAS,OAAO,GAAGE;IACvB;IAEA,OAAO,WAAP,GACI,oBAACR,wBAAwB,QAAQ;QAAC,OAAOM,SAAS,OAAO;OACpDD;AAGb;AAEO,SAASO;IACZ,MAAMJ,QAAQK,WAAWb;IACzB,IAAI,CAACQ,OACD,MAAM,IAAIM,MACN;IAGR,OAAON;AACX;AAEO,SAASO;IACZ,OAAOF,WAAWb;AACtB;AAEO,SAASgB,aAAgBC,YAAe;IAC3C,MAAMT,QAAQO;IACd,MAAMG,QAAQC;IAGdC,qBACIZ,QAAQA,MAAM,SAAS,GAAGa,eAC1Bb,QAAQA,MAAM,WAAW,GAAGc;IAGhC,IAAI,CAACd,OACD,OAAOS;IAGX,MAAMM,SAASf,MAAM,YAAY,CAACS,cAAqBC,MAAM,KAAK,KAAKD;IAEvE,OAAOM;AACX;AAEA,MAAMF,gBAAgB,IAAM,KAAO;AACnC,MAAMC,kBAAkB,IAAM;AAoBvB,SAASE;IACZ,MAAMhB,QAAQI;IAEd,OAAO;QACH,kBAAkB,CAACa,WAAWd,MAAMO,QAAQ,GAAG,EAAEQ,UAAU,KAAK,GACrDlB,MAAM,QAAQ,CAACiB,WAAWd,MAAeO,OAAOQ;QAE3D,cAAc,CAACD,WAAWP,QACfV,MAAM,YAAY,CAACiB,WAAWP;IAE7C;AACJ;AAKO,SAASS;IACZ,MAAMnB,QAAQO;IACd,IAAI,CAACP,OACD;IAGJ,OAAO;QACH,kBAAkB,CAACiB,WAAWd,MAAMO,QAAQ,GAAG,EAAEQ,UAAU,KAAK,GACrDlB,MAAM,QAAQ,CAACiB,WAAWd,MAAeO,OAAOQ;QAE3D,cAAc,CAACD,WAAWP,QACfV,MAAM,YAAY,CAACiB,WAAWP;IAE7C;AACJ"}
|
package/README.md
CHANGED
|
@@ -1,8 +1,11 @@
|
|
|
1
|
-
#
|
|
2
|
-
[](https://www.npmjs.com/package/@webiny/react-composition)
|
|
3
|
-
[](https://www.npmjs.com/package/@webiny/react-composition)
|
|
4
|
-
[](https://github.com/prettier/prettier)
|
|
5
|
-
[](http://makeapullrequest.com)
|
|
1
|
+
# @webiny/react-composition
|
|
6
2
|
|
|
7
|
-
|
|
3
|
+
> [!NOTE]
|
|
4
|
+
> This package is part of the [Webiny](https://www.webiny.com) monorepo.
|
|
5
|
+
> It’s **included in every Webiny project by default** and is not meant to be used as a standalone package.
|
|
8
6
|
|
|
7
|
+
📘 **Documentation:** [https://www.webiny.com/docs](https://www.webiny.com/docs)
|
|
8
|
+
|
|
9
|
+
---
|
|
10
|
+
|
|
11
|
+
_This README file is automatically generated during the publish process._
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import React from "react";
|
|
2
|
+
import type { CanReturnNullOrElement, Decoratable, DecoratableComponent, DecoratableHook, Decorator } from "./types.js";
|
|
3
|
+
type GetBaseFunction<T> = T extends DecoratableComponent<infer F> ? F : never;
|
|
4
|
+
/**
|
|
5
|
+
* Creates a component which, when mounted, registers a Higher Order Component for the given base component.
|
|
6
|
+
* This is particularly useful for decorating (wrapping) existing composable components.
|
|
7
|
+
* For more information, visit https://www.webiny.com/docs/admin-area/basics/framework.
|
|
8
|
+
*/
|
|
9
|
+
export declare function createComponentPlugin<T extends Decoratable>(Base: T, hoc: T extends DecoratableComponent ? Decorator<CanReturnNullOrElement<GetBaseFunction<T>>> : Decorator<GetBaseFunction<T>>): {
|
|
10
|
+
(): React.JSX.Element;
|
|
11
|
+
displayName: string;
|
|
12
|
+
};
|
|
13
|
+
export type GetDecorateeParams<T> = T extends (params?: infer P1) => any ? P1 : T extends (params: infer P2) => any ? P2 : any;
|
|
14
|
+
export type GetDecoratee<T> = T extends DecoratableHook<infer F> ? F : T extends DecoratableComponent<infer F> ? F : never;
|
|
15
|
+
export declare function createDecorator<T extends Decoratable>(Base: T, hoc: T extends DecoratableComponent ? Decorator<CanReturnNullOrElement<GetBaseFunction<T>>> : Decorator<GetBaseFunction<T>>): {
|
|
16
|
+
(): React.JSX.Element;
|
|
17
|
+
displayName: string;
|
|
18
|
+
};
|
|
19
|
+
export {};
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import react from "react";
|
|
2
|
+
import { Compose } from "./Compose.js";
|
|
3
|
+
function createComponentPlugin(Base, hoc) {
|
|
4
|
+
return createDecorator(Base, hoc);
|
|
5
|
+
}
|
|
6
|
+
const isDecoratableComponent = (decoratable)=>"displayName" in decoratable;
|
|
7
|
+
function createDecorator(Base, hoc) {
|
|
8
|
+
const DecoratorPlugin = ()=>/*#__PURE__*/ react.createElement(Compose, {
|
|
9
|
+
component: Base,
|
|
10
|
+
with: hoc
|
|
11
|
+
});
|
|
12
|
+
if (isDecoratableComponent(Base)) DecoratorPlugin.displayName = Base.displayName;
|
|
13
|
+
return DecoratorPlugin;
|
|
14
|
+
}
|
|
15
|
+
export { createComponentPlugin, createDecorator };
|
|
16
|
+
|
|
17
|
+
//# sourceMappingURL=createDecorator.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"createDecorator.js","sources":["../src/createDecorator.tsx"],"sourcesContent":["import React from \"react\";\nimport type {\n CanReturnNullOrElement,\n Decoratable,\n DecoratableComponent,\n DecoratableHook,\n Decorator\n} from \"~/types.js\";\nimport { Compose } from \"~/Compose.js\";\n\ntype GetBaseFunction<T> = T extends DecoratableComponent<infer F> ? F : never;\n\n/**\n * Creates a component which, when mounted, registers a Higher Order Component for the given base component.\n * This is particularly useful for decorating (wrapping) existing composable components.\n * For more information, visit https://www.webiny.com/docs/admin-area/basics/framework.\n */\nexport function createComponentPlugin<T extends Decoratable>(\n Base: T,\n hoc: T extends DecoratableComponent\n ? Decorator<CanReturnNullOrElement<GetBaseFunction<T>>>\n : Decorator<GetBaseFunction<T>>\n) {\n return createDecorator(Base, hoc);\n}\n\n// Maybe there's a better way to mark params as non-existent, but for now I left it as `any`.\n// TODO: revisit this type; not sure if `?` can be handled in one clause\nexport type GetDecorateeParams<T> = T extends (params?: infer P1) => any\n ? P1\n : T extends (params: infer P2) => any\n ? P2\n : any;\n\nexport type GetDecoratee<T> =\n T extends DecoratableHook<infer F> ? F : T extends DecoratableComponent<infer F> ? F : never;\n\nconst isDecoratableComponent = (\n decoratable: DecoratableComponent | DecoratableHook\n): decoratable is DecoratableComponent => {\n return \"displayName\" in decoratable;\n};\n\nexport function createDecorator<T extends Decoratable>(\n Base: T,\n hoc: T extends DecoratableComponent\n ? Decorator<CanReturnNullOrElement<GetBaseFunction<T>>>\n : Decorator<GetBaseFunction<T>>\n) {\n const DecoratorPlugin = () => <Compose component={Base} with={hoc as any} />;\n if (isDecoratableComponent(Base)) {\n DecoratorPlugin.displayName = Base.displayName;\n }\n return DecoratorPlugin;\n}\n"],"names":["createComponentPlugin","Base","hoc","createDecorator","isDecoratableComponent","decoratable","DecoratorPlugin","Compose"],"mappings":";;AAiBO,SAASA,sBACZC,IAAO,EACPC,GAEmC;IAEnC,OAAOC,gBAAgBF,MAAMC;AACjC;AAaA,MAAME,yBAAyB,CAC3BC,cAEO,iBAAiBA;AAGrB,SAASF,gBACZF,IAAO,EACPC,GAEmC;IAEnC,MAAMI,kBAAkB,kBAAM,oBAACC,SAAOA;YAAC,WAAWN;YAAM,MAAMC;;IAC9D,IAAIE,uBAAuBH,OACvBK,gBAAgB,WAAW,GAAGL,KAAK,WAAW;IAElD,OAAOK;AACX"}
|
package/decorators.d.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import React from "react";
|
|
2
|
+
import type { GetDecoratee, GetDecorateeParams } from "./createDecorator.js";
|
|
3
|
+
import type { DecoratableComponent, GenericComponent, Decorator, GenericHook, DecoratableHook, ComponentDecorator } from "./types.js";
|
|
4
|
+
export interface ShouldDecorate<TDecorator = any, TComponent = any> {
|
|
5
|
+
(decoratorProps: TDecorator, componentProps: TComponent): boolean;
|
|
6
|
+
}
|
|
7
|
+
export declare function createConditionalDecorator<TDecoratee extends GenericComponent>(shouldDecorate: ShouldDecorate, decorator: Decorator<TDecoratee>, decoratorProps: unknown): Decorator<TDecoratee>;
|
|
8
|
+
export declare function createDecoratorFactory<TDecorator>(): <TDecoratable extends DecoratableComponent>(decoratable: TDecoratable, shouldDecorate?: ShouldDecorate<TDecorator, GetDecorateeParams<GetDecoratee<TDecoratable>>>) => (decorator: ComponentDecorator<GetDecoratee<TDecoratable>>) => (props: TDecorator) => React.JSX.Element;
|
|
9
|
+
export declare function createHookDecoratorFactory(): <TDecoratable extends DecoratableHook>(decoratable: TDecoratable) => (decorator: Decorator<GetDecoratee<TDecoratable>>) => () => React.JSX.Element;
|
|
10
|
+
export declare function withDecoratorFactory<TDecorator>(): <TDecoratable extends DecoratableComponent>(Component: TDecoratable, shouldDecorate?: ShouldDecorate<TDecorator, GetDecorateeParams<GetDecoratee<TDecoratable>>>) => TDecoratable & {
|
|
11
|
+
createDecorator: (decorator: ComponentDecorator<GetDecoratee<TDecoratable>>) => (props: TDecorator) => React.JSX.Element;
|
|
12
|
+
};
|
|
13
|
+
export declare function withHookDecoratorFactory(): <TDecoratable extends DecoratableHook>(hook: TDecoratable) => DecoratableHook<GenericHook<GetDecorateeParams<GetDecoratee<TDecoratable>>, ReturnType<GetDecoratee<TDecoratable>>>> & {
|
|
14
|
+
createDecorator: (decorator: Decorator<GetDecoratee<TDecoratable>>) => () => React.JSX.Element;
|
|
15
|
+
};
|
package/decorators.js
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import react from "react";
|
|
2
|
+
import { Compose } from "./Compose.js";
|
|
3
|
+
function createConditionalDecorator(shouldDecorate, decorator, decoratorProps) {
|
|
4
|
+
return (Original)=>{
|
|
5
|
+
const DecoratedComponent = /*#__PURE__*/ react.memo(decorator(Original));
|
|
6
|
+
DecoratedComponent.displayName = Original.displayName;
|
|
7
|
+
return function(props) {
|
|
8
|
+
if (shouldDecorate(decoratorProps, props)) return /*#__PURE__*/ react.createElement(DecoratedComponent, props);
|
|
9
|
+
return /*#__PURE__*/ react.createElement(Original, props);
|
|
10
|
+
};
|
|
11
|
+
};
|
|
12
|
+
}
|
|
13
|
+
const memoizedComponent = (decorator)=>(decoratee)=>/*#__PURE__*/ react.memo(decorator(decoratee));
|
|
14
|
+
function createDecoratorFactory() {
|
|
15
|
+
return function(decoratable, shouldDecorate) {
|
|
16
|
+
return function(decorator) {
|
|
17
|
+
return function(props) {
|
|
18
|
+
if (shouldDecorate) {
|
|
19
|
+
const componentDecorator = createConditionalDecorator(shouldDecorate, decorator, props);
|
|
20
|
+
return /*#__PURE__*/ react.createElement(Compose, {
|
|
21
|
+
function: decoratable,
|
|
22
|
+
with: componentDecorator
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
return /*#__PURE__*/ react.createElement(Compose, {
|
|
26
|
+
function: decoratable,
|
|
27
|
+
with: memoizedComponent(decorator)
|
|
28
|
+
});
|
|
29
|
+
};
|
|
30
|
+
};
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
function createHookDecoratorFactory() {
|
|
34
|
+
return function(decoratable) {
|
|
35
|
+
return function(decorator) {
|
|
36
|
+
return function() {
|
|
37
|
+
return /*#__PURE__*/ react.createElement(Compose, {
|
|
38
|
+
function: decoratable,
|
|
39
|
+
with: decorator
|
|
40
|
+
});
|
|
41
|
+
};
|
|
42
|
+
};
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
function withDecoratorFactory() {
|
|
46
|
+
return function(Component, shouldDecorate) {
|
|
47
|
+
const createDecorator = createDecoratorFactory()(Component, shouldDecorate);
|
|
48
|
+
return Object.assign(Component, {
|
|
49
|
+
createDecorator
|
|
50
|
+
});
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
function withHookDecoratorFactory() {
|
|
54
|
+
return function(hook) {
|
|
55
|
+
const createDecorator = createHookDecoratorFactory()(hook);
|
|
56
|
+
return Object.assign(hook, {
|
|
57
|
+
createDecorator
|
|
58
|
+
});
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
export { createConditionalDecorator, createDecoratorFactory, createHookDecoratorFactory, withDecoratorFactory, withHookDecoratorFactory };
|
|
62
|
+
|
|
63
|
+
//# sourceMappingURL=decorators.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"decorators.js","sources":["../src/decorators.tsx"],"sourcesContent":["import React from \"react\";\nimport { Compose } from \"~/Compose.js\";\nimport type { GetDecoratee, GetDecorateeParams } from \"~/createDecorator.js\";\nimport type {\n DecoratableComponent,\n GenericComponent,\n Decorator,\n GenericHook,\n DecoratableHook,\n ComponentDecorator\n} from \"~/types.js\";\n\nexport interface ShouldDecorate<TDecorator = any, TComponent = any> {\n (decoratorProps: TDecorator, componentProps: TComponent): boolean;\n}\n\nexport function createConditionalDecorator<TDecoratee extends GenericComponent>(\n shouldDecorate: ShouldDecorate,\n decorator: Decorator<TDecoratee>,\n decoratorProps: unknown\n): Decorator<TDecoratee> {\n return (Original => {\n const DecoratedComponent = React.memo(decorator(Original));\n DecoratedComponent.displayName = Original.displayName;\n\n return function ShouldDecorate(props: unknown) {\n if (shouldDecorate(decoratorProps, props)) {\n // @ts-expect-error\n return <DecoratedComponent {...props} />;\n }\n\n // @ts-expect-error\n return <Original {...props} />;\n };\n }) as Decorator<TDecoratee>;\n}\n\nconst memoizedComponent = <T extends GenericComponent>(decorator: Decorator<T>) => {\n return (decoratee: T) => {\n return React.memo(decorator(decoratee));\n };\n};\n\nexport function createDecoratorFactory<TDecorator>() {\n return function from<TDecoratable extends DecoratableComponent>(\n decoratable: TDecoratable,\n shouldDecorate?: ShouldDecorate<TDecorator, GetDecorateeParams<GetDecoratee<TDecoratable>>>\n ) {\n return function createDecorator(decorator: ComponentDecorator<GetDecoratee<TDecoratable>>) {\n return function DecoratorPlugin(props: TDecorator) {\n if (shouldDecorate) {\n const componentDecorator = createConditionalDecorator<GenericComponent>(\n shouldDecorate,\n decorator as unknown as Decorator<GenericComponent>,\n props\n );\n\n return <Compose function={decoratable} with={componentDecorator} />;\n }\n\n return (\n <Compose\n function={decoratable}\n with={memoizedComponent(\n decorator as unknown as Decorator<GenericComponent>\n )}\n />\n );\n };\n };\n };\n}\n\nexport function createHookDecoratorFactory() {\n return function from<TDecoratable extends DecoratableHook>(decoratable: TDecoratable) {\n return function createDecorator(decorator: Decorator<GetDecoratee<TDecoratable>>) {\n return function DecoratorPlugin() {\n return (\n <Compose\n function={decoratable}\n with={decorator as unknown as Decorator<GenericHook>}\n />\n );\n };\n };\n };\n}\n\nexport function withDecoratorFactory<TDecorator>() {\n return function WithDecorator<TDecoratable extends DecoratableComponent>(\n Component: TDecoratable,\n shouldDecorate?: ShouldDecorate<TDecorator, GetDecorateeParams<GetDecoratee<TDecoratable>>>\n ) {\n const createDecorator = createDecoratorFactory<TDecorator>()(Component, shouldDecorate);\n\n return Object.assign(Component, { createDecorator }) as TDecoratable & {\n createDecorator: typeof createDecorator;\n };\n };\n}\n\nexport function withHookDecoratorFactory() {\n return function WithHookDecorator<TDecoratable extends DecoratableHook>(hook: TDecoratable) {\n const createDecorator = createHookDecoratorFactory()(hook);\n\n return Object.assign(hook, { createDecorator }) as unknown as DecoratableHook<\n GenericHook<\n GetDecorateeParams<GetDecoratee<TDecoratable>>,\n ReturnType<GetDecoratee<TDecoratable>>\n >\n > & { createDecorator: typeof createDecorator };\n };\n}\n"],"names":["createConditionalDecorator","shouldDecorate","decorator","decoratorProps","Original","DecoratedComponent","React","props","memoizedComponent","decoratee","createDecoratorFactory","decoratable","componentDecorator","Compose","createHookDecoratorFactory","withDecoratorFactory","Component","createDecorator","Object","withHookDecoratorFactory","hook"],"mappings":";;AAgBO,SAASA,2BACZC,cAA8B,EAC9BC,SAAgC,EAChCC,cAAuB;IAEvB,OAAQC,CAAAA;QACJ,MAAMC,qBAAqB,WAArBA,GAAqBC,MAAAA,IAAU,CAACJ,UAAUE;QAChDC,mBAAmB,WAAW,GAAGD,SAAS,WAAW;QAErD,OAAO,SAAwBG,KAAc;YACzC,IAAIN,eAAeE,gBAAgBI,QAE/B,OAAO,WAAP,GAAO,oBAACF,oBAAuBE;YAInC,OAAO,WAAP,GAAO,oBAACH,UAAaG;QACzB;IACJ;AACJ;AAEA,MAAMC,oBAAoB,CAA6BN,YAC5C,CAACO,YACG,WAAP,GAAOH,MAAAA,IAAU,CAACJ,UAAUO;AAI7B,SAASC;IACZ,OAAO,SACHC,WAAyB,EACzBV,cAA2F;QAE3F,OAAO,SAAyBC,SAAyD;YACrF,OAAO,SAAyBK,KAAiB;gBAC7C,IAAIN,gBAAgB;oBAChB,MAAMW,qBAAqBZ,2BACvBC,gBACAC,WACAK;oBAGJ,OAAO,WAAP,GAAO,oBAACM,SAAOA;wBAAC,UAAUF;wBAAa,MAAMC;;gBACjD;gBAEA,OAAO,WAAP,GACI,oBAACC,SAAOA;oBACJ,UAAUF;oBACV,MAAMH,kBACFN;;YAIhB;QACJ;IACJ;AACJ;AAEO,SAASY;IACZ,OAAO,SAAoDH,WAAyB;QAChF,OAAO,SAAyBT,SAAgD;YAC5E,OAAO;gBACH,OAAO,WAAP,GACI,oBAACW,SAAOA;oBACJ,UAAUF;oBACV,MAAMT;;YAGlB;QACJ;IACJ;AACJ;AAEO,SAASa;IACZ,OAAO,SACHC,SAAuB,EACvBf,cAA2F;QAE3F,MAAMgB,kBAAkBP,yBAAqCM,WAAWf;QAExE,OAAOiB,OAAO,MAAM,CAACF,WAAW;YAAEC;QAAgB;IAGtD;AACJ;AAEO,SAASE;IACZ,OAAO,SAAiEC,IAAkB;QACtF,MAAMH,kBAAkBH,6BAA6BM;QAErD,OAAOF,OAAO,MAAM,CAACE,MAAM;YAAEH;QAAgB;IAMjD;AACJ"}
|