@webiny/react-composition 0.0.0-unstable.615a930a68 → 0.0.0-unstable.61c048f412
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 -13
- package/Compose.js +21 -22
- package/Compose.js.map +1 -1
- package/CompositionScope.d.ts +17 -0
- package/CompositionScope.js +27 -0
- package/CompositionScope.js.map +1 -0
- package/Context.d.ts +33 -19
- package/Context.js +85 -91
- package/Context.js.map +1 -1
- package/README.md +9 -6
- package/createDecorator.d.ts +19 -0
- package/createDecorator.js +29 -0
- package/createDecorator.js.map +1 -0
- package/decorators.d.ts +15 -0
- package/decorators.js +71 -0
- package/decorators.js.map +1 -0
- package/index.d.ts +8 -4
- package/index.js +9 -57
- package/index.js.map +1 -1
- package/makeComposable.d.ts +43 -3
- package/makeComposable.js +10 -68
- package/makeComposable.js.map +1 -1
- package/makeDecoratable.d.ts +31 -0
- package/makeDecoratable.js +53 -0
- package/makeDecoratable.js.map +1 -0
- package/package.json +10 -19
- package/types.d.ts +35 -0
- package/types.js +3 -0
- package/types.js.map +1 -0
- package/createComponentPlugin.d.ts +0 -3
- package/createComponentPlugin.js +0 -24
- package/createComponentPlugin.js.map +0 -1
package/Compose.d.ts
CHANGED
|
@@ -1,15 +1,8 @@
|
|
|
1
|
-
import
|
|
2
|
-
import {
|
|
3
|
-
export interface ComposableFC<TProps = unknown> extends React.FC<TProps> {
|
|
4
|
-
original: React.FC<TProps>;
|
|
5
|
-
originalName: string;
|
|
6
|
-
}
|
|
1
|
+
import type { DecoratableTypes } from "./Context.js";
|
|
2
|
+
import type { ComposeWith } from "./types.js";
|
|
7
3
|
export interface ComposeProps {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
*/
|
|
12
|
-
component: ComposableFC<any>;
|
|
13
|
-
with: HigherOrderComponent | HigherOrderComponent[];
|
|
4
|
+
function?: DecoratableTypes;
|
|
5
|
+
component?: DecoratableTypes;
|
|
6
|
+
with: ComposeWith;
|
|
14
7
|
}
|
|
15
|
-
export declare const Compose:
|
|
8
|
+
export declare const Compose: (props: ComposeProps) => null;
|
package/Compose.js
CHANGED
|
@@ -1,28 +1,27 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
if (typeof
|
|
18
|
-
console.warn(
|
|
1
|
+
import { useEffect } from "react";
|
|
2
|
+
import { useComposition } from "./Context.js";
|
|
3
|
+
import { useCompositionScope } from "./CompositionScope.js";
|
|
4
|
+
export const Compose = props => {
|
|
5
|
+
const {
|
|
6
|
+
composeComponent
|
|
7
|
+
} = useComposition();
|
|
8
|
+
const {
|
|
9
|
+
scope,
|
|
10
|
+
inherit
|
|
11
|
+
} = useCompositionScope();
|
|
12
|
+
const targetFn = props.function ?? props.component;
|
|
13
|
+
useEffect(() => {
|
|
14
|
+
if (!targetFn) {
|
|
15
|
+
console.warn("You must provide a function or a component to compose with!", props);
|
|
16
|
+
}
|
|
17
|
+
if (typeof targetFn.original === "undefined") {
|
|
18
|
+
console.warn(`You must make your function "${targetFn.originalName ?? targetFn.name}" composable, by using the makeDecoratable() function!`);
|
|
19
19
|
return;
|
|
20
20
|
}
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
return composeComponent(props.component.original, hocs);
|
|
21
|
+
const decorators = Array.isArray(props.with) ? props.with : [props.with];
|
|
22
|
+
return composeComponent(targetFn.original, decorators, scope[scope.length - 1], inherit);
|
|
24
23
|
}, [props.with]);
|
|
25
24
|
return null;
|
|
26
25
|
};
|
|
27
26
|
|
|
28
|
-
|
|
27
|
+
//# sourceMappingURL=Compose.js.map
|
package/Compose.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["Compose","props","
|
|
1
|
+
{"version":3,"names":["useEffect","useComposition","useCompositionScope","Compose","props","composeComponent","scope","inherit","targetFn","function","component","console","warn","original","originalName","name","decorators","Array","isArray","with","length"],"sources":["Compose.tsx"],"sourcesContent":["import { useEffect } from \"react\";\nimport type { DecoratableTypes } from \"./Context.js\";\nimport { useComposition } from \"./Context.js\";\nimport { useCompositionScope } from \"~/CompositionScope.js\";\nimport type { ComposeWith, Decoratable, Enumerable } 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 { composeComponent } = useComposition();\n const { scope, inherit } = useCompositionScope();\n\n const targetFn = (props.function ?? props.component) as Decoratable;\n\n useEffect(() => {\n if (!targetFn) {\n console.warn(\"You must provide a function or a component to compose with!\", props);\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\n return;\n }\n\n const decorators = Array.isArray(props.with) ? props.with : [props.with];\n return composeComponent(\n targetFn.original,\n decorators as Enumerable<ComposeWith>,\n scope[scope.length - 1],\n inherit\n );\n }, [props.with]);\n\n return null;\n};\n"],"mappings":"AAAA,SAASA,SAAS,QAAQ,OAAO;AAEjC,SAASC,cAAc;AACvB,SAASC,mBAAmB;AAS5B,OAAO,MAAMC,OAAO,GAAIC,KAAmB,IAAK;EAC5C,MAAM;IAAEC;EAAiB,CAAC,GAAGJ,cAAc,CAAC,CAAC;EAC7C,MAAM;IAAEK,KAAK;IAAEC;EAAQ,CAAC,GAAGL,mBAAmB,CAAC,CAAC;EAEhD,MAAMM,QAAQ,GAAIJ,KAAK,CAACK,QAAQ,IAAIL,KAAK,CAACM,SAAyB;EAEnEV,SAAS,CAAC,MAAM;IACZ,IAAI,CAACQ,QAAQ,EAAE;MACXG,OAAO,CAACC,IAAI,CAAC,6DAA6D,EAAER,KAAK,CAAC;IACtF;IACA,IAAI,OAAOI,QAAQ,CAACK,QAAQ,KAAK,WAAW,EAAE;MAC1CF,OAAO,CAACC,IAAI,CACR,gCACIJ,QAAQ,CAACM,YAAY,IAAIN,QAAQ,CAACO,IAAI,wDAE9C,CAAC;MAED;IACJ;IAEA,MAAMC,UAAU,GAAGC,KAAK,CAACC,OAAO,CAACd,KAAK,CAACe,IAAI,CAAC,GAAGf,KAAK,CAACe,IAAI,GAAG,CAACf,KAAK,CAACe,IAAI,CAAC;IACxE,OAAOd,gBAAgB,CACnBG,QAAQ,CAACK,QAAQ,EACjBG,UAAU,EACVV,KAAK,CAACA,KAAK,CAACc,MAAM,GAAG,CAAC,CAAC,EACvBb,OACJ,CAAC;EACL,CAAC,EAAE,CAACH,KAAK,CAACe,IAAI,CAAC,CAAC;EAEhB,OAAO,IAAI;AACf,CAAC","ignoreList":[]}
|
|
@@ -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,27 @@
|
|
|
1
|
+
import React from "react";
|
|
2
|
+
const CompositionScopeContext = /*#__PURE__*/React.createContext(undefined);
|
|
3
|
+
export const CompositionScope = ({
|
|
4
|
+
name,
|
|
5
|
+
inherit = false,
|
|
6
|
+
children
|
|
7
|
+
}) => {
|
|
8
|
+
const parentScope = useCompositionScope();
|
|
9
|
+
return /*#__PURE__*/React.createElement(CompositionScopeContext.Provider, {
|
|
10
|
+
value: {
|
|
11
|
+
scope: [...parentScope.scope, name],
|
|
12
|
+
inherit
|
|
13
|
+
}
|
|
14
|
+
}, children);
|
|
15
|
+
};
|
|
16
|
+
export function useCompositionScope() {
|
|
17
|
+
const context = React.useContext(CompositionScopeContext);
|
|
18
|
+
if (!context) {
|
|
19
|
+
return {
|
|
20
|
+
scope: [],
|
|
21
|
+
inherit: false
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
return context;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
//# sourceMappingURL=CompositionScope.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["React","CompositionScopeContext","createContext","undefined","CompositionScope","name","inherit","children","parentScope","useCompositionScope","createElement","Provider","value","scope","context","useContext"],"sources":["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"],"mappings":"AAAA,OAAOA,KAAK,MAAM,OAAO;AAOzB,MAAMC,uBAAuB,gBAAGD,KAAK,CAACE,aAAa,CAAsCC,SAAS,CAAC;AAYnG,OAAO,MAAMC,gBAAgB,GAAGA,CAAC;EAAEC,IAAI;EAAEC,OAAO,GAAG,KAAK;EAAEC;AAAgC,CAAC,KAAK;EAC5F,MAAMC,WAAW,GAAGC,mBAAmB,CAAC,CAAC;EAEzC,oBACIT,KAAA,CAAAU,aAAA,CAACT,uBAAuB,CAACU,QAAQ;IAACC,KAAK,EAAE;MAAEC,KAAK,EAAE,CAAC,GAAGL,WAAW,CAACK,KAAK,EAAER,IAAI,CAAC;MAAEC;IAAQ;EAAE,GACrFC,QAC6B,CAAC;AAE3C,CAAC;AAED,OAAO,SAASE,mBAAmBA,CAAA,EAAG;EAClC,MAAMK,OAAO,GAAGd,KAAK,CAACe,UAAU,CAACd,uBAAuB,CAAC;EAEzD,IAAI,CAACa,OAAO,EAAE;IACV,OAAO;MAAED,KAAK,EAAE,EAAE;MAAEP,OAAO,EAAE;IAAM,CAAC;EACxC;EAEA,OAAOQ,OAAO;AAClB","ignoreList":[]}
|
package/Context.d.ts
CHANGED
|
@@ -1,38 +1,52 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
1
|
+
import type { ComponentType } from "react";
|
|
2
|
+
import React from "react";
|
|
3
|
+
import type { ComposedFunction, ComposeWith, Decoratable, DecoratableComponent, DecoratableHook, Decorator, Enumerable, GenericComponent, GenericHook } from "./types.js";
|
|
4
|
+
export declare function compose<T>(...fns: Decorator<T>[]): (decoratee: T) => T;
|
|
3
5
|
interface ComposedComponent {
|
|
4
6
|
/**
|
|
5
7
|
* Ready to use React component.
|
|
6
8
|
*/
|
|
7
|
-
component:
|
|
9
|
+
component: GenericHook | GenericComponent;
|
|
8
10
|
/**
|
|
9
11
|
* HOCs used to compose the original component.
|
|
10
12
|
*/
|
|
11
|
-
hocs:
|
|
13
|
+
hocs: Decorator<GenericComponent | GenericHook>[];
|
|
14
|
+
/**
|
|
15
|
+
* Component composition can be scoped.
|
|
16
|
+
*/
|
|
17
|
+
scope?: string;
|
|
12
18
|
}
|
|
13
19
|
/**
|
|
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.
|
|
20
|
+
* @deprecated Use `Decorator` instead.
|
|
17
21
|
*/
|
|
18
|
-
export interface HigherOrderComponent<
|
|
19
|
-
(Component:
|
|
22
|
+
export interface HigherOrderComponent<TProps = any, TOutput = TProps> {
|
|
23
|
+
(Component: GenericComponent<TProps>): GenericComponent<TOutput>;
|
|
24
|
+
}
|
|
25
|
+
type ComposedComponents = Map<ComponentType<unknown>, ComposedComponent>;
|
|
26
|
+
type ComponentScopes = Map<string, ComposedComponents>;
|
|
27
|
+
export type DecoratableTypes = DecoratableComponent | DecoratableHook;
|
|
28
|
+
interface CompositionContextGetComponentCallable {
|
|
29
|
+
(component: ComponentType<unknown>, scope: string[]): ComposedFunction | GenericComponent | undefined;
|
|
30
|
+
}
|
|
31
|
+
interface CompositionContextValue {
|
|
32
|
+
components: ComponentScopes;
|
|
33
|
+
getComponent: CompositionContextGetComponentCallable;
|
|
34
|
+
composeComponent(component: ComponentType<unknown>, hocs: Enumerable<ComposeWith>, scope?: string, inherit?: boolean): void;
|
|
20
35
|
}
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
36
|
+
export type DecoratorsTuple = [Decoratable, Decorator<any>[]];
|
|
37
|
+
export type DecoratorsCollection = Array<DecoratorsTuple>;
|
|
38
|
+
interface CompositionProviderProps {
|
|
39
|
+
decorators?: DecoratorsCollection;
|
|
40
|
+
children: React.ReactNode;
|
|
26
41
|
}
|
|
27
|
-
declare const
|
|
28
|
-
export declare
|
|
29
|
-
export declare function useComponent(Component: ComponentType<any>): React.ComponentClass<unknown, any> | React.FunctionComponent<unknown>;
|
|
42
|
+
export declare const CompositionProvider: ({ decorators, children }: CompositionProviderProps) => React.JSX.Element;
|
|
43
|
+
export declare function useComponent<T>(baseFunction: T): T;
|
|
30
44
|
/**
|
|
31
45
|
* This hook will throw an error if composition context doesn't exist.
|
|
32
46
|
*/
|
|
33
|
-
export declare function useComposition():
|
|
47
|
+
export declare function useComposition(): CompositionContextValue;
|
|
34
48
|
/**
|
|
35
49
|
* This hook will not throw an error if composition context doesn't exist.
|
|
36
50
|
*/
|
|
37
|
-
export declare function useOptionalComposition():
|
|
51
|
+
export declare function useOptionalComposition(): CompositionContextValue | undefined;
|
|
38
52
|
export {};
|
package/Context.js
CHANGED
|
@@ -1,126 +1,120 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
Object.defineProperty(exports, "__esModule", {
|
|
8
|
-
value: true
|
|
9
|
-
});
|
|
10
|
-
exports.CompositionProvider = void 0;
|
|
11
|
-
exports.compose = compose;
|
|
12
|
-
exports.useComponent = useComponent;
|
|
13
|
-
exports.useComposition = useComposition;
|
|
14
|
-
exports.useOptionalComposition = useOptionalComposition;
|
|
15
|
-
|
|
16
|
-
var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
|
|
17
|
-
|
|
18
|
-
var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
|
|
19
|
-
|
|
20
|
-
var _react = _interopRequireWildcard(require("react"));
|
|
21
|
-
|
|
22
|
-
function compose() {
|
|
23
|
-
for (var _len = arguments.length, fns = new Array(_len), _key = 0; _key < _len; _key++) {
|
|
24
|
-
fns[_key] = arguments[_key];
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
return function ComposedComponent(Base) {
|
|
28
|
-
return fns.reduceRight(function (Component, hoc) {
|
|
29
|
-
return hoc(Component);
|
|
30
|
-
}, Base);
|
|
1
|
+
import React, { createContext, useCallback, useContext, useMemo, useState } from "react";
|
|
2
|
+
import { useCompositionScope } from "./CompositionScope.js";
|
|
3
|
+
export function compose(...fns) {
|
|
4
|
+
return decoratee => {
|
|
5
|
+
return fns.reduceRight((decoratee, decorator) => decorator(decoratee), decoratee);
|
|
31
6
|
};
|
|
32
7
|
}
|
|
33
8
|
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
var children = _ref.children;
|
|
38
|
-
|
|
39
|
-
var _useState = (0, _react.useState)(new Map()),
|
|
40
|
-
_useState2 = (0, _slicedToArray2.default)(_useState, 2),
|
|
41
|
-
components = _useState2[0],
|
|
42
|
-
setComponents = _useState2[1];
|
|
9
|
+
/**
|
|
10
|
+
* @deprecated Use `Decorator` instead.
|
|
11
|
+
*/
|
|
43
12
|
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
13
|
+
const CompositionContext = /*#__PURE__*/createContext(undefined);
|
|
14
|
+
const composeComponents = (components, decorators, scope = "*", inherit = false) => {
|
|
15
|
+
const scopeMap = components.get(scope) || new Map();
|
|
16
|
+
for (const [component, newHocs] of decorators) {
|
|
17
|
+
const recipe = scopeMap.get(component) || {
|
|
18
|
+
component: null,
|
|
19
|
+
hocs: []
|
|
20
|
+
};
|
|
21
|
+
const existingHocs = [...(recipe.hocs || [])];
|
|
22
|
+
if (inherit && scope !== "*") {
|
|
23
|
+
const globalScope = components.get("*") || new Map();
|
|
24
|
+
const globalRecipe = globalScope.get(component) || {
|
|
48
25
|
component: null,
|
|
49
26
|
hocs: []
|
|
50
27
|
};
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
});
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
28
|
+
existingHocs.unshift(...globalRecipe.hocs);
|
|
29
|
+
}
|
|
30
|
+
const finalHocs = [...existingHocs, ...newHocs];
|
|
31
|
+
scopeMap.set(component, {
|
|
32
|
+
component: compose(...[...finalHocs].reverse())(component),
|
|
33
|
+
hocs: finalHocs
|
|
34
|
+
});
|
|
35
|
+
components.set(scope, scopeMap);
|
|
36
|
+
}
|
|
37
|
+
return components;
|
|
38
|
+
};
|
|
39
|
+
export const CompositionProvider = ({
|
|
40
|
+
decorators = [],
|
|
41
|
+
children
|
|
42
|
+
}) => {
|
|
43
|
+
const [components, setComponents] = useState(() => {
|
|
44
|
+
return composeComponents(new Map(), decorators.map(tuple => {
|
|
45
|
+
return [tuple[0].original, tuple[1]];
|
|
46
|
+
}));
|
|
47
|
+
});
|
|
48
|
+
const composeComponent = useCallback((component, hocs, scope = "*", inherit = false) => {
|
|
49
|
+
setComponents(prevComponents => {
|
|
50
|
+
return composeComponents(new Map(prevComponents), [[component, hocs]], scope, inherit);
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
// Return a function that will remove the added HOCs.
|
|
54
|
+
return () => {
|
|
55
|
+
setComponents(prevComponents => {
|
|
56
|
+
const components = new Map(prevComponents);
|
|
57
|
+
const scopeMap = components.get(scope) || new Map();
|
|
58
|
+
const recipe = scopeMap.get(component) || {
|
|
63
59
|
component: null,
|
|
64
60
|
hocs: []
|
|
65
61
|
};
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
var NewComponent = compose.apply(void 0, (0, _toConsumableArray2.default)((0, _toConsumableArray2.default)(newHOCs).reverse()))(component);
|
|
70
|
-
components.set(component, {
|
|
62
|
+
const newHOCs = [...recipe.hocs].filter(hoc => !hocs.includes(hoc));
|
|
63
|
+
const NewComponent = compose(...[...newHOCs].reverse())(component);
|
|
64
|
+
scopeMap.set(component, {
|
|
71
65
|
component: NewComponent,
|
|
72
66
|
hocs: newHOCs
|
|
73
67
|
});
|
|
68
|
+
components.set(scope, scopeMap);
|
|
74
69
|
return components;
|
|
75
70
|
});
|
|
76
71
|
};
|
|
77
72
|
}, [setComponents]);
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
73
|
+
const getComponent = useCallback((Component, scope = []) => {
|
|
74
|
+
const scopesToResolve = ["*", ...scope].reverse();
|
|
75
|
+
for (const scope of scopesToResolve) {
|
|
76
|
+
const scopeMap = components.get(scope) || new Map();
|
|
77
|
+
const composedComponent = scopeMap.get(Component);
|
|
78
|
+
if (composedComponent) {
|
|
79
|
+
return composedComponent.component;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
return undefined;
|
|
81
83
|
}, [components]);
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
}, [components, composeComponent]);
|
|
89
|
-
return /*#__PURE__*/_react.default.createElement(CompositionContext.Provider, {
|
|
84
|
+
const context = useMemo(() => ({
|
|
85
|
+
getComponent,
|
|
86
|
+
composeComponent,
|
|
87
|
+
components
|
|
88
|
+
}), [components, composeComponent]);
|
|
89
|
+
return /*#__PURE__*/React.createElement(CompositionContext.Provider, {
|
|
90
90
|
value: context
|
|
91
91
|
}, children);
|
|
92
92
|
};
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
function useComponent(Component) {
|
|
97
|
-
var context = useOptionalComposition();
|
|
98
|
-
|
|
93
|
+
export function useComponent(baseFunction) {
|
|
94
|
+
const context = useOptionalComposition();
|
|
95
|
+
const scope = useCompositionScope();
|
|
99
96
|
if (!context) {
|
|
100
|
-
return
|
|
97
|
+
return baseFunction;
|
|
101
98
|
}
|
|
102
|
-
|
|
103
|
-
return context.getComponent(Component) || Component;
|
|
99
|
+
return context.getComponent(baseFunction, scope.scope) || baseFunction;
|
|
104
100
|
}
|
|
101
|
+
|
|
105
102
|
/**
|
|
106
103
|
* This hook will throw an error if composition context doesn't exist.
|
|
107
104
|
*/
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
function useComposition() {
|
|
111
|
-
var context = (0, _react.useContext)(CompositionContext);
|
|
112
|
-
|
|
105
|
+
export function useComposition() {
|
|
106
|
+
const context = useContext(CompositionContext);
|
|
113
107
|
if (!context) {
|
|
114
|
-
throw new Error(
|
|
108
|
+
throw new Error(`You're missing a <CompositionProvider> higher up in your component hierarchy!`);
|
|
115
109
|
}
|
|
116
|
-
|
|
117
110
|
return context;
|
|
118
111
|
}
|
|
112
|
+
|
|
119
113
|
/**
|
|
120
114
|
* This hook will not throw an error if composition context doesn't exist.
|
|
121
115
|
*/
|
|
116
|
+
export function useOptionalComposition() {
|
|
117
|
+
return useContext(CompositionContext);
|
|
118
|
+
}
|
|
122
119
|
|
|
123
|
-
|
|
124
|
-
function useOptionalComposition() {
|
|
125
|
-
return (0, _react.useContext)(CompositionContext);
|
|
126
|
-
}
|
|
120
|
+
//# sourceMappingURL=Context.js.map
|
package/Context.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["compose","fns","ComposedComponent","Base","reduceRight","Component","hoc","CompositionContext","createContext","undefined","CompositionProvider","children","useState","Map","components","setComponents","composeComponent","useCallback","component","hocs","prevComponents","recipe","get","newHocs","set","reverse","newHOCs","filter","includes","NewComponent","getComponent","composedComponent","context","useMemo","useComponent","useOptionalComposition","useComposition","useContext","Error"],"sources":["Context.tsx"],"sourcesContent":["import React, {\n FC,\n ComponentType,\n useState,\n useCallback,\n createContext,\n useContext,\n useMemo\n} from \"react\";\n\nexport function compose(...fns: HigherOrderComponent[]) {\n return function ComposedComponent(Base: FC<unknown>): FC<unknown> {\n return fns.reduceRight((Component, hoc) => hoc(Component), Base);\n };\n}\n\ninterface ComposedComponent {\n /**\n * Ready to use React component.\n */\n component: ComponentType<unknown>;\n /**\n * HOCs used to compose the original component.\n */\n hocs: HigherOrderComponent[];\n}\n\n/**\n * IMPORTANT: TInputProps default type is `any` because this interface is use as a prop type in the `Compose` component.\n * You can pass any HigherOrderComponent as a prop, regardless of its TInputProps type. The only way to allow that is\n * to let it be `any` in this interface.\n */\nexport interface HigherOrderComponent<TInputProps = any, TOutputProps = TInputProps> {\n (Component: FC<TInputProps>): FC<TOutputProps>;\n}\n\ntype ComposedComponents = Map<ComponentType<unknown>, ComposedComponent>;\n\ninterface CompositionContext {\n components: ComposedComponents;\n getComponent(component: ComponentType<unknown>): ComponentType<unknown> | undefined;\n composeComponent(component: ComponentType<unknown>, hocs: HigherOrderComponent[]): void;\n}\n\nconst CompositionContext = createContext<CompositionContext | undefined>(undefined);\n\nexport const CompositionProvider: React.FC = ({ children }) => {\n const [components, setComponents] = useState<ComposedComponents>(new Map());\n\n const composeComponent = useCallback(\n (component, hocs) => {\n setComponents(prevComponents => {\n const components = new Map(prevComponents);\n const recipe = components.get(component) || { component: null, hocs: [] };\n\n const newHocs = [...(recipe.hocs || []), ...hocs];\n\n components.set(component, {\n component: compose(...[...newHocs].reverse())(component),\n hocs: newHocs\n });\n\n return components;\n });\n\n // Return a function that will remove the added HOCs.\n return () => {\n setComponents(prevComponents => {\n const components = new Map(prevComponents);\n const recipe = components.get(component) || {\n component: null,\n hocs: []\n };\n\n const newHOCs = [...recipe.hocs].filter(hoc => !hocs.includes(hoc));\n const NewComponent = compose(...[...newHOCs].reverse())(component);\n\n components.set(component, {\n component: NewComponent,\n hocs: newHOCs\n });\n\n return components;\n });\n };\n },\n [setComponents]\n );\n\n const getComponent: CompositionContext[\"getComponent\"] = useCallback(\n Component => {\n const composedComponent = components.get(Component);\n return composedComponent ? composedComponent.component : undefined;\n },\n [components]\n );\n\n const context: CompositionContext = useMemo(\n () => ({\n getComponent,\n composeComponent,\n components\n }),\n [components, composeComponent]\n );\n\n return <CompositionContext.Provider value={context}>{children}</CompositionContext.Provider>;\n};\n\nexport function useComponent(Component: ComponentType<any>) {\n const context = useOptionalComposition();\n\n if (!context) {\n return Component;\n }\n\n return context.getComponent(Component) || Component;\n}\n\n/**\n * This hook will throw an error if composition context doesn't exist.\n */\nexport function useComposition() {\n const context = useContext(CompositionContext);\n if (!context) {\n throw new Error(\n `You're missing a <CompositionProvider> higher up in your component hierarchy!`\n );\n }\n\n return context;\n}\n\n/**\n * This hook will not throw an error if composition context doesn't exist.\n */\nexport function useOptionalComposition() {\n return useContext(CompositionContext);\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;AAAA;;AAUO,SAASA,OAAT,GAAiD;EAAA,kCAA7BC,GAA6B;IAA7BA,GAA6B;EAAA;;EACpD,OAAO,SAASC,iBAAT,CAA2BC,IAA3B,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;EACH;;EAED,OAAO2B,OAAO,CAACF,YAAR,CAAqBzB,SAArB,KAAmCA,SAA1C;AACH;AAED;AACA;AACA;;;AACO,SAAS+B,cAAT,GAA0B;EAC7B,IAAMJ,OAAO,GAAG,IAAAK,iBAAA,EAAW9B,kBAAX,CAAhB;;EACA,IAAI,CAACyB,OAAL,EAAc;IACV,MAAM,IAAIM,KAAJ,iFAAN;EAGH;;EAED,OAAON,OAAP;AACH;AAED;AACA;AACA;;;AACO,SAASG,sBAAT,GAAkC;EACrC,OAAO,IAAAE,iBAAA,EAAW9B,kBAAX,CAAP;AACH"}
|
|
1
|
+
{"version":3,"names":["React","createContext","useCallback","useContext","useMemo","useState","useCompositionScope","compose","fns","decoratee","reduceRight","decorator","CompositionContext","undefined","composeComponents","components","decorators","scope","inherit","scopeMap","get","Map","component","newHocs","recipe","hocs","existingHocs","globalScope","globalRecipe","unshift","finalHocs","set","reverse","CompositionProvider","children","setComponents","map","tuple","original","composeComponent","prevComponents","newHOCs","filter","hoc","includes","NewComponent","getComponent","Component","scopesToResolve","composedComponent","context","createElement","Provider","value","useComponent","baseFunction","useOptionalComposition","useComposition","Error"],"sources":["Context.tsx"],"sourcesContent":["import type { ComponentType } from \"react\";\nimport React, { createContext, useCallback, useContext, useMemo, useState } from \"react\";\nimport { useCompositionScope } from \"~/CompositionScope.js\";\nimport type {\n ComposedFunction,\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\ninterface ComposedComponent {\n /**\n * Ready to use React component.\n */\n component: GenericHook | GenericComponent;\n /**\n * HOCs used to compose the original component.\n */\n hocs: Decorator<GenericComponent | GenericHook>[];\n /**\n * Component composition can be scoped.\n */\n scope?: string;\n}\n\n/**\n * @deprecated Use `Decorator` instead.\n */\nexport interface HigherOrderComponent<TProps = any, TOutput = TProps> {\n (Component: GenericComponent<TProps>): GenericComponent<TOutput>;\n}\n\ntype ComposedComponents = Map<ComponentType<unknown>, ComposedComponent>;\ntype ComponentScopes = Map<string, ComposedComponents>;\n\nexport type DecoratableTypes = DecoratableComponent | DecoratableHook;\n\ninterface CompositionContextGetComponentCallable {\n (\n component: ComponentType<unknown>,\n scope: string[]\n ): ComposedFunction | GenericComponent | undefined;\n}\n\ninterface CompositionContextValue {\n components: ComponentScopes;\n getComponent: CompositionContextGetComponentCallable;\n composeComponent(\n component: ComponentType<unknown>,\n hocs: Enumerable<ComposeWith>,\n scope?: string,\n inherit?: boolean\n ): void;\n}\n\nconst CompositionContext = createContext<CompositionContextValue | 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\nconst composeComponents = (\n components: ComponentScopes,\n decorators: Array<[GenericComponent | GenericHook, Decorator<any>[]]>,\n scope = \"*\",\n inherit = false\n) => {\n const scopeMap: ComposedComponents = components.get(scope) || new Map();\n for (const [component, newHocs] of decorators) {\n const recipe = scopeMap.get(component) || { component: null, hocs: [] };\n\n const existingHocs = [...(recipe.hocs || [])];\n if (inherit && scope !== \"*\") {\n const globalScope = components.get(\"*\") || new Map();\n const globalRecipe = globalScope.get(component) || { component: null, hocs: [] };\n existingHocs.unshift(...globalRecipe.hocs);\n }\n\n const finalHocs = [...existingHocs, ...newHocs] as Decorator<\n GenericHook | GenericComponent\n >[];\n\n scopeMap.set(component, {\n component: compose(...[...finalHocs].reverse())(component),\n hocs: finalHocs\n });\n\n components.set(scope, scopeMap);\n }\n\n return components;\n};\n\nexport const CompositionProvider = ({ decorators = [], children }: CompositionProviderProps) => {\n const [components, setComponents] = useState<ComponentScopes>(() => {\n return composeComponents(\n new Map(),\n decorators.map(tuple => {\n return [tuple[0].original, tuple[1]];\n })\n );\n });\n\n const composeComponent = useCallback(\n (\n component: GenericComponent | GenericHook,\n hocs: HigherOrderComponent<any, any>[],\n scope: string | undefined = \"*\",\n inherit = false\n ) => {\n setComponents(prevComponents => {\n return composeComponents(\n new Map(prevComponents),\n [[component, hocs]],\n scope,\n inherit\n );\n });\n\n // Return a function that will remove the added HOCs.\n return () => {\n setComponents(prevComponents => {\n const components = new Map(prevComponents);\n const scopeMap: ComposedComponents = components.get(scope) || new Map();\n const recipe = scopeMap.get(component) || {\n component: null,\n hocs: []\n };\n\n const newHOCs = [...recipe.hocs].filter(hoc => !hocs.includes(hoc));\n const NewComponent = compose(...[...newHOCs].reverse())(component);\n\n scopeMap.set(component, {\n component: NewComponent,\n hocs: newHOCs\n });\n\n components.set(scope, scopeMap);\n return components;\n });\n };\n },\n [setComponents]\n );\n\n const getComponent = useCallback<CompositionContextGetComponentCallable>(\n (Component, scope = []) => {\n const scopesToResolve = [\"*\", ...scope].reverse();\n for (const scope of scopesToResolve) {\n const scopeMap: ComposedComponents = components.get(scope) || new Map();\n const composedComponent = scopeMap.get(Component);\n if (composedComponent) {\n return composedComponent.component;\n }\n }\n\n return undefined;\n },\n [components]\n );\n\n const context: CompositionContextValue = useMemo(\n () => ({\n getComponent,\n composeComponent,\n components\n }),\n [components, composeComponent]\n );\n\n return <CompositionContext.Provider value={context}>{children}</CompositionContext.Provider>;\n};\n\nexport function useComponent<T>(baseFunction: T) {\n const context = useOptionalComposition();\n const scope = useCompositionScope();\n\n if (!context) {\n return baseFunction;\n }\n\n return (context.getComponent(baseFunction as any, scope.scope) || baseFunction) as T;\n}\n\n/**\n * This hook will throw an error if composition context doesn't exist.\n */\nexport function useComposition() {\n const context = useContext(CompositionContext);\n if (!context) {\n throw new Error(\n `You're missing a <CompositionProvider> higher up in your component hierarchy!`\n );\n }\n\n return context;\n}\n\n/**\n * This hook will not throw an error if composition context doesn't exist.\n */\nexport function useOptionalComposition() {\n return useContext(Composition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CAAC,EACD,CAACE,UAAU,CACf,CAAC;EAED,MAAMmC,OAAgC,GAAG9C,OAAO,CAC5C,OAAO;IACH0C,YAAY;IACZP,gBAAgB;IAChBxB;EACJ,CAAC,CAAC,EACF,CAACA,UAAU,EAAEwB,gBAAgB,CACjC,CAAC;EAED,oBAAOvC,KAAA,CAAAmD,aAAA,CAACvC,kBAAkB,CAACwC,QAAQ;IAACC,KAAK,EAAEH;EAAQ,GAAEhB,QAAsC,CAAC;AAChG,CAAC;AAED,OAAO,SAASoB,YAAYA,CAAIC,YAAe,EAAE;EAC7C,MAAML,OAAO,GAAGM,sBAAsB,CAAC,CAAC;EACxC,MAAMvC,KAAK,GAAGX,mBAAmB,CAAC,CAAC;EAEnC,IAAI,CAAC4C,OAAO,EAAE;IACV,OAAOK,YAAY;EACvB;EAEA,OAAQL,OAAO,CAACJ,YAAY,CAACS,YAAY,EAAStC,KAAK,CAACA,KAAK,CAAC,IAAIsC,YAAY;AAClF;;AAEA;AACA;AACA;AACA,OAAO,SAASE,cAAcA,CAAA,EAAG;EAC7B,MAAMP,OAAO,GAAG/C,UAAU,CAACS,kBAAkB,CAAC;EAC9C,IAAI,CAACsC,OAAO,EAAE;IACV,MAAM,IAAIQ,KAAK,CACX,+EACJ,CAAC;EACL;EAEA,OAAOR,OAAO;AAClB;;AAEA;AACA;AACA;AACA,OAAO,SAASM,sBAAsBA,CAAA,EAAG;EACrC,OAAOrD,UAAU,CAACS,kBAAkB,CAAC;AACzC","ignoreList":[]}
|
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,29 @@
|
|
|
1
|
+
import React from "react";
|
|
2
|
+
import { Compose } from "./Compose.js";
|
|
3
|
+
/**
|
|
4
|
+
* Creates a component which, when mounted, registers a Higher Order Component for the given base component.
|
|
5
|
+
* This is particularly useful for decorating (wrapping) existing composable components.
|
|
6
|
+
* For more information, visit https://www.webiny.com/docs/admin-area/basics/framework.
|
|
7
|
+
*/
|
|
8
|
+
export function createComponentPlugin(Base, hoc) {
|
|
9
|
+
return createDecorator(Base, hoc);
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
// Maybe there's a better way to mark params as non-existent, but for now I left it as `any`.
|
|
13
|
+
// TODO: revisit this type; not sure if `?` can be handled in one clause
|
|
14
|
+
|
|
15
|
+
const isDecoratableComponent = decoratable => {
|
|
16
|
+
return "displayName" in decoratable;
|
|
17
|
+
};
|
|
18
|
+
export function createDecorator(Base, hoc) {
|
|
19
|
+
const DecoratorPlugin = () => /*#__PURE__*/React.createElement(Compose, {
|
|
20
|
+
component: Base,
|
|
21
|
+
with: hoc
|
|
22
|
+
});
|
|
23
|
+
if (isDecoratableComponent(Base)) {
|
|
24
|
+
DecoratorPlugin.displayName = Base.displayName;
|
|
25
|
+
}
|
|
26
|
+
return DecoratorPlugin;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
//# sourceMappingURL=createDecorator.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["React","Compose","createComponentPlugin","Base","hoc","createDecorator","isDecoratableComponent","decoratable","DecoratorPlugin","createElement","component","with","displayName"],"sources":["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"],"mappings":"AAAA,OAAOA,KAAK,MAAM,OAAO;AAQzB,SAASC,OAAO;AAIhB;AACA;AACA;AACA;AACA;AACA,OAAO,SAASC,qBAAqBA,CACjCC,IAAO,EACPC,GAEmC,EACrC;EACE,OAAOC,eAAe,CAACF,IAAI,EAAEC,GAAG,CAAC;AACrC;;AAEA;AACA;;AAUA,MAAME,sBAAsB,GACxBC,WAAmD,IACb;EACtC,OAAO,aAAa,IAAIA,WAAW;AACvC,CAAC;AAED,OAAO,SAASF,eAAeA,CAC3BF,IAAO,EACPC,GAEmC,EACrC;EACE,MAAMI,eAAe,GAAGA,CAAA,kBAAMR,KAAA,CAAAS,aAAA,CAACR,OAAO;IAACS,SAAS,EAAEP,IAAK;IAACQ,IAAI,EAAEP;EAAW,CAAE,CAAC;EAC5E,IAAIE,sBAAsB,CAACH,IAAI,CAAC,EAAE;IAC9BK,eAAe,CAACI,WAAW,GAAGT,IAAI,CAACS,WAAW;EAClD;EACA,OAAOJ,eAAe;AAC1B","ignoreList":[]}
|
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,71 @@
|
|
|
1
|
+
import React from "react";
|
|
2
|
+
import { Compose } from "./Compose.js";
|
|
3
|
+
export function createConditionalDecorator(shouldDecorate, decorator, decoratorProps) {
|
|
4
|
+
return Original => {
|
|
5
|
+
const DecoratedComponent = /*#__PURE__*/React.memo(decorator(Original));
|
|
6
|
+
DecoratedComponent.displayName = Original.displayName;
|
|
7
|
+
return function ShouldDecorate(props) {
|
|
8
|
+
if (shouldDecorate(decoratorProps, props)) {
|
|
9
|
+
// @ts-expect-error
|
|
10
|
+
return /*#__PURE__*/React.createElement(DecoratedComponent, props);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
// @ts-expect-error
|
|
14
|
+
return /*#__PURE__*/React.createElement(Original, props);
|
|
15
|
+
};
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
const memoizedComponent = decorator => {
|
|
19
|
+
return decoratee => {
|
|
20
|
+
return /*#__PURE__*/React.memo(decorator(decoratee));
|
|
21
|
+
};
|
|
22
|
+
};
|
|
23
|
+
export function createDecoratorFactory() {
|
|
24
|
+
return function from(decoratable, shouldDecorate) {
|
|
25
|
+
return function createDecorator(decorator) {
|
|
26
|
+
return function DecoratorPlugin(props) {
|
|
27
|
+
if (shouldDecorate) {
|
|
28
|
+
const componentDecorator = createConditionalDecorator(shouldDecorate, decorator, props);
|
|
29
|
+
return /*#__PURE__*/React.createElement(Compose, {
|
|
30
|
+
function: decoratable,
|
|
31
|
+
with: componentDecorator
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
return /*#__PURE__*/React.createElement(Compose, {
|
|
35
|
+
function: decoratable,
|
|
36
|
+
with: memoizedComponent(decorator)
|
|
37
|
+
});
|
|
38
|
+
};
|
|
39
|
+
};
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
export function createHookDecoratorFactory() {
|
|
43
|
+
return function from(decoratable) {
|
|
44
|
+
return function createDecorator(decorator) {
|
|
45
|
+
return function DecoratorPlugin() {
|
|
46
|
+
return /*#__PURE__*/React.createElement(Compose, {
|
|
47
|
+
function: decoratable,
|
|
48
|
+
with: decorator
|
|
49
|
+
});
|
|
50
|
+
};
|
|
51
|
+
};
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
export function withDecoratorFactory() {
|
|
55
|
+
return function WithDecorator(Component, shouldDecorate) {
|
|
56
|
+
const createDecorator = createDecoratorFactory()(Component, shouldDecorate);
|
|
57
|
+
return Object.assign(Component, {
|
|
58
|
+
createDecorator
|
|
59
|
+
});
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
export function withHookDecoratorFactory() {
|
|
63
|
+
return function WithHookDecorator(hook) {
|
|
64
|
+
const createDecorator = createHookDecoratorFactory()(hook);
|
|
65
|
+
return Object.assign(hook, {
|
|
66
|
+
createDecorator
|
|
67
|
+
});
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
//# sourceMappingURL=decorators.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["React","Compose","createConditionalDecorator","shouldDecorate","decorator","decoratorProps","Original","DecoratedComponent","memo","displayName","ShouldDecorate","props","createElement","memoizedComponent","decoratee","createDecoratorFactory","from","decoratable","createDecorator","DecoratorPlugin","componentDecorator","function","with","createHookDecoratorFactory","withDecoratorFactory","WithDecorator","Component","Object","assign","withHookDecoratorFactory","WithHookDecorator","hook"],"sources":["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"],"mappings":"AAAA,OAAOA,KAAK,MAAM,OAAO;AACzB,SAASC,OAAO;AAehB,OAAO,SAASC,0BAA0BA,CACtCC,cAA8B,EAC9BC,SAAgC,EAChCC,cAAuB,EACF;EACrB,OAAQC,QAAQ,IAAI;IAChB,MAAMC,kBAAkB,gBAAGP,KAAK,CAACQ,IAAI,CAACJ,SAAS,CAACE,QAAQ,CAAC,CAAC;IAC1DC,kBAAkB,CAACE,WAAW,GAAGH,QAAQ,CAACG,WAAW;IAErD,OAAO,SAASC,cAAcA,CAACC,KAAc,EAAE;MAC3C,IAAIR,cAAc,CAACE,cAAc,EAAEM,KAAK,CAAC,EAAE;QACvC;QACA,oBAAOX,KAAA,CAAAY,aAAA,CAACL,kBAAkB,EAAKI,KAAQ,CAAC;MAC5C;;MAEA;MACA,oBAAOX,KAAA,CAAAY,aAAA,CAACN,QAAQ,EAAKK,KAAQ,CAAC;IAClC,CAAC;EACL,CAAC;AACL;AAEA,MAAME,iBAAiB,GAAgCT,SAAuB,IAAK;EAC/E,OAAQU,SAAY,IAAK;IACrB,oBAAOd,KAAK,CAACQ,IAAI,CAACJ,SAAS,CAACU,SAAS,CAAC,CAAC;EAC3C,CAAC;AACL,CAAC;AAED,OAAO,SAASC,sBAAsBA,CAAA,EAAe;EACjD,OAAO,SAASC,IAAIA,CAChBC,WAAyB,EACzBd,cAA2F,EAC7F;IACE,OAAO,SAASe,eAAeA,CAACd,SAAyD,EAAE;MACvF,OAAO,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,EAAE;MAAEb;IAAgB,CAAC,CAAC;EAMnD,CAAC;AACL","ignoreList":[]}
|
package/index.d.ts
CHANGED
|
@@ -1,4 +1,8 @@
|
|
|
1
|
-
export * from "./Context";
|
|
2
|
-
export * from "./Compose";
|
|
3
|
-
export * from "./makeComposable";
|
|
4
|
-
export * from "./
|
|
1
|
+
export * from "./Context.js";
|
|
2
|
+
export * from "./Compose.js";
|
|
3
|
+
export * from "./makeComposable.js";
|
|
4
|
+
export * from "./makeDecoratable.js";
|
|
5
|
+
export * from "./createDecorator.js";
|
|
6
|
+
export * from "./decorators.js";
|
|
7
|
+
export * from "./CompositionScope.js";
|
|
8
|
+
export type * from "./types.js";
|
package/index.js
CHANGED
|
@@ -1,57 +1,9 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
if (key === "default" || key === "__esModule") return;
|
|
11
|
-
if (key in exports && exports[key] === _Context[key]) return;
|
|
12
|
-
Object.defineProperty(exports, key, {
|
|
13
|
-
enumerable: true,
|
|
14
|
-
get: function get() {
|
|
15
|
-
return _Context[key];
|
|
16
|
-
}
|
|
17
|
-
});
|
|
18
|
-
});
|
|
19
|
-
|
|
20
|
-
var _Compose = require("./Compose");
|
|
21
|
-
|
|
22
|
-
Object.keys(_Compose).forEach(function (key) {
|
|
23
|
-
if (key === "default" || key === "__esModule") return;
|
|
24
|
-
if (key in exports && exports[key] === _Compose[key]) return;
|
|
25
|
-
Object.defineProperty(exports, key, {
|
|
26
|
-
enumerable: true,
|
|
27
|
-
get: function get() {
|
|
28
|
-
return _Compose[key];
|
|
29
|
-
}
|
|
30
|
-
});
|
|
31
|
-
});
|
|
32
|
-
|
|
33
|
-
var _makeComposable = require("./makeComposable");
|
|
34
|
-
|
|
35
|
-
Object.keys(_makeComposable).forEach(function (key) {
|
|
36
|
-
if (key === "default" || key === "__esModule") return;
|
|
37
|
-
if (key in exports && exports[key] === _makeComposable[key]) return;
|
|
38
|
-
Object.defineProperty(exports, key, {
|
|
39
|
-
enumerable: true,
|
|
40
|
-
get: function get() {
|
|
41
|
-
return _makeComposable[key];
|
|
42
|
-
}
|
|
43
|
-
});
|
|
44
|
-
});
|
|
45
|
-
|
|
46
|
-
var _createComponentPlugin = require("./createComponentPlugin");
|
|
47
|
-
|
|
48
|
-
Object.keys(_createComponentPlugin).forEach(function (key) {
|
|
49
|
-
if (key === "default" || key === "__esModule") return;
|
|
50
|
-
if (key in exports && exports[key] === _createComponentPlugin[key]) return;
|
|
51
|
-
Object.defineProperty(exports, key, {
|
|
52
|
-
enumerable: true,
|
|
53
|
-
get: function get() {
|
|
54
|
-
return _createComponentPlugin[key];
|
|
55
|
-
}
|
|
56
|
-
});
|
|
57
|
-
});
|
|
1
|
+
export * from "./Context.js";
|
|
2
|
+
export * from "./Compose.js";
|
|
3
|
+
export * from "./makeComposable.js";
|
|
4
|
+
export * from "./makeDecoratable.js";
|
|
5
|
+
export * from "./createDecorator.js";
|
|
6
|
+
export * from "./decorators.js";
|
|
7
|
+
export * from "./CompositionScope.js";
|
|
8
|
+
|
|
9
|
+
//# sourceMappingURL=index.js.map
|
package/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":[],"sources":["index.ts"],"sourcesContent":["export * from \"./Context\";\nexport * from \"./Compose\";\nexport * from \"./makeComposable\";\nexport * from \"./
|
|
1
|
+
{"version":3,"names":[],"sources":["index.ts"],"sourcesContent":["export * from \"./Context.js\";\nexport * from \"./Compose.js\";\nexport * from \"./makeComposable.js\";\nexport * from \"./makeDecoratable.js\";\nexport * from \"./createDecorator.js\";\nexport * from \"./decorators.js\";\nexport * from \"./CompositionScope.js\";\nexport type * from \"./types.js\";\n"],"mappings":"AAAA;AACA;AACA;AACA;AACA;AACA;AACA","ignoreList":[]}
|
package/makeComposable.d.ts
CHANGED
|
@@ -1,3 +1,43 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
import type { GenericComponent } from "./types.js";
|
|
2
|
+
/**
|
|
3
|
+
* @deprecated Use `makeDecoratable` instead.
|
|
4
|
+
*/
|
|
5
|
+
export declare function makeComposable<T extends GenericComponent>(name: string, Component?: T): (((() => null) | T) & {
|
|
6
|
+
original: (() => null) | T;
|
|
7
|
+
originalName: string;
|
|
8
|
+
displayName: string;
|
|
9
|
+
}) & {
|
|
10
|
+
original: ((() => null) | T) & {
|
|
11
|
+
original: (() => null) | T;
|
|
12
|
+
originalName: string;
|
|
13
|
+
displayName: string;
|
|
14
|
+
};
|
|
15
|
+
originalName: string;
|
|
16
|
+
displayName: string;
|
|
17
|
+
} & {
|
|
18
|
+
createDecorator: (decorator: import("~/types.js").ComponentDecorator<import("./createDecorator").GetDecoratee<(() => null) & {
|
|
19
|
+
original: (() => null) | T;
|
|
20
|
+
originalName: string;
|
|
21
|
+
displayName: string;
|
|
22
|
+
} & {
|
|
23
|
+
original: ((() => null) | T) & {
|
|
24
|
+
original: (() => null) | T;
|
|
25
|
+
originalName: string;
|
|
26
|
+
displayName: string;
|
|
27
|
+
};
|
|
28
|
+
originalName: string;
|
|
29
|
+
displayName: string;
|
|
30
|
+
}> | import("./createDecorator").GetDecoratee<T & {
|
|
31
|
+
original: (() => null) | T;
|
|
32
|
+
originalName: string;
|
|
33
|
+
displayName: string;
|
|
34
|
+
} & {
|
|
35
|
+
original: ((() => null) | T) & {
|
|
36
|
+
original: (() => null) | T;
|
|
37
|
+
originalName: string;
|
|
38
|
+
displayName: string;
|
|
39
|
+
};
|
|
40
|
+
originalName: string;
|
|
41
|
+
displayName: string;
|
|
42
|
+
}>>) => (props: unknown) => import("react").JSX.Element;
|
|
43
|
+
};
|
package/makeComposable.js
CHANGED
|
@@ -1,72 +1,14 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
|
|
6
|
-
|
|
7
|
-
Object.defineProperty(exports, "__esModule", {
|
|
8
|
-
value: true
|
|
9
|
-
});
|
|
10
|
-
exports.makeComposable = makeComposable;
|
|
11
|
-
|
|
12
|
-
var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
|
|
13
|
-
|
|
14
|
-
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
|
|
15
|
-
|
|
16
|
-
var _react = _interopRequireWildcard(require("react"));
|
|
17
|
-
|
|
18
|
-
var _lodash = _interopRequireDefault(require("lodash.debounce"));
|
|
19
|
-
|
|
20
|
-
var _Context = require("./Context");
|
|
21
|
-
|
|
22
|
-
var ComposableContext = /*#__PURE__*/(0, _react.createContext)([]);
|
|
1
|
+
import { createContext } from "react";
|
|
2
|
+
import { makeDecoratable } from "./makeDecoratable.js";
|
|
3
|
+
const ComposableContext = /*#__PURE__*/createContext([]);
|
|
23
4
|
ComposableContext.displayName = "ComposableContext";
|
|
5
|
+
const nullRenderer = () => null;
|
|
24
6
|
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
return context;
|
|
7
|
+
/**
|
|
8
|
+
* @deprecated Use `makeDecoratable` instead.
|
|
9
|
+
*/
|
|
10
|
+
export function makeComposable(name, Component) {
|
|
11
|
+
return makeDecoratable(name, Component ?? nullRenderer);
|
|
33
12
|
}
|
|
34
13
|
|
|
35
|
-
|
|
36
|
-
return (0, _defineProperty2.default)({}, name, function () {
|
|
37
|
-
(0, _react.useEffect)(function () {
|
|
38
|
-
// We need to debounce the log, as it sometimes only requires a single tick to get the new
|
|
39
|
-
// composed component to render, and we don't want to scare developers for no reason.
|
|
40
|
-
var debounced = (0, _lodash.default)(function () {
|
|
41
|
-
console.info("<".concat(name, "/> is not implemented! To provide an implementation, use the <Compose/> component."));
|
|
42
|
-
}, 100);
|
|
43
|
-
return function () {
|
|
44
|
-
debounced.cancel();
|
|
45
|
-
};
|
|
46
|
-
}, []);
|
|
47
|
-
return null;
|
|
48
|
-
})[name];
|
|
49
|
-
};
|
|
50
|
-
|
|
51
|
-
function makeComposable(name, Component) {
|
|
52
|
-
if (!Component) {
|
|
53
|
-
Component = createEmptyRenderer(name);
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
var Composable = function Composable(props) {
|
|
57
|
-
var parents = useComposableParents();
|
|
58
|
-
var ComposedComponent = (0, _Context.useComponent)(Component);
|
|
59
|
-
var context = (0, _react.useMemo)(function () {
|
|
60
|
-
return [].concat((0, _toConsumableArray2.default)(parents), [name]);
|
|
61
|
-
}, [parents, name]);
|
|
62
|
-
return /*#__PURE__*/_react.default.createElement(ComposableContext.Provider, {
|
|
63
|
-
value: context
|
|
64
|
-
}, /*#__PURE__*/_react.default.createElement(ComposedComponent, props, props.children));
|
|
65
|
-
};
|
|
66
|
-
|
|
67
|
-
Component.displayName = name;
|
|
68
|
-
Composable.original = Component;
|
|
69
|
-
Composable.originalName = name;
|
|
70
|
-
Composable.displayName = "Composable<".concat(name, ">");
|
|
71
|
-
return Composable;
|
|
72
|
-
}
|
|
14
|
+
//# sourceMappingURL=makeComposable.js.map
|
package/makeComposable.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["
|
|
1
|
+
{"version":3,"names":["createContext","makeDecoratable","ComposableContext","displayName","nullRenderer","makeComposable","name","Component"],"sources":["makeComposable.tsx"],"sourcesContent":["import { createContext } from \"react\";\nimport type { GenericComponent } from \"~/types.js\";\nimport { makeDecoratable } from \"~/makeDecoratable.js\";\n\nconst ComposableContext = createContext<string[]>([]);\nComposableContext.displayName = \"ComposableContext\";\n\nconst nullRenderer = () => null;\n\n/**\n * @deprecated Use `makeDecoratable` instead.\n */\nexport function makeComposable<T extends GenericComponent>(name: string, Component?: T) {\n return makeDecoratable(name, Component ?? nullRenderer);\n}\n"],"mappings":"AAAA,SAASA,aAAa,QAAQ,OAAO;AAErC,SAASC,eAAe;AAExB,MAAMC,iBAAiB,gBAAGF,aAAa,CAAW,EAAE,CAAC;AACrDE,iBAAiB,CAACC,WAAW,GAAG,mBAAmB;AAEnD,MAAMC,YAAY,GAAGA,CAAA,KAAM,IAAI;;AAE/B;AACA;AACA;AACA,OAAO,SAASC,cAAcA,CAA6BC,IAAY,EAAEC,SAAa,EAAE;EACpF,OAAON,eAAe,CAACK,IAAI,EAAEC,SAAS,IAAIH,YAAY,CAAC;AAC3D","ignoreList":[]}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import React from "react";
|
|
2
|
+
import type { DecoratableComponent, DecoratableHook, GenericComponent, GenericHook } from "./types.js";
|
|
3
|
+
declare function makeDecoratableComponent<T extends GenericComponent>(name: string, Component?: T): T & {
|
|
4
|
+
original: T;
|
|
5
|
+
originalName: string;
|
|
6
|
+
displayName: string;
|
|
7
|
+
} & {
|
|
8
|
+
original: T & {
|
|
9
|
+
original: T;
|
|
10
|
+
originalName: string;
|
|
11
|
+
displayName: string;
|
|
12
|
+
};
|
|
13
|
+
originalName: string;
|
|
14
|
+
displayName: string;
|
|
15
|
+
} & {
|
|
16
|
+
createDecorator: (decorator: import("~/types.js").ComponentDecorator<import("./createDecorator.js").GetDecoratee<DecoratableComponent<T & {
|
|
17
|
+
original: T;
|
|
18
|
+
originalName: string;
|
|
19
|
+
displayName: string;
|
|
20
|
+
}>>>) => (props: unknown) => React.JSX.Element;
|
|
21
|
+
};
|
|
22
|
+
export declare function makeDecoratableHook<T extends GenericHook>(hook: T): GenericHook<import("./createDecorator.js").GetDecorateeParams<import("./createDecorator.js").GetDecoratee<DecoratableHook<T>>>, ReturnType<import("./createDecorator.js").GetDecoratee<DecoratableHook<T>>>> & {
|
|
23
|
+
original: GenericHook<import("./createDecorator.js").GetDecorateeParams<import("./createDecorator.js").GetDecoratee<DecoratableHook<T>>>, ReturnType<import("./createDecorator.js").GetDecoratee<DecoratableHook<T>>>>;
|
|
24
|
+
originalName: string;
|
|
25
|
+
} & {
|
|
26
|
+
createDecorator: (decorator: import("~/types.js").Decorator<import("./createDecorator.js").GetDecoratee<DecoratableHook<T>>>) => () => React.JSX.Element;
|
|
27
|
+
};
|
|
28
|
+
export declare function createVoidComponent<T>(): (props: T) => JSX.Element | null;
|
|
29
|
+
export declare function makeDecoratable<T extends GenericHook>(hook: T): ReturnType<typeof makeDecoratableHook<T>>;
|
|
30
|
+
export declare function makeDecoratable<T extends GenericComponent>(name: string, Component: T): ReturnType<typeof makeDecoratableComponent<T>>;
|
|
31
|
+
export {};
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import React, { createContext, useContext, useMemo } from "react";
|
|
2
|
+
import { useComponent } from "./Context.js";
|
|
3
|
+
import { withDecoratorFactory, withHookDecoratorFactory } from "./decorators.js";
|
|
4
|
+
const ComposableContext = /*#__PURE__*/createContext([]);
|
|
5
|
+
ComposableContext.displayName = "ComposableContext";
|
|
6
|
+
function useComposableParents() {
|
|
7
|
+
const context = useContext(ComposableContext);
|
|
8
|
+
if (!context) {
|
|
9
|
+
return [];
|
|
10
|
+
}
|
|
11
|
+
return context;
|
|
12
|
+
}
|
|
13
|
+
const nullRenderer = () => null;
|
|
14
|
+
function makeDecoratableComponent(name, Component = nullRenderer) {
|
|
15
|
+
const Decoratable = props => {
|
|
16
|
+
const parents = useComposableParents();
|
|
17
|
+
const ComposedComponent = useComponent(Component);
|
|
18
|
+
const context = useMemo(() => [...parents, name], [parents, name]);
|
|
19
|
+
return /*#__PURE__*/React.createElement(ComposableContext.Provider, {
|
|
20
|
+
value: context
|
|
21
|
+
}, /*#__PURE__*/React.createElement(ComposedComponent, props, props.children));
|
|
22
|
+
};
|
|
23
|
+
const staticProps = {
|
|
24
|
+
original: Component,
|
|
25
|
+
originalName: name,
|
|
26
|
+
displayName: `Decoratable<${name}>`
|
|
27
|
+
};
|
|
28
|
+
return withDecoratorFactory()(Object.assign(Decoratable, staticProps));
|
|
29
|
+
}
|
|
30
|
+
export function makeDecoratableHook(hook) {
|
|
31
|
+
const decoratableHook = params => {
|
|
32
|
+
const composedHook = useComponent(hook);
|
|
33
|
+
return composedHook(params);
|
|
34
|
+
};
|
|
35
|
+
decoratableHook.original = hook;
|
|
36
|
+
return withHookDecoratorFactory()(decoratableHook);
|
|
37
|
+
}
|
|
38
|
+
export function createVoidComponent() {
|
|
39
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
40
|
+
return props => {
|
|
41
|
+
return null;
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
export function makeDecoratable(hookOrName, Component) {
|
|
45
|
+
if (Component) {
|
|
46
|
+
const component = makeDecoratableComponent(hookOrName, /*#__PURE__*/React.memo(Component));
|
|
47
|
+
component.original.displayName = hookOrName;
|
|
48
|
+
return component;
|
|
49
|
+
}
|
|
50
|
+
return makeDecoratableHook(hookOrName);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
//# sourceMappingURL=makeDecoratable.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["React","createContext","useContext","useMemo","useComponent","withDecoratorFactory","withHookDecoratorFactory","ComposableContext","displayName","useComposableParents","context","nullRenderer","makeDecoratableComponent","name","Component","Decoratable","props","parents","ComposedComponent","createElement","Provider","value","children","staticProps","original","originalName","Object","assign","makeDecoratableHook","hook","decoratableHook","params","composedHook","createVoidComponent","makeDecoratable","hookOrName","component","memo"],"sources":["makeDecoratable.tsx"],"sourcesContent":["import React, { createContext, useContext, useMemo } from \"react\";\nimport { useComponent } from \"./Context.js\";\nimport type {\n DecoratableComponent,\n DecoratableHook,\n GenericComponent,\n GenericHook\n} from \"~/types.js\";\nimport { withDecoratorFactory, withHookDecoratorFactory } from \"~/decorators.js\";\n\nconst ComposableContext = createContext<string[]>([]);\nComposableContext.displayName = \"ComposableContext\";\n\nfunction useComposableParents() {\n const context = useContext(ComposableContext);\n if (!context) {\n return [];\n }\n\n return context;\n}\n\nconst nullRenderer = () => null;\n\nfunction makeDecoratableComponent<T extends GenericComponent>(\n name: string,\n Component: T = nullRenderer as unknown as T\n) {\n const Decoratable = (props: React.ComponentProps<T>): JSX.Element | null => {\n const parents = useComposableParents();\n const ComposedComponent = useComponent(Component) as GenericComponent<\n React.ComponentProps<T>\n >;\n\n const context = useMemo(() => [...parents, name], [parents, name]);\n\n return (\n <ComposableContext.Provider value={context}>\n <ComposedComponent {...props}>{props.children}</ComposedComponent>\n </ComposableContext.Provider>\n );\n };\n\n const staticProps = {\n original: Component,\n originalName: name,\n displayName: `Decoratable<${name}>`\n };\n\n return withDecoratorFactory()(\n Object.assign(Decoratable, staticProps) as DecoratableComponent<\n typeof Component & typeof staticProps\n >\n );\n}\n\nexport function makeDecoratableHook<T extends GenericHook>(hook: T) {\n const decoratableHook = (params: Parameters<T>) => {\n const composedHook = useComponent(hook);\n\n return composedHook(params) as DecoratableHook<T>;\n };\n\n decoratableHook.original = hook;\n\n return withHookDecoratorFactory()(decoratableHook as DecoratableHook<T>);\n}\n\nexport function createVoidComponent<T>() {\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\n return (props: T): JSX.Element | null => {\n return null;\n };\n}\n\nexport function makeDecoratable<T extends GenericHook>(\n hook: T\n): ReturnType<typeof makeDecoratableHook<T>>;\nexport function makeDecoratable<T extends GenericComponent>(\n name: string,\n Component: T\n): ReturnType<typeof makeDecoratableComponent<T>>;\nexport function makeDecoratable(hookOrName: any, Component?: any) {\n if (Component) {\n const component = makeDecoratableComponent(hookOrName, React.memo(Component));\n component.original.displayName = hookOrName;\n return component;\n }\n\n return makeDecoratableHook(hookOrName);\n}\n"],"mappings":"AAAA,OAAOA,KAAK,IAAIC,aAAa,EAAEC,UAAU,EAAEC,OAAO,QAAQ,OAAO;AACjE,SAASC,YAAY;AAOrB,SAASC,oBAAoB,EAAEC,wBAAwB;AAEvD,MAAMC,iBAAiB,gBAAGN,aAAa,CAAW,EAAE,CAAC;AACrDM,iBAAiB,CAACC,WAAW,GAAG,mBAAm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AwB,CAAC,CAAC,CAACwB,eAAqC,CAAC;AAC5E;AAEA,OAAO,SAASG,mBAAmBA,CAAA,EAAM;EACrC;EACA,OAAQjB,KAAQ,IAAyB;IACrC,OAAO,IAAI;EACf,CAAC;AACL;AASA,OAAO,SAASkB,eAAeA,CAACC,UAAe,EAAErB,SAAe,EAAE;EAC9D,IAAIA,SAAS,EAAE;IACX,MAAMsB,SAAS,GAAGxB,wBAAwB,CAACuB,UAAU,eAAEnC,KAAK,CAACqC,IAAI,CAACvB,SAAS,CAAC,CAAC;IAC7EsB,SAAS,CAACZ,QAAQ,CAAChB,WAAW,GAAG2B,UAAU;IAC3C,OAAOC,SAAS;EACpB;EAEA,OAAOR,mBAAmB,CAACO,UAAU,CAAC;AAC1C","ignoreList":[]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@webiny/react-composition",
|
|
3
|
-
"version": "0.0.0-unstable.
|
|
3
|
+
"version": "0.0.0-unstable.61c048f412",
|
|
4
|
+
"type": "module",
|
|
4
5
|
"main": "index.js",
|
|
5
6
|
"repository": {
|
|
6
7
|
"type": "git",
|
|
@@ -14,29 +15,19 @@
|
|
|
14
15
|
],
|
|
15
16
|
"license": "MIT",
|
|
16
17
|
"dependencies": {
|
|
17
|
-
"@
|
|
18
|
-
"
|
|
19
|
-
"
|
|
20
|
-
"react": "17.0.2",
|
|
21
|
-
"react-dom": "17.0.2"
|
|
18
|
+
"@types/react": "18.2.79",
|
|
19
|
+
"react": "18.2.0",
|
|
20
|
+
"react-dom": "18.2.0"
|
|
22
21
|
},
|
|
23
22
|
"devDependencies": {
|
|
24
|
-
"@
|
|
25
|
-
"@
|
|
26
|
-
"
|
|
27
|
-
"
|
|
28
|
-
"@webiny/cli": "^0.0.0-unstable.615a930a68",
|
|
29
|
-
"@webiny/project-utils": "^0.0.0-unstable.615a930a68",
|
|
30
|
-
"ttypescript": "^1.5.13",
|
|
31
|
-
"typescript": "4.7.4"
|
|
23
|
+
"@testing-library/react": "15.0.7",
|
|
24
|
+
"@webiny/build-tools": "0.0.0-unstable.61c048f412",
|
|
25
|
+
"typescript": "5.9.3",
|
|
26
|
+
"vitest": "3.2.4"
|
|
32
27
|
},
|
|
33
28
|
"publishConfig": {
|
|
34
29
|
"access": "public",
|
|
35
30
|
"directory": "dist"
|
|
36
31
|
},
|
|
37
|
-
"
|
|
38
|
-
"build": "yarn webiny run build",
|
|
39
|
-
"watch": "yarn webiny run watch"
|
|
40
|
-
},
|
|
41
|
-
"gitHead": "615a930a68d692c832e4f100405eeb3f5a8874af"
|
|
32
|
+
"gitHead": "61c048f412d6b4aa70c1d105aab21e3fa69730f3"
|
|
42
33
|
}
|
package/types.d.ts
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import type React from "react";
|
|
2
|
+
export type GenericHook<TParams = any, TReturn = any> = (...args: TParams[]) => TReturn;
|
|
3
|
+
export type GenericComponent<T = any> = React.FunctionComponent<T>;
|
|
4
|
+
export type ComposedFunction = GenericHook;
|
|
5
|
+
export type Decorator<T> = (decoratee: T) => T;
|
|
6
|
+
/**
|
|
7
|
+
* Some decoratable components will always return `null`, by design.
|
|
8
|
+
* To allow you to decorate these components, we must tell TS that the decorator is allowed to return not just `null`
|
|
9
|
+
* (which is inferred from the component type), but also a JSX.Element.
|
|
10
|
+
*/
|
|
11
|
+
export type ComponentDecorator<T> = (decoratee: T) => CanReturnNullOrElement<T>;
|
|
12
|
+
/**
|
|
13
|
+
* @deprecated
|
|
14
|
+
*/
|
|
15
|
+
export type ComposableFC<T> = T & {
|
|
16
|
+
displayName?: string;
|
|
17
|
+
original: T;
|
|
18
|
+
originalName: string;
|
|
19
|
+
};
|
|
20
|
+
export type Enumerable<T> = T extends Array<infer D> ? Array<D> : never;
|
|
21
|
+
export type ComposeWith = Decorator<GenericComponent> | Decorator<GenericComponent>[] | Decorator<GenericHook> | Decorator<GenericHook>[];
|
|
22
|
+
export type DecoratableHook<T extends GenericHook = GenericHook> = T & {
|
|
23
|
+
original: T;
|
|
24
|
+
originalName: string;
|
|
25
|
+
};
|
|
26
|
+
export type DecoratableComponent<T = GenericComponent> = T & {
|
|
27
|
+
original: T;
|
|
28
|
+
originalName: string;
|
|
29
|
+
displayName: string;
|
|
30
|
+
};
|
|
31
|
+
export type Decoratable = DecoratableComponent | DecoratableHook;
|
|
32
|
+
/**
|
|
33
|
+
* @internal Add `null` to the ReturnType of the given function.
|
|
34
|
+
*/
|
|
35
|
+
export type CanReturnNullOrElement<T> = T extends (...args: any) => any ? (...args: Parameters<T>) => JSX.Element | null : never;
|
package/types.js
ADDED
package/types.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":[],"sources":["types.ts"],"sourcesContent":["import type React from \"react\";\n\nexport type GenericHook<TParams = any, TReturn = any> = (...args: TParams[]) => TReturn;\n\nexport type GenericComponent<T = any> = React.FunctionComponent<T>;\n\nexport type ComposedFunction = GenericHook;\n\nexport type Decorator<T> = (decoratee: T) => T;\n\n/**\n * Some decoratable components will always return `null`, by design.\n * To allow you to decorate these components, we must tell TS that the decorator is allowed to return not just `null`\n * (which is inferred from the component type), but also a JSX.Element.\n */\nexport type ComponentDecorator<T> = (decoratee: T) => CanReturnNullOrElement<T>;\n\n/**\n * @deprecated\n */\nexport type ComposableFC<T> = T & {\n displayName?: string;\n original: T;\n originalName: string;\n};\n\nexport type Enumerable<T> = T extends Array<infer D> ? Array<D> : never;\n\nexport type ComposeWith =\n | Decorator<GenericComponent>\n | Decorator<GenericComponent>[]\n | Decorator<GenericHook>\n | Decorator<GenericHook>[];\n\nexport type DecoratableHook<T extends GenericHook = GenericHook> = T & {\n original: T;\n originalName: string;\n};\n\nexport type DecoratableComponent<T = GenericComponent> = T & {\n original: T;\n originalName: string;\n displayName: string;\n};\n\nexport type Decoratable = DecoratableComponent | DecoratableHook;\n\n/**\n * @internal Add `null` to the ReturnType of the given function.\n */\nexport type CanReturnNullOrElement<T> = T extends (...args: any) => any\n ? (...args: Parameters<T>) => JSX.Element | null\n : never;\n"],"mappings":"","ignoreList":[]}
|
package/createComponentPlugin.js
DELETED
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
|
|
4
|
-
|
|
5
|
-
Object.defineProperty(exports, "__esModule", {
|
|
6
|
-
value: true
|
|
7
|
-
});
|
|
8
|
-
exports.createComponentPlugin = createComponentPlugin;
|
|
9
|
-
|
|
10
|
-
var _react = _interopRequireDefault(require("react"));
|
|
11
|
-
|
|
12
|
-
var _index = require("./index");
|
|
13
|
-
|
|
14
|
-
function createComponentPlugin(Base, hoc) {
|
|
15
|
-
var ComponentPlugin = function ComponentPlugin() {
|
|
16
|
-
return /*#__PURE__*/_react.default.createElement(_index.Compose, {
|
|
17
|
-
component: Base,
|
|
18
|
-
with: hoc
|
|
19
|
-
});
|
|
20
|
-
};
|
|
21
|
-
|
|
22
|
-
ComponentPlugin.displayName = Base.displayName;
|
|
23
|
-
return ComponentPlugin;
|
|
24
|
-
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"names":["createComponentPlugin","Base","hoc","ComponentPlugin","displayName"],"sources":["createComponentPlugin.tsx"],"sourcesContent":["import React, { ComponentProps } from \"react\";\nimport { ComposableFC, Compose, HigherOrderComponent } from \"./index\";\n\nexport function createComponentPlugin<T extends ComposableFC<ComponentProps<T>>>(\n Base: T,\n hoc: HigherOrderComponent<ComponentProps<T>>\n): React.FC {\n const ComponentPlugin = () => <Compose component={Base} with={hoc} />;\n ComponentPlugin.displayName = Base.displayName;\n return ComponentPlugin;\n}\n"],"mappings":";;;;;;;;;AAAA;;AACA;;AAEO,SAASA,qBAAT,CACHC,IADG,EAEHC,GAFG,EAGK;EACR,IAAMC,eAAe,GAAG,SAAlBA,eAAkB;IAAA,oBAAM,6BAAC,cAAD;MAAS,SAAS,EAAEF,IAApB;MAA0B,IAAI,EAAEC;IAAhC,EAAN;EAAA,CAAxB;;EACAC,eAAe,CAACC,WAAhB,GAA8BH,IAAI,CAACG,WAAnC;EACA,OAAOD,eAAP;AACH"}
|