@webiny/react-composition 5.36.2 → 5.37.0-beta.1
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.js +3 -1
- package/Compose.js.map +1 -1
- package/CompositionScope.d.ts +11 -0
- package/CompositionScope.js +27 -0
- package/CompositionScope.js.map +1 -0
- package/Context.d.ts +20 -9
- package/Context.js +32 -6
- package/Context.js.map +1 -1
- package/createComponentPlugin.js.map +1 -1
- package/decorators.d.ts +9 -0
- package/decorators.js +39 -0
- package/decorators.js.map +1 -0
- package/index.d.ts +2 -0
- package/index.js +22 -0
- package/index.js.map +1 -1
- package/makeComposable.js +2 -2
- package/makeComposable.js.map +1 -1
- package/package.json +10 -10
package/Compose.js
CHANGED
|
@@ -6,16 +6,18 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
6
6
|
exports.Compose = void 0;
|
|
7
7
|
var _react = require("react");
|
|
8
8
|
var _Context = require("./Context");
|
|
9
|
+
var _CompositionScope = require("./CompositionScope");
|
|
9
10
|
var Compose = function Compose(props) {
|
|
10
11
|
var _useComposition = (0, _Context.useComposition)(),
|
|
11
12
|
composeComponent = _useComposition.composeComponent;
|
|
13
|
+
var scope = (0, _CompositionScope.useCompositionScope)();
|
|
12
14
|
(0, _react.useEffect)(function () {
|
|
13
15
|
if (typeof props.component.original === "undefined") {
|
|
14
16
|
console.warn("You must make your component \"<".concat(props.component.displayName, ">\" composable, by using the makeComposable() function!"));
|
|
15
17
|
return;
|
|
16
18
|
}
|
|
17
19
|
var hocs = Array.isArray(props.with) ? props.with : [props.with];
|
|
18
|
-
return composeComponent(props.component.original, hocs);
|
|
20
|
+
return composeComponent(props.component.original, hocs, scope);
|
|
19
21
|
}, [props.with]);
|
|
20
22
|
return null;
|
|
21
23
|
};
|
package/Compose.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["Compose","props","useComposition","composeComponent","useEffect","component","original","console","warn","displayName","hocs","Array","isArray","with"],"sources":["Compose.tsx"],"sourcesContent":["import React, { useEffect } from \"react\";\nimport { HigherOrderComponent, useComposition } from \"./Context\";\n\nexport interface ComposableFC<TProps = unknown> extends React.FC<TProps> {\n original: React.FC<TProps>;\n originalName: string;\n}\n\nexport interface ComposeProps {\n /**\n * Component to compose.\n * NOTE: ComposableFC<TProps> use `any` because the type of the component props is irrelevant.\n */\n component: ComposableFC<any>;\n with: HigherOrderComponent | HigherOrderComponent[];\n}\n\nexport const Compose: React.FC<ComposeProps> = props => {\n const { composeComponent } = useComposition();\n\n useEffect(() => {\n if (typeof props.component.original === \"undefined\") {\n console.warn(\n `You must make your component \"<${props.component.displayName}>\" composable, by using the makeComposable() function!`\n );\n\n return;\n }\n\n const hocs = Array.isArray(props.with) ? props.with : [props.with];\n return composeComponent(props.component.original, hocs);\n }, [props.with]);\n\n return null;\n};\n"],"mappings":";;;;;;AAAA;AACA;AAgBO,
|
|
1
|
+
{"version":3,"names":["_react","require","_Context","_CompositionScope","Compose","props","_useComposition","useComposition","composeComponent","scope","useCompositionScope","useEffect","component","original","console","warn","concat","displayName","hocs","Array","isArray","with","exports"],"sources":["Compose.tsx"],"sourcesContent":["import React, { useEffect } from \"react\";\nimport { HigherOrderComponent, useComposition } from \"./Context\";\nimport { useCompositionScope } from \"~/CompositionScope\";\n\nexport interface ComposableFC<TProps = unknown> extends React.FC<TProps> {\n original: React.FC<TProps>;\n originalName: string;\n}\n\nexport interface ComposeProps {\n /**\n * Component to compose.\n * NOTE: ComposableFC<TProps> use `any` because the type of the component props is irrelevant.\n */\n component: ComposableFC<any>;\n with: HigherOrderComponent | HigherOrderComponent[];\n}\n\nexport const Compose: React.FC<ComposeProps> = props => {\n const { composeComponent } = useComposition();\n const scope = useCompositionScope();\n\n useEffect(() => {\n if (typeof props.component.original === \"undefined\") {\n console.warn(\n `You must make your component \"<${props.component.displayName}>\" composable, by using the makeComposable() function!`\n );\n\n return;\n }\n\n const hocs = Array.isArray(props.with) ? props.with : [props.with];\n return composeComponent(props.component.original, hocs, scope);\n }, [props.with]);\n\n return null;\n};\n"],"mappings":";;;;;;AAAA,IAAAA,MAAA,GAAAC,OAAA;AACA,IAAAC,QAAA,GAAAD,OAAA;AACA,IAAAE,iBAAA,GAAAF,OAAA;AAgBO,IAAMG,OAA+B,GAAG,SAAlCA,OAA+BA,CAAGC,KAAK,EAAI;EACpD,IAAAC,eAAA,GAA6B,IAAAC,uBAAc,EAAC,CAAC;IAArCC,gBAAgB,GAAAF,eAAA,CAAhBE,gBAAgB;EACxB,IAAMC,KAAK,GAAG,IAAAC,qCAAmB,EAAC,CAAC;EAEnC,IAAAC,gBAAS,EAAC,YAAM;IACZ,IAAI,OAAON,KAAK,CAACO,SAAS,CAACC,QAAQ,KAAK,WAAW,EAAE;MACjDC,OAAO,CAACC,IAAI,oCAAAC,MAAA,CAC0BX,KAAK,CAACO,SAAS,CAACK,WAAW,4DACjE,CAAC;MAED;IACJ;IAEA,IAAMC,IAAI,GAAGC,KAAK,CAACC,OAAO,CAACf,KAAK,CAACgB,IAAI,CAAC,GAAGhB,KAAK,CAACgB,IAAI,GAAG,CAAChB,KAAK,CAACgB,IAAI,CAAC;IAClE,OAAOb,gBAAgB,CAACH,KAAK,CAACO,SAAS,CAACC,QAAQ,EAAEK,IAAI,EAAET,KAAK,CAAC;EAClE,CAAC,EAAE,CAACJ,KAAK,CAACgB,IAAI,CAAC,CAAC;EAEhB,OAAO,IAAI;AACf,CAAC;AAACC,OAAA,CAAAlB,OAAA,GAAAA,OAAA"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import React from "react";
|
|
2
|
+
export interface CompositionScopeContext {
|
|
3
|
+
name: string;
|
|
4
|
+
}
|
|
5
|
+
interface CompositionScopeProps {
|
|
6
|
+
name: string;
|
|
7
|
+
children: React.ReactNode;
|
|
8
|
+
}
|
|
9
|
+
export declare const CompositionScope: ({ name, children }: CompositionScopeProps) => JSX.Element;
|
|
10
|
+
export declare function useCompositionScope(): string | undefined;
|
|
11
|
+
export {};
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
|
|
4
|
+
Object.defineProperty(exports, "__esModule", {
|
|
5
|
+
value: true
|
|
6
|
+
});
|
|
7
|
+
exports.CompositionScope = void 0;
|
|
8
|
+
exports.useCompositionScope = useCompositionScope;
|
|
9
|
+
var _react = _interopRequireDefault(require("react"));
|
|
10
|
+
var CompositionScopeContext = /*#__PURE__*/_react.default.createContext(undefined);
|
|
11
|
+
var CompositionScope = function CompositionScope(_ref) {
|
|
12
|
+
var name = _ref.name,
|
|
13
|
+
children = _ref.children;
|
|
14
|
+
return /*#__PURE__*/_react.default.createElement(CompositionScopeContext.Provider, {
|
|
15
|
+
value: {
|
|
16
|
+
name: name
|
|
17
|
+
}
|
|
18
|
+
}, children);
|
|
19
|
+
};
|
|
20
|
+
exports.CompositionScope = CompositionScope;
|
|
21
|
+
function useCompositionScope() {
|
|
22
|
+
var context = _react.default.useContext(CompositionScopeContext);
|
|
23
|
+
if (!context) {
|
|
24
|
+
return undefined;
|
|
25
|
+
}
|
|
26
|
+
return context.name;
|
|
27
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["_react","_interopRequireDefault","require","CompositionScopeContext","React","createContext","undefined","CompositionScope","_ref","name","children","default","createElement","Provider","value","exports","useCompositionScope","context","useContext"],"sources":["CompositionScope.tsx"],"sourcesContent":["import React from \"react\";\n\nexport interface CompositionScopeContext {\n name: string;\n}\n\nconst CompositionScopeContext = React.createContext<CompositionScopeContext | undefined>(undefined);\n\ninterface CompositionScopeProps {\n name: string;\n children: React.ReactNode;\n}\n\nexport const CompositionScope = ({ name, children }: CompositionScopeProps) => {\n return (\n <CompositionScopeContext.Provider value={{ name }}>\n {children}\n </CompositionScopeContext.Provider>\n );\n};\n\nexport function useCompositionScope() {\n const context = React.useContext(CompositionScopeContext);\n if (!context) {\n return undefined;\n }\n return context.name;\n}\n"],"mappings":";;;;;;;;AAAA,IAAAA,MAAA,GAAAC,sBAAA,CAAAC,OAAA;AAMA,IAAMC,uBAAuB,gBAAGC,cAAK,CAACC,aAAa,CAAsCC,SAAS,CAAC;AAO5F,IAAMC,gBAAgB,GAAG,SAAnBA,gBAAgBA,CAAAC,IAAA,EAAkD;EAAA,IAA5CC,IAAI,GAAAD,IAAA,CAAJC,IAAI;IAAEC,QAAQ,GAAAF,IAAA,CAARE,QAAQ;EAC7C,oBACIV,MAAA,CAAAW,OAAA,CAAAC,aAAA,CAACT,uBAAuB,CAACU,QAAQ;IAACC,KAAK,EAAE;MAAEL,IAAI,EAAJA;IAAK;EAAE,GAC7CC,QAC6B,CAAC;AAE3C,CAAC;AAACK,OAAA,CAAAR,gBAAA,GAAAA,gBAAA;AAEK,SAASS,mBAAmBA,CAAA,EAAG;EAClC,IAAMC,OAAO,GAAGb,cAAK,CAACc,UAAU,CAACf,uBAAuB,CAAC;EACzD,IAAI,CAACc,OAAO,EAAE;IACV,OAAOX,SAAS;EACpB;EACA,OAAOW,OAAO,CAACR,IAAI;AACvB"}
|
package/Context.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import React, { FC, ComponentType } from "react";
|
|
2
|
-
export declare function compose(...fns:
|
|
2
|
+
export declare function compose(...fns: Decorator[]): (Base: FC<unknown>) => FC<unknown>;
|
|
3
3
|
interface ComposedComponent {
|
|
4
4
|
/**
|
|
5
5
|
* Ready to use React component.
|
|
@@ -8,21 +8,32 @@ interface ComposedComponent {
|
|
|
8
8
|
/**
|
|
9
9
|
* HOCs used to compose the original component.
|
|
10
10
|
*/
|
|
11
|
-
hocs:
|
|
11
|
+
hocs: Decorator[];
|
|
12
|
+
/**
|
|
13
|
+
* Component composition can be scoped.
|
|
14
|
+
*/
|
|
15
|
+
scope?: string;
|
|
12
16
|
}
|
|
13
17
|
/**
|
|
14
|
-
* IMPORTANT:
|
|
15
|
-
* You can pass any
|
|
18
|
+
* IMPORTANT: TProps default type is `any` because this interface is use as a prop type in the `Compose` component.
|
|
19
|
+
* You can pass any Decorator as a prop, regardless of its TProps type. The only way to allow that is
|
|
16
20
|
* to let it be `any` in this interface.
|
|
17
21
|
*/
|
|
18
|
-
export interface
|
|
19
|
-
(Component: FC<
|
|
22
|
+
export interface Decorator<TProps = any> {
|
|
23
|
+
(Component: FC<TProps>): FC<TProps>;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* @deprecated Use `Decorator` instead.
|
|
27
|
+
*/
|
|
28
|
+
export interface HigherOrderComponent<TProps = any, TOutput = TProps> {
|
|
29
|
+
(Component: FC<TProps>): FC<TOutput>;
|
|
20
30
|
}
|
|
21
31
|
declare type ComposedComponents = Map<ComponentType<unknown>, ComposedComponent>;
|
|
32
|
+
declare type ComponentScopes = Map<string, ComposedComponents>;
|
|
22
33
|
interface CompositionContext {
|
|
23
|
-
components:
|
|
24
|
-
getComponent(component: ComponentType<unknown
|
|
25
|
-
composeComponent(component: ComponentType<unknown>, hocs: HigherOrderComponent[]): void;
|
|
34
|
+
components: ComponentScopes;
|
|
35
|
+
getComponent(component: ComponentType<unknown>, scope?: string): ComponentType<unknown> | undefined;
|
|
36
|
+
composeComponent(component: ComponentType<unknown>, hocs: HigherOrderComponent[], scope?: string): void;
|
|
26
37
|
}
|
|
27
38
|
declare const CompositionContext: React.Context<CompositionContext | undefined>;
|
|
28
39
|
export declare const CompositionProvider: React.FC;
|
package/Context.js
CHANGED
|
@@ -13,6 +13,7 @@ exports.useOptionalComposition = useOptionalComposition;
|
|
|
13
13
|
var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
|
|
14
14
|
var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
|
|
15
15
|
var _react = _interopRequireWildcard(require("react"));
|
|
16
|
+
var _CompositionScope = require("./CompositionScope");
|
|
16
17
|
function compose() {
|
|
17
18
|
for (var _len = arguments.length, fns = new Array(_len), _key = 0; _key < _len; _key++) {
|
|
18
19
|
fns[_key] = arguments[_key];
|
|
@@ -23,6 +24,17 @@ function compose() {
|
|
|
23
24
|
}, Base);
|
|
24
25
|
};
|
|
25
26
|
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* IMPORTANT: TProps default type is `any` because this interface is use as a prop type in the `Compose` component.
|
|
30
|
+
* You can pass any Decorator as a prop, regardless of its TProps type. The only way to allow that is
|
|
31
|
+
* to let it be `any` in this interface.
|
|
32
|
+
*/
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* @deprecated Use `Decorator` instead.
|
|
36
|
+
*/
|
|
37
|
+
|
|
26
38
|
var CompositionContext = /*#__PURE__*/(0, _react.createContext)(undefined);
|
|
27
39
|
var CompositionProvider = function CompositionProvider(_ref) {
|
|
28
40
|
var children = _ref.children;
|
|
@@ -31,17 +43,20 @@ var CompositionProvider = function CompositionProvider(_ref) {
|
|
|
31
43
|
components = _useState2[0],
|
|
32
44
|
setComponents = _useState2[1];
|
|
33
45
|
var composeComponent = (0, _react.useCallback)(function (component, hocs) {
|
|
46
|
+
var scope = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : "*";
|
|
34
47
|
setComponents(function (prevComponents) {
|
|
35
48
|
var components = new Map(prevComponents);
|
|
36
|
-
var
|
|
49
|
+
var scopeMap = components.get(scope) || new Map();
|
|
50
|
+
var recipe = scopeMap.get(component) || {
|
|
37
51
|
component: null,
|
|
38
52
|
hocs: []
|
|
39
53
|
};
|
|
40
54
|
var newHocs = [].concat((0, _toConsumableArray2.default)(recipe.hocs || []), (0, _toConsumableArray2.default)(hocs));
|
|
41
|
-
|
|
55
|
+
scopeMap.set(component, {
|
|
42
56
|
component: compose.apply(void 0, (0, _toConsumableArray2.default)((0, _toConsumableArray2.default)(newHocs).reverse()))(component),
|
|
43
57
|
hocs: newHocs
|
|
44
58
|
});
|
|
59
|
+
components.set(scope, scopeMap);
|
|
45
60
|
return components;
|
|
46
61
|
});
|
|
47
62
|
|
|
@@ -49,7 +64,8 @@ var CompositionProvider = function CompositionProvider(_ref) {
|
|
|
49
64
|
return function () {
|
|
50
65
|
setComponents(function (prevComponents) {
|
|
51
66
|
var components = new Map(prevComponents);
|
|
52
|
-
var
|
|
67
|
+
var scopeMap = components.get(scope) || new Map();
|
|
68
|
+
var recipe = scopeMap.get(component) || {
|
|
53
69
|
component: null,
|
|
54
70
|
hocs: []
|
|
55
71
|
};
|
|
@@ -57,16 +73,25 @@ var CompositionProvider = function CompositionProvider(_ref) {
|
|
|
57
73
|
return !hocs.includes(hoc);
|
|
58
74
|
});
|
|
59
75
|
var NewComponent = compose.apply(void 0, (0, _toConsumableArray2.default)((0, _toConsumableArray2.default)(newHOCs).reverse()))(component);
|
|
60
|
-
|
|
76
|
+
scopeMap.set(component, {
|
|
61
77
|
component: NewComponent,
|
|
62
78
|
hocs: newHOCs
|
|
63
79
|
});
|
|
80
|
+
components.set(scope, scopeMap);
|
|
64
81
|
return components;
|
|
65
82
|
});
|
|
66
83
|
};
|
|
67
84
|
}, [setComponents]);
|
|
68
85
|
var getComponent = (0, _react.useCallback)(function (Component) {
|
|
69
|
-
var
|
|
86
|
+
var scope = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : "*";
|
|
87
|
+
var scopeMap = components.get(scope) || new Map();
|
|
88
|
+
var composedComponent = scopeMap.get(Component);
|
|
89
|
+
if (!composedComponent && scope !== "*") {
|
|
90
|
+
// Check if a default scope component exists
|
|
91
|
+
var defaultScopeMap = components.get("*") || new Map();
|
|
92
|
+
var defaultComponent = defaultScopeMap.get(Component);
|
|
93
|
+
return defaultComponent ? defaultComponent.component : undefined;
|
|
94
|
+
}
|
|
70
95
|
return composedComponent ? composedComponent.component : undefined;
|
|
71
96
|
}, [components]);
|
|
72
97
|
var context = (0, _react.useMemo)(function () {
|
|
@@ -83,10 +108,11 @@ var CompositionProvider = function CompositionProvider(_ref) {
|
|
|
83
108
|
exports.CompositionProvider = CompositionProvider;
|
|
84
109
|
function useComponent(Component) {
|
|
85
110
|
var context = useOptionalComposition();
|
|
111
|
+
var scope = (0, _CompositionScope.useCompositionScope)();
|
|
86
112
|
if (!context) {
|
|
87
113
|
return Component;
|
|
88
114
|
}
|
|
89
|
-
return context.getComponent(Component) || Component;
|
|
115
|
+
return context.getComponent(Component, scope) || Component;
|
|
90
116
|
}
|
|
91
117
|
|
|
92
118
|
/**
|
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,OAAO,GAAiC;EAAA,kCAA7BC,GAAG;IAAHA,GAAG;EAAA;EAC1B,OAAO,SA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AACvD;;AAEA;AACA;AACA;AACO,SAAS+B,cAAc,GAAG;EAC7B,IAAMJ,OAAO,GAAG,IAAAK,iBAAU,EAAC9B,kBAAkB,CAAC;EAC9C,IAAI,CAACyB,OAAO,EAAE;IACV,MAAM,IAAIM,KAAK,iFAEd;EACL;EAEA,OAAON,OAAO;AAClB;;AAEA;AACA;AACA;AACO,SAASG,sBAAsB,GAAG;EACrC,OAAO,IAAAE,iBAAU,EAAC9B,kBAAkB,CAAC;AACzC"}
|
|
1
|
+
{"version":3,"names":["_react","_interopRequireWildcard","require","_CompositionScope","compose","_len","arguments","length","fns","Array","_key","ComposedComponent","Base","reduceRight","Component","hoc","CompositionContext","createContext","undefined","CompositionProvider","_ref","children","_useState","useState","Map","_useState2","_slicedToArray2","default","components","setComponents","composeComponent","useCallback","component","hocs","scope","prevComponents","scopeMap","get","recipe","newHocs","concat","_toConsumableArray2","set","apply","reverse","newHOCs","filter","includes","NewComponent","getComponent","composedComponent","defaultScopeMap","defaultComponent","context","useMemo","createElement","Provider","value","exports","useComponent","useOptionalComposition","useCompositionScope","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\";\nimport { useCompositionScope } from \"~/CompositionScope\";\n\nexport function compose(...fns: Decorator[]) {\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: Decorator[];\n /**\n * Component composition can be scoped.\n */\n scope?: string;\n}\n\n/**\n * IMPORTANT: TProps default type is `any` because this interface is use as a prop type in the `Compose` component.\n * You can pass any Decorator as a prop, regardless of its TProps type. The only way to allow that is\n * to let it be `any` in this interface.\n */\nexport interface Decorator<TProps = any> {\n (Component: FC<TProps>): FC<TProps>;\n}\n\n/**\n * @deprecated Use `Decorator` instead.\n */\nexport interface HigherOrderComponent<TProps = any, TOutput = TProps> {\n (Component: FC<TProps>): FC<TOutput>;\n}\n\ntype ComposedComponents = Map<ComponentType<unknown>, ComposedComponent>;\ntype ComponentScopes = Map<string, ComposedComponents>;\n\ninterface CompositionContext {\n components: ComponentScopes;\n getComponent(\n component: ComponentType<unknown>,\n scope?: string\n ): ComponentType<unknown> | undefined;\n composeComponent(\n component: ComponentType<unknown>,\n hocs: HigherOrderComponent[],\n scope?: string\n ): void;\n}\n\nconst CompositionContext = createContext<CompositionContext | undefined>(undefined);\n\nexport const CompositionProvider: React.FC = ({ children }) => {\n const [components, setComponents] = useState<ComponentScopes>(new Map());\n\n const composeComponent = useCallback(\n (component, hocs, scope = \"*\") => {\n setComponents(prevComponents => {\n const components = new Map(prevComponents);\n const scopeMap: ComposedComponents = components.get(scope) || new Map();\n const recipe = scopeMap.get(component) || { component: null, hocs: [] };\n\n const newHocs = [...(recipe.hocs || []), ...hocs];\n\n scopeMap.set(component, {\n component: compose(...[...newHocs].reverse())(component),\n hocs: newHocs\n });\n\n components.set(scope, scopeMap);\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 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: CompositionContext[\"getComponent\"] = useCallback(\n (Component, scope = \"*\") => {\n const scopeMap: ComposedComponents = components.get(scope) || new Map();\n const composedComponent = scopeMap.get(Component);\n if (!composedComponent && scope !== \"*\") {\n // Check if a default scope component exists\n const defaultScopeMap: ComposedComponents = components.get(\"*\") || new Map();\n const defaultComponent = defaultScopeMap.get(Component);\n return defaultComponent ? defaultComponent.component : undefined;\n }\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 const scope = useCompositionScope();\n\n if (!context) {\n return Component;\n }\n\n return context.getComponent(Component, scope) || 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,IAAAA,MAAA,GAAAC,uBAAA,CAAAC,OAAA;AASA,IAAAC,iBAAA,GAAAD,OAAA;AAEO,SAASE,OAAOA,CAAA,EAAsB;EAAA,SAAAC,IAAA,GAAAC,SAAA,CAAAC,MAAA,EAAlBC,GAAG,OAAAC,KAAA,CAAAJ,IAAA,GAAAK,IAAA,MAAAA,IAAA,GAAAL,IAAA,EAAAK,IAAA;IAAHF,GAAG,CAAAE,IAAA,IAAAJ,SAAA,CAAAI,IAAA;EAAA;EAC1B,OAAO,SAASC,iBAAiBA,CAACC,IAAiB,EAAe;IAC9D,OAAOJ,GAAG,CAACK,WAAW,CAAC,UAACC,SAAS,EAAEC,GAAG;MAAA,OAAKA,GAAG,CAACD,SAAS,CAAC;IAAA,GAAEF,IAAI,CAAC;EACpE,CAAC;AACL;;AAiBA;AACA;AACA;AACA;AACA;;AAKA;AACA;AACA;;AAqBA,IAAMI,kBAAkB,gBAAG,IAAAC,oBAAa,EAAiCC,SAAS,CAAC;AAE5E,IAAMC,mBAA6B,GAAG,S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7B,IAAMT,OAAO,GAAG,IAAAU,iBAAU,EAAC/C,kBAAkB,CAAC;EAC9C,IAAI,CAACqC,OAAO,EAAE;IACV,MAAM,IAAIW,KAAK,gFAEf,CAAC;EACL;EAEA,OAAOX,OAAO;AAClB;;AAEA;AACA;AACA;AACO,SAASO,sBAAsBA,CAAA,EAAG;EACrC,OAAO,IAAAG,iBAAU,EAAC/C,kBAAkB,CAAC;AACzC"}
|
|
@@ -1 +1 @@
|
|
|
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\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 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;AAEA;AACA;AACA;AACA;AACA;AACO,
|
|
1
|
+
{"version":3,"names":["_react","_interopRequireDefault","require","_index","createComponentPlugin","Base","hoc","ComponentPlugin","default","createElement","Compose","component","with","displayName"],"sources":["createComponentPlugin.tsx"],"sourcesContent":["import React, { ComponentProps } from \"react\";\nimport { ComposableFC, Compose, HigherOrderComponent } from \"./index\";\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 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,IAAAA,MAAA,GAAAC,sBAAA,CAAAC,OAAA;AACA,IAAAC,MAAA,GAAAD,OAAA;AAEA;AACA;AACA;AACA;AACA;AACO,SAASE,qBAAqBA,CACjCC,IAAO,EACPC,GAA4C,EACpC;EACR,IAAMC,eAAe,GAAG,SAAlBA,eAAeA,CAAA;IAAA,oBAASP,MAAA,CAAAQ,OAAA,CAAAC,aAAA,CAACN,MAAA,CAAAO,OAAO;MAACC,SAAS,EAAEN,IAAK;MAACO,IAAI,EAAEN;IAAI,CAAE,CAAC;EAAA;EACrEC,eAAe,CAACM,WAAW,GAAGR,IAAI,CAACQ,WAAW;EAC9C,OAAON,eAAe;AAC1B"}
|
package/decorators.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/// <reference types="react" />
|
|
2
|
+
import { Decorator } from "./Context";
|
|
3
|
+
import { ComposableFC } from "./Compose";
|
|
4
|
+
interface ShouldDecorate<TDecorator = any, TComponent = any> {
|
|
5
|
+
(decoratorProps: TDecorator, componentProps: TComponent): boolean;
|
|
6
|
+
}
|
|
7
|
+
export declare function createConditionalDecorator(shouldDecorate: ShouldDecorate, decorator: Decorator, decoratorProps: unknown): Decorator;
|
|
8
|
+
export declare function createDecoratorFactory<TDecorator>(): <TComponent>(Component: ComposableFC<TComponent>, shouldDecorate?: ShouldDecorate<TDecorator, TComponent> | undefined) => (decorator: Decorator<TComponent>) => (props: TDecorator) => JSX.Element;
|
|
9
|
+
export {};
|
package/decorators.js
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
|
|
4
|
+
Object.defineProperty(exports, "__esModule", {
|
|
5
|
+
value: true
|
|
6
|
+
});
|
|
7
|
+
exports.createConditionalDecorator = createConditionalDecorator;
|
|
8
|
+
exports.createDecoratorFactory = createDecoratorFactory;
|
|
9
|
+
var _react = _interopRequireDefault(require("react"));
|
|
10
|
+
var _Compose = require("./Compose");
|
|
11
|
+
function createConditionalDecorator(shouldDecorate, decorator, decoratorProps) {
|
|
12
|
+
return function (Original) {
|
|
13
|
+
return function ShouldDecorate(props) {
|
|
14
|
+
if (shouldDecorate(decoratorProps, props)) {
|
|
15
|
+
var Component = decorator(Original);
|
|
16
|
+
return /*#__PURE__*/_react.default.createElement(Component, props);
|
|
17
|
+
}
|
|
18
|
+
return /*#__PURE__*/_react.default.createElement(Original, props);
|
|
19
|
+
};
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
function createDecoratorFactory() {
|
|
23
|
+
return function from(Component, shouldDecorate) {
|
|
24
|
+
return function createDecorator(decorator) {
|
|
25
|
+
return function DecoratorPlugin(props) {
|
|
26
|
+
if (shouldDecorate) {
|
|
27
|
+
return /*#__PURE__*/_react.default.createElement(_Compose.Compose, {
|
|
28
|
+
component: Component,
|
|
29
|
+
with: createConditionalDecorator(shouldDecorate, decorator, props)
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
return /*#__PURE__*/_react.default.createElement(_Compose.Compose, {
|
|
33
|
+
component: Component,
|
|
34
|
+
with: decorator
|
|
35
|
+
});
|
|
36
|
+
};
|
|
37
|
+
};
|
|
38
|
+
};
|
|
39
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["_react","_interopRequireDefault","require","_Compose","createConditionalDecorator","shouldDecorate","decorator","decoratorProps","Original","ShouldDecorate","props","Component","default","createElement","createDecoratorFactory","from","createDecorator","DecoratorPlugin","Compose","component","with"],"sources":["decorators.tsx"],"sourcesContent":["import React from \"react\";\nimport { Decorator } from \"~/Context\";\nimport { ComposableFC, Compose } from \"~/Compose\";\n\ninterface ShouldDecorate<TDecorator = any, TComponent = any> {\n (decoratorProps: TDecorator, componentProps: TComponent): boolean;\n}\n\nexport function createConditionalDecorator(\n shouldDecorate: ShouldDecorate,\n decorator: Decorator,\n decoratorProps: unknown\n): Decorator {\n return (Original: React.FC) => {\n return function ShouldDecorate(props) {\n if (shouldDecorate(decoratorProps, props)) {\n const Component = decorator(Original);\n return <Component {...props} />;\n }\n\n return <Original {...props} />;\n };\n };\n}\n\nexport function createDecoratorFactory<TDecorator>() {\n return function from<TComponent>(\n Component: ComposableFC<TComponent>,\n shouldDecorate?: ShouldDecorate<TDecorator, TComponent>\n ) {\n return function createDecorator(decorator: Decorator<TComponent>) {\n return function DecoratorPlugin(props: TDecorator) {\n if (shouldDecorate) {\n return (\n <Compose\n component={Component}\n with={createConditionalDecorator(shouldDecorate, decorator, props)}\n />\n );\n }\n return <Compose component={Component} with={decorator} />;\n };\n };\n };\n}\n"],"mappings":";;;;;;;;AAAA,IAAAA,MAAA,GAAAC,sBAAA,CAAAC,OAAA;AAEA,IAAAC,QAAA,GAAAD,OAAA;AAMO,SAASE,0BAA0BA,CACtCC,cAA8B,EAC9BC,SAAoB,EACpBC,cAAuB,EACd;EACT,OAAO,UAACC,QAAkB,EAAK;IAC3B,OAAO,SAASC,cAAcA,CAACC,KAAK,EAAE;MAClC,IAAIL,cAAc,CAACE,cAAc,EAAEG,KAAK,CAAC,EAAE;QACvC,IAAMC,SAAS,GAAGL,SAAS,CAACE,QAAQ,CAAC;QACrC,oBAAOR,MAAA,CAAAY,OAAA,CAAAC,aAAA,CAACF,SAAS,EAAKD,KAAQ,CAAC;MACnC;MAEA,oBAAOV,MAAA,CAAAY,OAAA,CAAAC,aAAA,CAACL,QAAQ,EAAKE,KAAQ,CAAC;IAClC,CAAC;EACL,CAAC;AACL;AAEO,SAASI,sBAAsBA,CAAA,EAAe;EACjD,OAAO,SAASC,IAAIA,CAChBJ,SAAmC,EACnCN,cAAuD,EACzD;IACE,OAAO,SAASW,eAAeA,CAACV,SAAgC,EAAE;MAC9D,OAAO,SAASW,eAAeA,CAACP,KAAiB,EAAE;QAC/C,IAAIL,cAAc,EAAE;UAChB,oBACIL,MAAA,CAAAY,OAAA,CAAAC,aAAA,CAACV,QAAA,CAAAe,OAAO;YACJC,SAAS,EAAER,SAAU;YACrBS,IAAI,EAAEhB,0BAA0B,CAACC,cAAc,EAAEC,SAAS,EAAEI,KAAK;UAAE,CACtE,CAAC;QAEV;QACA,oBAAOV,MAAA,CAAAY,OAAA,CAAAC,aAAA,CAACV,QAAA,CAAAe,OAAO;UAACC,SAAS,EAAER,SAAU;UAACS,IAAI,EAAEd;QAAU,CAAE,CAAC;MAC7D,CAAC;IACL,CAAC;EACL,CAAC;AACL"}
|
package/index.d.ts
CHANGED
package/index.js
CHANGED
|
@@ -46,4 +46,26 @@ Object.keys(_createComponentPlugin).forEach(function (key) {
|
|
|
46
46
|
return _createComponentPlugin[key];
|
|
47
47
|
}
|
|
48
48
|
});
|
|
49
|
+
});
|
|
50
|
+
var _decorators = require("./decorators");
|
|
51
|
+
Object.keys(_decorators).forEach(function (key) {
|
|
52
|
+
if (key === "default" || key === "__esModule") return;
|
|
53
|
+
if (key in exports && exports[key] === _decorators[key]) return;
|
|
54
|
+
Object.defineProperty(exports, key, {
|
|
55
|
+
enumerable: true,
|
|
56
|
+
get: function get() {
|
|
57
|
+
return _decorators[key];
|
|
58
|
+
}
|
|
59
|
+
});
|
|
60
|
+
});
|
|
61
|
+
var _CompositionScope = require("./CompositionScope");
|
|
62
|
+
Object.keys(_CompositionScope).forEach(function (key) {
|
|
63
|
+
if (key === "default" || key === "__esModule") return;
|
|
64
|
+
if (key in exports && exports[key] === _CompositionScope[key]) return;
|
|
65
|
+
Object.defineProperty(exports, key, {
|
|
66
|
+
enumerable: true,
|
|
67
|
+
get: function get() {
|
|
68
|
+
return _CompositionScope[key];
|
|
69
|
+
}
|
|
70
|
+
});
|
|
49
71
|
});
|
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 \"./createComponentPlugin\";\n"],"mappings":";;;;;AAAA;
|
|
1
|
+
{"version":3,"names":["_Context","require","Object","keys","forEach","key","exports","defineProperty","enumerable","get","_Compose","_makeComposable","_createComponentPlugin","_decorators","_CompositionScope"],"sources":["index.ts"],"sourcesContent":["export * from \"./Context\";\nexport * from \"./Compose\";\nexport * from \"./makeComposable\";\nexport * from \"./createComponentPlugin\";\nexport * from \"./decorators\";\nexport * from \"./CompositionScope\";\n"],"mappings":";;;;;AAAA,IAAAA,QAAA,GAAAC,OAAA;AAAAC,MAAA,CAAAC,IAAA,CAAAH,QAAA,EAAAI,OAAA,WAAAC,GAAA;EAAA,IAAAA,GAAA,kBAAAA,GAAA;EAAA,IAAAA,GAAA,IAAAC,OAAA,IAAAA,OAAA,CAAAD,GAAA,MAAAL,QAAA,CAAAK,GAAA;EAAAH,MAAA,CAAAK,cAAA,CAAAD,OAAA,EAAAD,GAAA;IAAAG,UAAA;IAAAC,GAAA,WAAAA,IAAA;MAAA,OAAAT,QAAA,CAAAK,GAAA;IAAA;EAAA;AAAA;AACA,IAAAK,QAAA,GAAAT,OAAA;AAAAC,MAAA,CAAAC,IAAA,CAAAO,QAAA,EAAAN,OAAA,WAAAC,GAAA;EAAA,IAAAA,GAAA,kBAAAA,GAAA;EAAA,IAAAA,GAAA,IAAAC,OAAA,IAAAA,OAAA,CAAAD,GAAA,MAAAK,QAAA,CAAAL,GAAA;EAAAH,MAAA,CAAAK,cAAA,CAAAD,OAAA,EAAAD,GAAA;IAAAG,UAAA;IAAAC,GAAA,WAAAA,IAAA;MAAA,OAAAC,QAAA,CAAAL,GAAA;IAAA;EAAA;AAAA;AACA,IAAAM,eAAA,GAAAV,OAAA;AAAAC,MAAA,CAAAC,IAAA,CAAAQ,eAAA,EAAAP,OAAA,WAAAC,GAAA;EAAA,IAAAA,GAAA,kBAAAA,GAAA;EAAA,IAAAA,GAAA,IAAAC,OAAA,IAAAA,OAAA,CAAAD,GAAA,MAAAM,eAAA,C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AA;EAAA;AAAA"}
|
package/makeComposable.js
CHANGED
|
@@ -9,7 +9,7 @@ exports.makeComposable = makeComposable;
|
|
|
9
9
|
var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
|
|
10
10
|
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
|
|
11
11
|
var _react = _interopRequireWildcard(require("react"));
|
|
12
|
-
var
|
|
12
|
+
var _debounce = _interopRequireDefault(require("lodash/debounce"));
|
|
13
13
|
var _Context = require("./Context");
|
|
14
14
|
var ComposableContext = /*#__PURE__*/(0, _react.createContext)([]);
|
|
15
15
|
ComposableContext.displayName = "ComposableContext";
|
|
@@ -25,7 +25,7 @@ var createEmptyRenderer = function createEmptyRenderer(name) {
|
|
|
25
25
|
(0, _react.useEffect)(function () {
|
|
26
26
|
// We need to debounce the log, as it sometimes only requires a single tick to get the new
|
|
27
27
|
// composed component to render, and we don't want to scare developers for no reason.
|
|
28
|
-
var debounced = (0,
|
|
28
|
+
var debounced = (0, _debounce.default)(function () {
|
|
29
29
|
console.info("<".concat(name, "/> is not implemented! To provide an implementation, use the <Compose/> component."));
|
|
30
30
|
}, 100);
|
|
31
31
|
return function () {
|
package/makeComposable.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["ComposableContext","createContext","displayName","useComposableParents","context","useContext","createEmptyRenderer","name","useEffect","debounced","debounce","console","info","cancel","makeComposable","Component","Composable","props","parents","ComposedComponent","useComponent","useMemo","children","original","originalName"],"sources":["makeComposable.tsx"],"sourcesContent":["import React, { createContext, useContext, useEffect, useMemo } from \"react\";\nimport debounce from \"lodash
|
|
1
|
+
{"version":3,"names":["_react","_interopRequireWildcard","require","_debounce","_interopRequireDefault","_Context","ComposableContext","createContext","displayName","useComposableParents","context","useContext","createEmptyRenderer","name","_defineProperty2","default","useEffect","debounced","debounce","console","info","concat","cancel","makeComposable","Component","Composable","props","parents","ComposedComponent","useComponent","useMemo","_toConsumableArray2","createElement","Provider","value","children","original","originalName"],"sources":["makeComposable.tsx"],"sourcesContent":["import React, { createContext, useContext, useEffect, useMemo } from \"react\";\nimport debounce from \"lodash/debounce\";\nimport { ComposableFC } from \"./Compose\";\nimport { useComponent } from \"./Context\";\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 createEmptyRenderer = (name: string): React.FC => {\n return {\n [name]: function () {\n useEffect(() => {\n // We need to debounce the log, as it sometimes only requires a single tick to get the new\n // composed component to render, and we don't want to scare developers for no reason.\n const debounced = debounce(() => {\n console.info(\n `<${name}/> is not implemented! To provide an implementation, use the <Compose/> component.`\n );\n }, 100);\n\n return () => {\n debounced.cancel();\n };\n }, []);\n\n return null;\n }\n }[name];\n};\n\nexport function makeComposable<TProps>(name: string, Component?: React.FC<TProps>) {\n if (!Component) {\n Component = createEmptyRenderer(name);\n }\n\n const Composable: ComposableFC<TProps> = props => {\n const parents = useComposableParents();\n const ComposedComponent = useComponent(Component as React.FC<TProps>);\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 Component.displayName = name;\n\n Composable.original = Component;\n Composable.originalName = name;\n Composable.displayName = `Composable<${name}>`;\n\n return Composable;\n}\n"],"mappings":";;;;;;;;;;AAAA,IAAAA,MAAA,GAAAC,uBAAA,CAAAC,OAAA;AACA,IAAAC,SAAA,GAAAC,sBAAA,CAAAF,OAAA;AAEA,IAAAG,QAAA,GAAAH,OAAA;AAEA,IAAMI,iBAAiB,gBAAG,IAAAC,oBAAa,EAAW,EAAE,CAAC;AACrDD,iBAAiB,CAACE,WAAW,GAAG,mBAAmB;AAEnD,SAASC,oBAAoBA,CAAA,EAAG;EAC5B,IAAMC,OAAO,GAAG,IAAAC,i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AAK,EAAGA,KAAK,CAACS,QAA4B,CACzC,CAAC;EAErC,CAAC;EAEDX,SAAS,CAAChB,WAAW,GAAGK,IAAI;EAE5BY,UAAU,CAACW,QAAQ,GAAGZ,SAAS;EAC/BC,UAAU,CAACY,YAAY,GAAGxB,IAAI;EAC9BY,UAAU,CAACjB,WAAW,iBAAAa,MAAA,CAAiBR,IAAI,MAAG;EAE9C,OAAOY,UAAU;AACrB"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@webiny/react-composition",
|
|
3
|
-
"version": "5.
|
|
3
|
+
"version": "5.37.0-beta.1",
|
|
4
4
|
"main": "index.js",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -14,19 +14,19 @@
|
|
|
14
14
|
],
|
|
15
15
|
"license": "MIT",
|
|
16
16
|
"dependencies": {
|
|
17
|
-
"@babel/runtime": "7.
|
|
17
|
+
"@babel/runtime": "7.22.6",
|
|
18
18
|
"@types/react": "17.0.39",
|
|
19
|
-
"lodash
|
|
19
|
+
"lodash": "4.17.21",
|
|
20
20
|
"react": "17.0.2",
|
|
21
21
|
"react-dom": "17.0.2"
|
|
22
22
|
},
|
|
23
23
|
"devDependencies": {
|
|
24
|
-
"@babel/cli": "7.
|
|
25
|
-
"@babel/core": "7.
|
|
26
|
-
"@babel/preset-env": "7.
|
|
27
|
-
"@babel/preset-typescript": "7.
|
|
28
|
-
"@webiny/cli": "5.
|
|
29
|
-
"@webiny/project-utils": "5.
|
|
24
|
+
"@babel/cli": "7.22.6",
|
|
25
|
+
"@babel/core": "7.22.8",
|
|
26
|
+
"@babel/preset-env": "7.22.7",
|
|
27
|
+
"@babel/preset-typescript": "7.22.5",
|
|
28
|
+
"@webiny/cli": "5.37.0-beta.1",
|
|
29
|
+
"@webiny/project-utils": "5.37.0-beta.1",
|
|
30
30
|
"ttypescript": "1.5.15",
|
|
31
31
|
"typescript": "4.7.4"
|
|
32
32
|
},
|
|
@@ -38,5 +38,5 @@
|
|
|
38
38
|
"build": "yarn webiny run build",
|
|
39
39
|
"watch": "yarn webiny run watch"
|
|
40
40
|
},
|
|
41
|
-
"gitHead": "
|
|
41
|
+
"gitHead": "176c29425477d1daaca1b7da79bacc79a1d35f6e"
|
|
42
42
|
}
|