@webiny/react-composition 0.0.0-unstable.c2780f51fe → 0.0.0-unstable.c7dec08bb0

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 CHANGED
@@ -4,25 +4,21 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.Compose = void 0;
7
-
8
7
  var _react = require("react");
9
-
10
8
  var _Context = require("./Context");
11
-
9
+ var _CompositionScope = require("./CompositionScope");
12
10
  var Compose = function Compose(props) {
13
11
  var _useComposition = (0, _Context.useComposition)(),
14
- composeComponent = _useComposition.composeComponent;
15
-
12
+ composeComponent = _useComposition.composeComponent;
13
+ var scope = (0, _CompositionScope.useCompositionScope)();
16
14
  (0, _react.useEffect)(function () {
17
15
  if (typeof props.component.original === "undefined") {
18
16
  console.warn("You must make your component \"<".concat(props.component.displayName, ">\" composable, by using the makeComposable() function!"));
19
17
  return;
20
18
  }
21
-
22
19
  var hocs = Array.isArray(props.with) ? props.with : [props.with];
23
- return composeComponent(props.component.original, hocs);
20
+ return composeComponent(props.component.original, hocs, scope);
24
21
  }, [props.with]);
25
22
  return null;
26
23
  };
27
-
28
24
  exports.Compose = Compose;
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,IAAMA,OAA+B,GAAG,SAAlCA,OAAkC,CAAAC,KAAK,EAAI;EACpD,sBAA6B,IAAAC,uBAAA,GAA7B;EAAA,IAAQC,gBAAR,mBAAQA,gBAAR;;EAEA,IAAAC,gBAAA,EAAU,YAAM;IACZ,IAAI,OAAOH,KAAK,CAACI,SAAN,CAAgBC,QAAvB,KAAoC,WAAxC,EAAqD;MACjDC,OAAO,CAACC,IAAR,2CACsCP,KAAK,CAACI,SAAN,CAAgBI,WADtD;MAIA;IACH;;IAED,IAAMC,IAAI,GAAGC,KAAK,CAACC,OAAN,CAAcX,KAAK,CAACY,IAApB,IAA4BZ,KAAK,CAACY,IAAlC,GAAyC,CAACZ,KAAK,CAACY,IAAP,CAAtD;IACA,OAAOV,gBAAgB,CAACF,KAAK,CAACI,SAAN,CAAgBC,QAAjB,EAA2BI,IAA3B,CAAvB;EACH,CAXD,EAWG,CAACT,KAAK,CAACY,IAAP,CAXH;EAaA,OAAO,IAAP;AACH,CAjBM"}
1
+ {"version":3,"names":["Compose","props","useComposition","composeComponent","scope","useCompositionScope","useEffect","component","original","console","warn","displayName","hocs","Array","isArray","with"],"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;AACA;AACA;AAgBO,IAAMA,OAA+B,GAAG,SAAlCA,OAA+B,CAAGC,KAAK,EAAI;EACpD,sBAA6B,IAAAC,uBAAc,GAAE;IAArCC,gBAAgB,mBAAhBA,gBAAgB;EACxB,IAAMC,KAAK,GAAG,IAAAC,qCAAmB,GAAE;EAEnC,IAAAC,gBAAS,EAAC,YAAM;IACZ,IAAI,OAAOL,KAAK,CAACM,SAAS,CAACC,QAAQ,KAAK,WAAW,EAAE;MACjDC,OAAO,CAACC,IAAI,2CAC0BT,KAAK,CAACM,SAAS,CAACI,WAAW,6DAChE;MAED;IACJ;IAEA,IAAMC,IAAI,GAAGC,KAAK,CAACC,OAAO,CAACb,KAAK,CAACc,IAAI,CAAC,GAAGd,KAAK,CAACc,IAAI,GAAG,CAACd,KAAK,CAACc,IAAI,CAAC;IAClE,OAAOZ,gBAAgB,CAACF,KAAK,CAACM,SAAS,CAACC,QAAQ,EAAEI,IAAI,EAAER,KAAK,CAAC;EAClE,CAAC,EAAE,CAACH,KAAK,CAACc,IAAI,CAAC,CAAC;EAEhB,OAAO,IAAI;AACf,CAAC;AAAC"}
@@ -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":["CompositionScopeContext","React","createContext","undefined","CompositionScope","name","children","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;AAMA,IAAMA,uBAAuB,gBAAGC,cAAK,CAACC,aAAa,CAAsCC,SAAS,CAAC;AAO5F,IAAMC,gBAAgB,GAAG,SAAnBA,gBAAgB,OAAkD;EAAA,IAA5CC,IAAI,QAAJA,IAAI;IAAEC,QAAQ,QAARA,QAAQ;EAC7C,oBACI,6BAAC,uBAAuB,CAAC,QAAQ;IAAC,KAAK,EAAE;MAAED,IAAI,EAAJA;IAAK;EAAE,GAC7CC,QAAQ,CACsB;AAE3C,CAAC;AAAC;AAEK,SAASC,mBAAmB,GAAG;EAClC,IAAMC,OAAO,GAAGP,cAAK,CAACQ,UAAU,CAACT,uBAAuB,CAAC;EACzD,IAAI,CAACQ,OAAO,EAAE;IACV,OAAOL,SAAS;EACpB;EACA,OAAOK,OAAO,CAACH,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: HigherOrderComponent[]): (Base: FC<unknown>) => FC<unknown>;
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: HigherOrderComponent[];
11
+ hocs: Decorator[];
12
+ /**
13
+ * Component composition can be scoped.
14
+ */
15
+ scope?: string;
12
16
  }
13
17
  /**
14
- * IMPORTANT: TInputProps default type is `any` because this interface is use as a prop type in the `Compose` component.
15
- * You can pass any HigherOrderComponent as a prop, regardless of its TInputProps type. The only way to allow that is
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 HigherOrderComponent<TInputProps = any, TOutputProps = TInputProps> {
19
- (Component: FC<TInputProps>): FC<TOutputProps>;
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: ComposedComponents;
24
- getComponent(component: ComponentType<unknown>): ComponentType<unknown> | undefined;
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
@@ -1,9 +1,7 @@
1
1
  "use strict";
2
2
 
3
3
  var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard").default;
4
-
5
4
  var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
6
-
7
5
  Object.defineProperty(exports, "__esModule", {
8
6
  value: true
9
7
  });
@@ -12,54 +10,51 @@ exports.compose = compose;
12
10
  exports.useComponent = useComponent;
13
11
  exports.useComposition = useComposition;
14
12
  exports.useOptionalComposition = useOptionalComposition;
15
-
16
13
  var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
17
-
18
14
  var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
19
-
20
15
  var _react = _interopRequireWildcard(require("react"));
21
-
16
+ var _CompositionScope = require("./CompositionScope");
22
17
  function compose() {
23
18
  for (var _len = arguments.length, fns = new Array(_len), _key = 0; _key < _len; _key++) {
24
19
  fns[_key] = arguments[_key];
25
20
  }
26
-
27
21
  return function ComposedComponent(Base) {
28
22
  return fns.reduceRight(function (Component, hoc) {
29
23
  return hoc(Component);
30
24
  }, Base);
31
25
  };
32
26
  }
33
-
34
27
  var CompositionContext = /*#__PURE__*/(0, _react.createContext)(undefined);
35
-
36
28
  var CompositionProvider = function CompositionProvider(_ref) {
37
29
  var children = _ref.children;
38
-
39
30
  var _useState = (0, _react.useState)(new Map()),
40
- _useState2 = (0, _slicedToArray2.default)(_useState, 2),
41
- components = _useState2[0],
42
- setComponents = _useState2[1];
43
-
31
+ _useState2 = (0, _slicedToArray2.default)(_useState, 2),
32
+ components = _useState2[0],
33
+ setComponents = _useState2[1];
44
34
  var composeComponent = (0, _react.useCallback)(function (component, hocs) {
35
+ var scope = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : "*";
45
36
  setComponents(function (prevComponents) {
46
37
  var components = new Map(prevComponents);
47
- var recipe = components.get(component) || {
38
+ var scopeMap = components.get(scope) || new Map();
39
+ var recipe = scopeMap.get(component) || {
48
40
  component: null,
49
41
  hocs: []
50
42
  };
51
43
  var newHocs = [].concat((0, _toConsumableArray2.default)(recipe.hocs || []), (0, _toConsumableArray2.default)(hocs));
52
- components.set(component, {
44
+ scopeMap.set(component, {
53
45
  component: compose.apply(void 0, (0, _toConsumableArray2.default)((0, _toConsumableArray2.default)(newHocs).reverse()))(component),
54
46
  hocs: newHocs
55
47
  });
48
+ components.set(scope, scopeMap);
56
49
  return components;
57
- }); // Return a function that will remove the added HOCs.
50
+ });
58
51
 
52
+ // Return a function that will remove the added HOCs.
59
53
  return function () {
60
54
  setComponents(function (prevComponents) {
61
55
  var components = new Map(prevComponents);
62
- var recipe = components.get(component) || {
56
+ var scopeMap = components.get(scope) || new Map();
57
+ var recipe = scopeMap.get(component) || {
63
58
  component: null,
64
59
  hocs: []
65
60
  };
@@ -67,16 +62,25 @@ var CompositionProvider = function CompositionProvider(_ref) {
67
62
  return !hocs.includes(hoc);
68
63
  });
69
64
  var NewComponent = compose.apply(void 0, (0, _toConsumableArray2.default)((0, _toConsumableArray2.default)(newHOCs).reverse()))(component);
70
- components.set(component, {
65
+ scopeMap.set(component, {
71
66
  component: NewComponent,
72
67
  hocs: newHOCs
73
68
  });
69
+ components.set(scope, scopeMap);
74
70
  return components;
75
71
  });
76
72
  };
77
73
  }, [setComponents]);
78
74
  var getComponent = (0, _react.useCallback)(function (Component) {
79
- var composedComponent = components.get(Component);
75
+ var scope = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : "*";
76
+ var scopeMap = components.get(scope) || new Map();
77
+ var composedComponent = scopeMap.get(Component);
78
+ if (!composedComponent && scope !== "*") {
79
+ // Check if a default scope component exists
80
+ var defaultScopeMap = components.get("*") || new Map();
81
+ var defaultComponent = defaultScopeMap.get(Component);
82
+ return defaultComponent ? defaultComponent.component : undefined;
83
+ }
80
84
  return composedComponent ? composedComponent.component : undefined;
81
85
  }, [components]);
82
86
  var context = (0, _react.useMemo)(function () {
@@ -90,37 +94,30 @@ var CompositionProvider = function CompositionProvider(_ref) {
90
94
  value: context
91
95
  }, children);
92
96
  };
93
-
94
97
  exports.CompositionProvider = CompositionProvider;
95
-
96
98
  function useComponent(Component) {
97
99
  var context = useOptionalComposition();
98
-
100
+ var scope = (0, _CompositionScope.useCompositionScope)();
99
101
  if (!context) {
100
102
  return Component;
101
103
  }
102
-
103
- return context.getComponent(Component) || Component;
104
+ return context.getComponent(Component, scope) || Component;
104
105
  }
106
+
105
107
  /**
106
108
  * This hook will throw an error if composition context doesn't exist.
107
109
  */
108
-
109
-
110
110
  function useComposition() {
111
111
  var context = (0, _react.useContext)(CompositionContext);
112
-
113
112
  if (!context) {
114
113
  throw new Error("You're missing a <CompositionProvider> higher up in your component hierarchy!");
115
114
  }
116
-
117
115
  return context;
118
116
  }
117
+
119
118
  /**
120
119
  * This hook will not throw an error if composition context doesn't exist.
121
120
  */
122
-
123
-
124
121
  function useOptionalComposition() {
125
122
  return (0, _react.useContext)(CompositionContext);
126
123
  }
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,EAA2D;IAC9D,OAAOF,GAAG,CAACG,WAAJ,CAAgB,UAACC,SAAD,EAAYC,GAAZ;MAAA,OAAoBA,GAAG,CAACD,SAAD,CAAvB;IAAA,CAAhB,EAAoDF,IAApD,CAAP;EACH,CAFD;AAGH;;AA8BD,IAAMI,kBAAkB,gBAAG,IAAAC,oBAAA,EAA8CC,SAA9C,CAA3B;;AAEO,IAAMC,mBAA6B,GAAG,SAAhCA,mBAAgC,OAAkB;EAAA,IAAfC,QAAe,QAAfA,QAAe;;EAC3D,gBAAoC,IAAAC,eAAA,EAA6B,IAAIC,GAAJ,EAA7B,CAApC;EAAA;EAAA,IAAOC,UAAP;EAAA,IAAmBC,aAAnB;;EAEA,IAAMC,gBAAgB,GAAG,IAAAC,kBAAA,EACrB,UAACC,SAAD,EAAYC,IAAZ,EAAqB;IACjBJ,aAAa,CAAC,U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;EAwCA,IAAMe,YAAgD,GAAG,IAAAb,kBAAA,EACrD,UAAAZ,SAAS,EAAI;IACT,IAAM0B,iBAAiB,GAAGjB,UAAU,CAACQ,GAAX,CAAejB,SAAf,CAA1B;IACA,OAAO0B,iBAAiB,GAAGA,iBAAiB,CAACb,SAArB,GAAiCT,SAAzD;EACH,CAJoD,EAKrD,CAACK,UAAD,CALqD,CAAzD;EAQA,IAAMkB,OAA2B,GAAG,IAAAC,cAAA,EAChC;IAAA,OAAO;MACHH,YAAY,EAAZA,YADG;MAEHd,gBAAgB,EAAhBA,gBAFG;MAGHF,UAAU,EAAVA;IAHG,CAAP;EAAA,CADgC,EAMhC,CAACA,UAAD,EAAaE,gBAAb,CANgC,CAApC;EASA,oBAAO,6BAAC,kBAAD,CAAoB,QAApB;IAA6B,KAAK,EAAEgB;EAApC,GAA8CrB,QAA9C,CAAP;AACH,CA7DM;;;;AA+DA,SAASuB,YAAT,CAAsB7B,SAAtB,EAAqD;EACxD,IAAM2B,OAAO,GAAGG,sBAAsB,EAAtC;;EAEA,IAAI,CAACH,OAAL,EAAc;IACV,OAAO3B,SAAP;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":["compose","fns","ComposedComponent","Base","reduceRight","Component","hoc","CompositionContext","createContext","undefined","CompositionProvider","children","useState","Map","components","setComponents","composeComponent","useCallback","component","hocs","scope","prevComponents","scopeMap","get","recipe","newHocs","set","reverse","newHOCs","filter","includes","NewComponent","getComponent","composedComponent","defaultScopeMap","defaultComponent","context","useMemo","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":";;;;;;;;;;;;;;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
1
1
  "use strict";
2
2
 
3
3
  var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
4
-
5
4
  Object.defineProperty(exports, "__esModule", {
6
5
  value: true
7
6
  });
8
7
  exports.createComponentPlugin = createComponentPlugin;
9
-
10
8
  var _react = _interopRequireDefault(require("react"));
11
-
12
9
  var _index = require("./index");
13
-
14
10
  /**
15
11
  * Creates a component which, when mounted, registers a Higher Order Component for the given base component.
16
12
  * This is particularly useful for decorating (wrapping) existing composable components.
@@ -23,7 +19,6 @@ function createComponentPlugin(Base, hoc) {
23
19
  with: hoc
24
20
  });
25
21
  };
26
-
27
22
  ComponentPlugin.displayName = Base.displayName;
28
23
  return ComponentPlugin;
29
24
  }
@@ -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,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"}
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,SAASA,qBAAqB,CACjCC,IAAO,EACPC,GAA4C,EACpC;EACR,IAAMC,eAAe,GAAG,SAAlBA,eAAe;IAAA,oBAAS,6BAAC,cAAO;MAAC,SAAS,EAAEF,IAAK;MAAC,IAAI,EAAEC;IAAI,EAAG;EAAA;EACrEC,eAAe,CAACC,WAAW,GAAGH,IAAI,CAACG,WAAW;EAC9C,OAAOD,eAAe;AAC1B"}
@@ -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":["createConditionalDecorator","shouldDecorate","decorator","decoratorProps","Original","ShouldDecorate","props","Component","createDecoratorFactory","from","createDecorator","DecoratorPlugin"],"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;AAEA;AAMO,SAASA,0BAA0B,CACtCC,cAA8B,EAC9BC,SAAoB,EACpBC,cAAuB,EACd;EACT,OAAO,UAACC,QAAkB,EAAK;IAC3B,OAAO,SAASC,cAAc,CAACC,KAAK,EAAE;MAClC,IAAIL,cAAc,CAACE,cAAc,EAAEG,KAAK,CAAC,EAAE;QACvC,IAAMC,SAAS,GAAGL,SAAS,CAACE,QAAQ,CAAC;QACrC,oBAAO,6BAAC,SAAS,EAAKE,KAAK,CAAI;MACnC;MAEA,oBAAO,6BAAC,QAAQ,EAAKA,KAAK,CAAI;IAClC,CAAC;EACL,CAAC;AACL;AAEO,SAASE,sBAAsB,GAAe;EACjD,OAAO,SAASC,IAAI,CAChBF,SAAmC,EACnCN,cAAuD,EACzD;IACE,OAAO,SAASS,eAAe,CAACR,SAAgC,EAAE;MAC9D,OAAO,SAASS,eAAe,CAACL,KAAiB,EAAE;QAC/C,IAAIL,cAAc,EAAE;UAChB,oBACI,6BAAC,gBAAO;YACJ,SAAS,EAAEM,SAAU;YACrB,IAAI,EAAEP,0BAA0B,CAACC,cAAc,EAAEC,SAAS,EAAEI,KAAK;UAAE,EACrE;QAEV;QACA,oBAAO,6BAAC,gBAAO;UAAC,SAAS,EAAEC,SAAU;UAAC,IAAI,EAAEL;QAAU,EAAG;MAC7D,CAAC;IACL,CAAC;EACL,CAAC;AACL"}
package/index.d.ts CHANGED
@@ -2,3 +2,5 @@ export * from "./Context";
2
2
  export * from "./Compose";
3
3
  export * from "./makeComposable";
4
4
  export * from "./createComponentPlugin";
5
+ export * from "./decorators";
6
+ export * from "./CompositionScope";
package/index.js CHANGED
@@ -3,9 +3,7 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
-
7
6
  var _Context = require("./Context");
8
-
9
7
  Object.keys(_Context).forEach(function (key) {
10
8
  if (key === "default" || key === "__esModule") return;
11
9
  if (key in exports && exports[key] === _Context[key]) return;
@@ -16,9 +14,7 @@ Object.keys(_Context).forEach(function (key) {
16
14
  }
17
15
  });
18
16
  });
19
-
20
17
  var _Compose = require("./Compose");
21
-
22
18
  Object.keys(_Compose).forEach(function (key) {
23
19
  if (key === "default" || key === "__esModule") return;
24
20
  if (key in exports && exports[key] === _Compose[key]) return;
@@ -29,9 +25,7 @@ Object.keys(_Compose).forEach(function (key) {
29
25
  }
30
26
  });
31
27
  });
32
-
33
28
  var _makeComposable = require("./makeComposable");
34
-
35
29
  Object.keys(_makeComposable).forEach(function (key) {
36
30
  if (key === "default" || key === "__esModule") return;
37
31
  if (key in exports && exports[key] === _makeComposable[key]) return;
@@ -42,9 +36,7 @@ Object.keys(_makeComposable).forEach(function (key) {
42
36
  }
43
37
  });
44
38
  });
45
-
46
39
  var _createComponentPlugin = require("./createComponentPlugin");
47
-
48
40
  Object.keys(_createComponentPlugin).forEach(function (key) {
49
41
  if (key === "default" || key === "__esModule") return;
50
42
  if (key in exports && exports[key] === _createComponentPlugin[key]) return;
@@ -54,4 +46,26 @@ Object.keys(_createComponentPlugin).forEach(function (key) {
54
46
  return _createComponentPlugin[key];
55
47
  }
56
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
+ });
57
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;;AAAA;EAAA;EAAA;EAAA;IAAA;IAAA;MAAA;IAAA;EAAA;AAAA;;AACA;;AAAA;EAAA;EAAA;EAAA;IAAA;IAAA;MAAA;IAAA;EAAA;AAAA;;AACA;;AAAA;EAAA;EAAA;EAAA;IAAA;IAAA;MAAA;IAAA;EAAA;AAAA;;AACA;;AAAA;EAAA;EAAA;EAAA;IAAA;IAAA;MAAA;IAAA;EAAA;AAAA"}
1
+ {"version":3,"names":[],"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;AAAA;EAAA;EAAA;EAAA;IAAA;IAAA;MAAA;IAAA;EAAA;AAAA;AACA;AAAA;EAAA;EAAA;EAAA;IAAA;IAAA;MAAA;IAAA;EAAA;AAAA;AACA;AAAA;EAAA;EAAA;EAAA;IAAA;IAAA;MAAA;IAAA;EAAA;AAAA;AACA;AAAA;EAAA;EAAA;EAAA;IAAA;IAAA;MAAA;IAAA;EAAA;AAAA;AACA;AAAA;EAAA;EAAA;EAAA;IAAA;IAAA;MAAA;IAAA;EAAA;AAAA;AACA;AAAA;EAAA;EAAA;EAAA;IAAA;IAAA;MAAA;IAAA;EAAA;AAAA"}
package/makeComposable.js CHANGED
@@ -1,37 +1,25 @@
1
1
  "use strict";
2
2
 
3
3
  var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard").default;
4
-
5
4
  var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
6
-
7
5
  Object.defineProperty(exports, "__esModule", {
8
6
  value: true
9
7
  });
10
8
  exports.makeComposable = makeComposable;
11
-
12
9
  var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
13
-
14
10
  var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
15
-
16
11
  var _react = _interopRequireWildcard(require("react"));
17
-
18
12
  var _lodash = _interopRequireDefault(require("lodash.debounce"));
19
-
20
13
  var _Context = require("./Context");
21
-
22
14
  var ComposableContext = /*#__PURE__*/(0, _react.createContext)([]);
23
15
  ComposableContext.displayName = "ComposableContext";
24
-
25
16
  function useComposableParents() {
26
17
  var context = (0, _react.useContext)(ComposableContext);
27
-
28
18
  if (!context) {
29
19
  return [];
30
20
  }
31
-
32
21
  return context;
33
22
  }
34
-
35
23
  var createEmptyRenderer = function createEmptyRenderer(name) {
36
24
  return (0, _defineProperty2.default)({}, name, function () {
37
25
  (0, _react.useEffect)(function () {
@@ -47,12 +35,10 @@ var createEmptyRenderer = function createEmptyRenderer(name) {
47
35
  return null;
48
36
  })[name];
49
37
  };
50
-
51
38
  function makeComposable(name, Component) {
52
39
  if (!Component) {
53
40
  Component = createEmptyRenderer(name);
54
41
  }
55
-
56
42
  var Composable = function Composable(props) {
57
43
  var parents = useComposableParents();
58
44
  var ComposedComponent = (0, _Context.useComponent)(Component);
@@ -63,7 +49,6 @@ function makeComposable(name, Component) {
63
49
  value: context
64
50
  }, /*#__PURE__*/_react.default.createElement(ComposedComponent, props, props.children));
65
51
  };
66
-
67
52
  Component.displayName = name;
68
53
  Composable.original = Component;
69
54
  Composable.originalName = name;
@@ -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.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;;AACA;;AAEA;;AAEA,IAAMA,iBAAiB,gBAAG,IAAAC,oBAAA,EAAwB,EAAxB,CAA1B;AACAD,iBAAiB,CAACE,WAAlB,GAAgC,mBAAhC;;AAEA,SAASC,oBAAT,GAAgC;EAC5B,IAAMC,OAAO,GAAG,IAAAC,iBAAA,EAAWL,iBAAX,CAAhB;;EACA,IAAI,CAACI,OAAL,EAAc;IACV,OAAO,EAA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
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.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;AACA;AAEA;AAEA,IAAMA,iBAAiB,gBAAG,IAAAC,oBAAa,EAAW,EAAE,CAAC;AACrDD,iBAAiB,CAACE,WAAW,GAAG,mBAAmB;AAEnD,SAASC,oBAAoB,GAAG;EAC5B,IAAMC,OAAO,GAAG,IAAAC,iB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C9BS,UAAU,CAACd,WAAW,wBAAiBK,IAAI,MAAG;EAE9C,OAAOS,UAAU;AACrB"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@webiny/react-composition",
3
- "version": "0.0.0-unstable.c2780f51fe",
3
+ "version": "0.0.0-unstable.c7dec08bb0",
4
4
  "main": "index.js",
5
5
  "repository": {
6
6
  "type": "git",
@@ -21,13 +21,13 @@
21
21
  "react-dom": "17.0.2"
22
22
  },
23
23
  "devDependencies": {
24
- "@babel/cli": "^7.19.3",
25
- "@babel/core": "^7.19.3",
26
- "@babel/preset-env": "^7.19.4",
27
- "@babel/preset-typescript": "^7.18.6",
28
- "@webiny/cli": "^0.0.0-unstable.c2780f51fe",
29
- "@webiny/project-utils": "^0.0.0-unstable.c2780f51fe",
30
- "ttypescript": "^1.5.13",
24
+ "@babel/cli": "7.20.7",
25
+ "@babel/core": "7.20.12",
26
+ "@babel/preset-env": "7.20.2",
27
+ "@babel/preset-typescript": "7.18.6",
28
+ "@webiny/cli": "0.0.0-unstable.c7dec08bb0",
29
+ "@webiny/project-utils": "0.0.0-unstable.c7dec08bb0",
30
+ "ttypescript": "1.5.15",
31
31
  "typescript": "4.7.4"
32
32
  },
33
33
  "publishConfig": {
@@ -38,5 +38,5 @@
38
38
  "build": "yarn webiny run build",
39
39
  "watch": "yarn webiny run watch"
40
40
  },
41
- "gitHead": "c2780f51fe628e1ef0f6b55c2bce9b74d3470d79"
41
+ "gitHead": "c7dec08bb095467cca6a61381bd399d66b7bb147"
42
42
  }