@webiny/react-composition 5.43.3 → 6.0.0-alpha.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.d.ts CHANGED
@@ -1,5 +1,5 @@
1
- import { DecoratableTypes } from "./Context";
2
- import { ComposeWith } from "./types";
1
+ import type { DecoratableTypes } from "./Context";
2
+ import type { ComposeWith } from "./types";
3
3
  export interface ComposeProps {
4
4
  function?: DecoratableTypes;
5
5
  component?: DecoratableTypes;
package/Compose.js CHANGED
@@ -1,24 +1,25 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.Compose = void 0;
7
- var _react = require("react");
8
- var _Context = require("./Context");
9
- var _CompositionScope = require("./CompositionScope");
10
- var Compose = exports.Compose = function Compose(props) {
11
- var _useComposition = (0, _Context.useComposition)(),
12
- composeComponent = _useComposition.composeComponent;
13
- var scope = (0, _CompositionScope.useCompositionScope)();
14
- var targetFn = props.function ?? props.component;
15
- (0, _react.useEffect)(function () {
1
+ import { useEffect } from "react";
2
+ import { useComposition } from "./Context";
3
+ import { useCompositionScope } from "./CompositionScope";
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
+ }
16
17
  if (typeof targetFn.original === "undefined") {
17
- console.warn("You must make your function \"".concat(targetFn.originalName ?? targetFn.name, "\" composable, by using the makeDecoratable() function!"));
18
+ console.warn(`You must make your function "${targetFn.originalName ?? targetFn.name}" composable, by using the makeDecoratable() function!`);
18
19
  return;
19
20
  }
20
- var decorators = Array.isArray(props.with) ? props.with : [props.with];
21
- return composeComponent(targetFn.original, decorators, scope[scope.length - 1]);
21
+ const decorators = Array.isArray(props.with) ? props.with : [props.with];
22
+ return composeComponent(targetFn.original, decorators, scope[scope.length - 1], inherit);
22
23
  }, [props.with]);
23
24
  return null;
24
25
  };
package/Compose.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"names":["_react","require","_Context","_CompositionScope","Compose","exports","props","_useComposition","useComposition","composeComponent","scope","useCompositionScope","targetFn","function","component","useEffect","original","console","warn","concat","originalName","name","decorators","Array","isArray","with","length"],"sources":["Compose.tsx"],"sourcesContent":["import { useEffect } from \"react\";\nimport { DecoratableTypes, useComposition } from \"./Context\";\nimport { useCompositionScope } from \"~/CompositionScope\";\nimport { ComposeWith, Decoratable, Enumerable } from \"./types\";\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 = useCompositionScope();\n\n const targetFn = (props.function ?? props.component) as Decoratable;\n\n useEffect(() => {\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 );\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;AASO,IAAMG,OAAO,GAAAC,OAAA,CAAAD,OAAA,GAAG,SAAVA,OAAOA,CAAIE,KAAmB,EAAK;EAC5C,IAAAC,eAAA,GAA6B,IAAAC,uBAAc,EAAC,CAAC;IAArCC,gBAAgB,GAAAF,eAAA,CAAhBE,gBAAgB;EACxB,IAAMC,KAAK,GAAG,IAAAC,qCAAmB,EAAC,CAAC;EAEnC,IAAMC,QAAQ,GAAIN,KAAK,CAACO,QAAQ,IAAIP,KAAK,CAACQ,SAAyB;EAEnE,IAAAC,gBAAS,EAAC,YAAM;IACZ,IAAI,OAAOH,QAAQ,CAACI,QAAQ,KAAK,WAAW,EAAE;MAC1CC,OAAO,CAACC,IAAI,kCAAAC,MAAA,CAEJP,QAAQ,CAACQ,YAAY,IAAIR,QAAQ,CAACS,IAAI,4DAE9C,CAAC;MAED;IACJ;IAEA,IAAMC,UAAU,GAAGC,KAAK,CAACC,OAAO,CAAClB,KAAK,CAACmB,IAAI,CAAC,GAAGnB,KAAK,CAACmB,IAAI,GAAG,CAACnB,KAAK,CAACmB,IAAI,CAAC;IACxE,OAAOhB,gBAAgB,CACnBG,QAAQ,CAACI,QAAQ,EACjBM,UAAU,EACVZ,KAAK,CAACA,KAAK,CAACgB,MAAM,GAAG,CAAC,CAC1B,CAAC;EACL,CAAC,EAAE,CAACpB,KAAK,CAACmB,IAAI,CAAC,CAAC;EAEhB,OAAO,IAAI;AACf,CAAC","ignoreList":[]}
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\";\nimport { useComposition } from \"./Context\";\nimport { useCompositionScope } from \"~/CompositionScope\";\nimport type { ComposeWith, Decoratable, Enumerable } from \"./types\";\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":[]}
@@ -1,11 +1,17 @@
1
1
  import React from "react";
2
2
  export interface CompositionScopeContext {
3
+ inherit: boolean;
3
4
  scope: string[];
4
5
  }
5
6
  interface CompositionScopeProps {
6
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;
7
13
  children: React.ReactNode;
8
14
  }
9
- export declare const CompositionScope: ({ name, children }: CompositionScopeProps) => React.JSX.Element;
10
- export declare function useCompositionScope(): string[];
15
+ export declare const CompositionScope: ({ name, inherit, children }: CompositionScopeProps) => React.JSX.Element;
16
+ export declare function useCompositionScope(): CompositionScopeContext;
11
17
  export {};
@@ -1,30 +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 _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
10
- var _react = _interopRequireDefault(require("react"));
11
- var CompositionScopeContext = /*#__PURE__*/_react.default.createContext(undefined);
12
- var CompositionScope = exports.CompositionScope = function CompositionScope(_ref) {
13
- var name = _ref.name,
14
- children = _ref.children;
15
- var parentScope = useCompositionScope();
16
- return /*#__PURE__*/_react.default.createElement(CompositionScopeContext.Provider, {
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, {
17
10
  value: {
18
- scope: [].concat((0, _toConsumableArray2.default)(parentScope), [name])
11
+ scope: [...parentScope.scope, name],
12
+ inherit
19
13
  }
20
14
  }, children);
21
15
  };
22
- function useCompositionScope() {
23
- var context = _react.default.useContext(CompositionScopeContext);
16
+ export function useCompositionScope() {
17
+ const context = React.useContext(CompositionScopeContext);
24
18
  if (!context) {
25
- return [];
19
+ return {
20
+ scope: [],
21
+ inherit: false
22
+ };
26
23
  }
27
- return context.scope;
24
+ return context;
28
25
  }
29
26
 
30
27
  //# sourceMappingURL=CompositionScope.js.map
@@ -1 +1 @@
1
- {"version":3,"names":["_react","_interopRequireDefault","require","CompositionScopeContext","React","createContext","undefined","CompositionScope","exports","_ref","name","children","parentScope","useCompositionScope","default","createElement","Provider","value","scope","concat","_toConsumableArray2","context","useContext"],"sources":["CompositionScope.tsx"],"sourcesContent":["import React from \"react\";\n\nexport interface CompositionScopeContext {\n scope: 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 const parentScope = useCompositionScope();\n\n return (\n <CompositionScopeContext.Provider value={{ scope: [...parentScope, name] }}>\n {children}\n </CompositionScopeContext.Provider>\n );\n};\n\nexport function useCompositionScope() {\n const context = React.useContext(CompositionScopeContext);\n if (!context) {\n return [];\n }\n return context.scope;\n}\n"],"mappings":";;;;;;;;;AAAA,IAAAA,MAAA,GAAAC,sBAAA,CAAAC,OAAA;AAMA,IAAMC,uBAAuB,gBAAGC,cAAK,CAACC,aAAa,CAAsCC,SAAS,CAAC;AAO5F,IAAMC,gBAAgB,GAAAC,OAAA,CAAAD,gBAAA,GAAG,SAAnBA,gBAAgBA,CAAAE,IAAA,EAAkD;EAAA,IAA5CC,IAAI,GAAAD,IAAA,CAAJC,IAAI;IAAEC,QAAQ,GAAAF,IAAA,CAARE,QAAQ;EAC7C,IAAMC,WAAW,GAAGC,mBAAmB,CAAC,CAAC;EAEzC,oBACIb,MAAA,CAAAc,OAAA,CAAAC,aAAA,CAACZ,uBAAuB,CAACa,QAAQ;IAACC,KAAK,EAAE;MAAEC,KAAK,KAAAC,MAAA,KAAAC,mBAAA,CAAAN,OAAA,EAAMF,WAAW,IAAEF,IAAI;IAAE;EAAE,GACtEC,QAC6B,CAAC;AAE3C,CAAC;AAEM,SAASE,mBAAmBA,CAAA,EAAG;EAClC,IAAMQ,OAAO,GAAGjB,cAAK,CAACkB,UAAU,CAACnB,uBAAuB,CAAC;EACzD,IAAI,CAACkB,OAAO,EAAE;IACV,OAAO,EAAE;EACb;EACA,OAAOA,OAAO,CAACH,KAAK;AACxB","ignoreList":[]}
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,5 +1,6 @@
1
- import React, { ComponentType } from "react";
2
- import { ComposedFunction, ComposeWith, Decoratable, DecoratableComponent, DecoratableHook, Decorator, Enumerable, GenericComponent, GenericHook } from "./types";
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";
3
4
  export declare function compose<T>(...fns: Decorator<T>[]): (decoratee: T) => T;
4
5
  interface ComposedComponent {
5
6
  /**
@@ -30,7 +31,7 @@ interface CompositionContextGetComponentCallable {
30
31
  interface CompositionContext {
31
32
  components: ComponentScopes;
32
33
  getComponent: CompositionContextGetComponentCallable;
33
- composeComponent(component: ComponentType<unknown>, hocs: Enumerable<ComposeWith>, scope?: string): void;
34
+ composeComponent(component: ComponentType<unknown>, hocs: Enumerable<ComposeWith>, scope?: string, inherit?: boolean): void;
34
35
  }
35
36
  declare const CompositionContext: React.Context<CompositionContext | undefined>;
36
37
  export type DecoratorsTuple = [Decoratable, Decorator<any>[]];
package/Context.js CHANGED
@@ -1,28 +1,8 @@
1
- "use strict";
2
-
3
- var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard").default;
4
- var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
5
- Object.defineProperty(exports, "__esModule", {
6
- value: true
7
- });
8
- exports.CompositionProvider = void 0;
9
- exports.compose = compose;
10
- exports.useComponent = useComponent;
11
- exports.useComposition = useComposition;
12
- exports.useOptionalComposition = useOptionalComposition;
13
- var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
14
- var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
15
- var _createForOfIteratorHelper2 = _interopRequireDefault(require("@babel/runtime/helpers/createForOfIteratorHelper"));
16
- var _react = _interopRequireWildcard(require("react"));
17
- var _CompositionScope = require("./CompositionScope");
18
- function compose() {
19
- for (var _len = arguments.length, fns = new Array(_len), _key = 0; _key < _len; _key++) {
20
- fns[_key] = arguments[_key];
21
- }
22
- return function (decoratee) {
23
- return fns.reduceRight(function (decoratee, decorator) {
24
- return decorator(decoratee);
25
- }, decoratee);
1
+ import React, { createContext, useCallback, useContext, useMemo, useState } from "react";
2
+ import { useCompositionScope } from "./CompositionScope";
3
+ export function compose(...fns) {
4
+ return decoratee => {
5
+ return fns.reduceRight((decoratee, decorator) => decorator(decoratee), decoratee);
26
6
  };
27
7
  }
28
8
 
@@ -30,66 +10,57 @@ function compose() {
30
10
  * @deprecated Use `Decorator` instead.
31
11
  */
32
12
 
33
- var CompositionContext = /*#__PURE__*/(0, _react.createContext)(undefined);
34
- var composeComponents = function composeComponents(components, decorators) {
35
- var scope = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : "*";
36
- var scopeMap = components.get(scope) || new Map();
37
- var _iterator = (0, _createForOfIteratorHelper2.default)(decorators),
38
- _step;
39
- try {
40
- for (_iterator.s(); !(_step = _iterator.n()).done;) {
41
- var _step$value = (0, _slicedToArray2.default)(_step.value, 2),
42
- _component = _step$value[0],
43
- _hocs = _step$value[1];
44
- var recipe = scopeMap.get(_component) || {
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) || {
45
25
  component: null,
46
26
  hocs: []
47
27
  };
48
- var newHocs = [].concat((0, _toConsumableArray2.default)(recipe.hocs || []), (0, _toConsumableArray2.default)(_hocs));
49
- scopeMap.set(_component, {
50
- component: compose.apply(void 0, (0, _toConsumableArray2.default)((0, _toConsumableArray2.default)(newHocs).reverse()))(_component),
51
- hocs: newHocs
52
- });
53
- components.set(scope, scopeMap);
28
+ existingHocs.unshift(...globalRecipe.hocs);
54
29
  }
55
- } catch (err) {
56
- _iterator.e(err);
57
- } finally {
58
- _iterator.f();
30
+ const finalHocs = [...existingHocs, ...newHocs];
31
+ scopeMap.set(component, {
32
+ component: compose(...[...finalHocs].reverse())(component),
33
+ hocs: finalHocs
34
+ });
35
+ components.set(scope, scopeMap);
59
36
  }
60
37
  return components;
61
38
  };
62
- var CompositionProvider = exports.CompositionProvider = function CompositionProvider(_ref) {
63
- var _ref$decorators = _ref.decorators,
64
- decorators = _ref$decorators === void 0 ? [] : _ref$decorators,
65
- children = _ref.children;
66
- var _useState = (0, _react.useState)(function () {
67
- return composeComponents(new Map(), decorators.map(function (tuple) {
68
- return [tuple[0].original, tuple[1]];
69
- }));
70
- }),
71
- _useState2 = (0, _slicedToArray2.default)(_useState, 2),
72
- components = _useState2[0],
73
- setComponents = _useState2[1];
74
- var composeComponent = (0, _react.useCallback)(function (component, hocs) {
75
- var scope = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : "*";
76
- setComponents(function (prevComponents) {
77
- return composeComponents(new Map(prevComponents), [[component, hocs]], scope);
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);
78
51
  });
79
52
 
80
53
  // Return a function that will remove the added HOCs.
81
- return function () {
82
- setComponents(function (prevComponents) {
83
- var components = new Map(prevComponents);
84
- var scopeMap = components.get(scope) || new Map();
85
- var recipe = scopeMap.get(component) || {
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) || {
86
59
  component: null,
87
60
  hocs: []
88
61
  };
89
- var newHOCs = (0, _toConsumableArray2.default)(recipe.hocs).filter(function (hoc) {
90
- return !hocs.includes(hoc);
91
- });
92
- var NewComponent = compose.apply(void 0, (0, _toConsumableArray2.default)((0, _toConsumableArray2.default)(newHOCs).reverse()))(component);
62
+ const newHOCs = [...recipe.hocs].filter(hoc => !hocs.includes(hoc));
63
+ const NewComponent = compose(...[...newHOCs].reverse())(component);
93
64
  scopeMap.set(component, {
94
65
  component: NewComponent,
95
66
  hocs: newHOCs
@@ -99,54 +70,42 @@ var CompositionProvider = exports.CompositionProvider = function CompositionProv
99
70
  });
100
71
  };
101
72
  }, [setComponents]);
102
- var getComponent = (0, _react.useCallback)(function (Component) {
103
- var scope = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
104
- var scopesToResolve = ["*"].concat((0, _toConsumableArray2.default)(scope)).reverse();
105
- var _iterator2 = (0, _createForOfIteratorHelper2.default)(scopesToResolve),
106
- _step2;
107
- try {
108
- for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
109
- var _scope = _step2.value;
110
- var scopeMap = components.get(_scope) || new Map();
111
- var composedComponent = scopeMap.get(Component);
112
- if (composedComponent) {
113
- return composedComponent.component;
114
- }
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;
115
80
  }
116
- } catch (err) {
117
- _iterator2.e(err);
118
- } finally {
119
- _iterator2.f();
120
81
  }
121
82
  return undefined;
122
83
  }, [components]);
123
- var context = (0, _react.useMemo)(function () {
124
- return {
125
- getComponent: getComponent,
126
- composeComponent: composeComponent,
127
- components: components
128
- };
129
- }, [components, composeComponent]);
130
- 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, {
131
90
  value: context
132
91
  }, children);
133
92
  };
134
- function useComponent(baseFunction) {
135
- var context = useOptionalComposition();
136
- var scope = (0, _CompositionScope.useCompositionScope)();
93
+ export function useComponent(baseFunction) {
94
+ const context = useOptionalComposition();
95
+ const scope = useCompositionScope();
137
96
  if (!context) {
138
97
  return baseFunction;
139
98
  }
140
- return context.getComponent(baseFunction, scope) || baseFunction;
99
+ return context.getComponent(baseFunction, scope.scope) || baseFunction;
141
100
  }
142
101
 
143
102
  /**
144
103
  * This hook will throw an error if composition context doesn't exist.
145
104
  */
146
- function useComposition() {
147
- var context = (0, _react.useContext)(CompositionContext);
105
+ export function useComposition() {
106
+ const context = useContext(CompositionContext);
148
107
  if (!context) {
149
- throw new Error("You're missing a <CompositionProvider> higher up in your component hierarchy!");
108
+ throw new Error(`You're missing a <CompositionProvider> higher up in your component hierarchy!`);
150
109
  }
151
110
  return context;
152
111
  }
@@ -154,8 +113,8 @@ function useComposition() {
154
113
  /**
155
114
  * This hook will not throw an error if composition context doesn't exist.
156
115
  */
157
- function useOptionalComposition() {
158
- return (0, _react.useContext)(CompositionContext);
116
+ export function useOptionalComposition() {
117
+ return useContext(CompositionContext);
159
118
  }
160
119
 
161
120
  //# sourceMappingURL=Context.js.map
package/Context.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"names":["_react","_interopRequireWildcard","require","_CompositionScope","compose","_len","arguments","length","fns","Array","_key","decoratee","reduceRight","decorator","CompositionContext","createContext","undefined","composeComponents","components","decorators","scope","scopeMap","get","Map","_iterator","_createForOfIteratorHelper2","default","_step","s","n","done","_step$value","_slicedToArray2","value","component","hocs","recipe","newHocs","concat","_toConsumableArray2","set","apply","reverse","err","e","f","CompositionProvider","exports","_ref","_ref$decorators","children","_useState","useState","map","tuple","original","_useState2","setComponents","composeComponent","useCallback","prevComponents","newHOCs","filter","hoc","includes","NewComponent","getComponent","Component","scopesToResolve","_iterator2","_step2","composedComponent","context","useMemo","createElement","Provider","useComponent","baseFunction","useOptionalComposition","useCompositionScope","useComposition","useContext","Error"],"sources":["Context.tsx"],"sourcesContent":["import React, {\n ComponentType,\n createContext,\n useCallback,\n useContext,\n useMemo,\n useState\n} from \"react\";\nimport { useCompositionScope } from \"~/CompositionScope\";\nimport {\n ComposedFunction,\n ComposeWith,\n Decoratable,\n DecoratableComponent,\n DecoratableHook,\n Decorator,\n Enumerable,\n GenericComponent,\n GenericHook\n} from \"~/types\";\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 (component: ComponentType<unknown>, scope: string[]):\n | ComposedFunction\n | GenericComponent\n | undefined;\n}\n\ninterface CompositionContext {\n components: ComponentScopes;\n getComponent: CompositionContextGetComponentCallable;\n composeComponent(\n component: ComponentType<unknown>,\n hocs: Enumerable<ComposeWith>,\n scope?: string\n ): void;\n}\n\nconst CompositionContext = createContext<CompositionContext | 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) => {\n const scopeMap: ComposedComponents = components.get(scope) || new Map();\n for (const [component, hocs] of decorators) {\n const recipe = scopeMap.get(component) || { component: null, hocs: [] };\n\n const newHocs = [...(recipe.hocs || []), ...hocs] as Decorator<\n GenericHook | GenericComponent\n >[];\n\n scopeMap.set(component, {\n component: compose(...[...newHocs].reverse())(component),\n hocs: newHocs\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 ) => {\n setComponents(prevComponents => {\n return composeComponents(new Map(prevComponents), [[component, hocs]], scope);\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: 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<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) || 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(CompositionContext);\n}\n"],"mappings":";;;;;;;;;;;;;;;AAAA,IAAAA,MAAA,GAAAC,uBAAA,CAAAC,OAAA;AAQA,IAAAC,iBAAA,GAAAD,OAAA;AAaO,SAASE,OAAOA,CAAA,EAA4B;EAAA,SAAAC,IAAA,GAAAC,SAAA,CAAAC,MAAA,EAArBC,GAAG,OAAAC,KAAA,CAAAJ,IAAA,GAAAK,IAAA,MAAAA,IAAA,GAAAL,IAAA,EAAAK,IAAA;IAAHF,GAAG,CAAAE,IAAA,IAAAJ,SAAA,CAAAI,IAAA;EAAA;EAC7B,OAAO,UAACC,SAAY,EAAQ;IACxB,OAAOH,GAAG,CAACI,WAAW,CAAC,UAACD,SAAS,EAAEE,SAAS;MAAA,OAAKA,SAAS,CAACF,SAAS,CAAC;IAAA,GAAEA,SAAS,CAAC;EACrF,CAAC;AACL;;AAiBA;AACA;AACA;;AA2BA,IAA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;EAC7B,IAAMR,OAAO,GAAG,IAAAS,iBAAU,EAACnE,kBAAkB,CAAC;EAC9C,IAAI,CAAC0D,OAAO,EAAE;IACV,MAAM,IAAIU,KAAK,gFAEf,CAAC;EACL;EAEA,OAAOV,OAAO;AAClB;;AAEA;AACA;AACA;AACO,SAASM,sBAAsBA,CAAA,EAAG;EACrC,OAAO,IAAAG,iBAAU,EAACnE,kBAAkB,CAAC;AACzC","ignoreList":[]}
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\";\nimport type {\n ComposedFunction,\n ComposeWith,\n Decoratable,\n DecoratableComponent,\n DecoratableHook,\n Decorator,\n Enumerable,\n GenericComponent,\n GenericHook\n} from \"~/types\";\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 (component: ComponentType<unknown>, scope: string[]):\n | ComposedFunction\n | GenericComponent\n | undefined;\n}\n\ninterface CompositionContext {\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<CompositionContext | 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: 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<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(CompositionContext);\n}\n"],"mappings":"AACA,OAAOA,KAAK,IAAIC,aAAa,EAAEC,WAAW,EAAEC,UAAU,EAAEC,OAAO,EAAEC,QAAQ,QAAQ,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BxB;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":[]}
@@ -1,5 +1,5 @@
1
1
  import React from "react";
2
- import { CanReturnNullOrElement, Decoratable, DecoratableComponent, DecoratableHook, Decorator } from "./types";
2
+ import type { CanReturnNullOrElement, Decoratable, DecoratableComponent, DecoratableHook, Decorator } from "./types";
3
3
  type GetBaseFunction<T> = T extends DecoratableComponent<infer F> ? F : never;
4
4
  /**
5
5
  * Creates a component which, when mounted, registers a Higher Order Component for the given base component.
@@ -1,35 +1,25 @@
1
- "use strict";
2
-
3
- var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
4
- Object.defineProperty(exports, "__esModule", {
5
- value: true
6
- });
7
- exports.createComponentPlugin = createComponentPlugin;
8
- exports.createDecorator = createDecorator;
9
- var _react = _interopRequireDefault(require("react"));
10
- var _Compose = require("./Compose");
1
+ import React from "react";
2
+ import { Compose } from "./Compose";
11
3
  /**
12
4
  * Creates a component which, when mounted, registers a Higher Order Component for the given base component.
13
5
  * This is particularly useful for decorating (wrapping) existing composable components.
14
6
  * For more information, visit https://www.webiny.com/docs/admin-area/basics/framework.
15
7
  */
16
- function createComponentPlugin(Base, hoc) {
8
+ export function createComponentPlugin(Base, hoc) {
17
9
  return createDecorator(Base, hoc);
18
10
  }
19
11
 
20
12
  // Maybe there's a better way to mark params as non-existent, but for now I left it as `any`.
21
13
  // TODO: revisit this type; not sure if `?` can be handled in one clause
22
14
 
23
- var isDecoratableComponent = function isDecoratableComponent(decoratable) {
15
+ const isDecoratableComponent = decoratable => {
24
16
  return "displayName" in decoratable;
25
17
  };
26
- function createDecorator(Base, hoc) {
27
- var DecoratorPlugin = function DecoratorPlugin() {
28
- return /*#__PURE__*/_react.default.createElement(_Compose.Compose, {
29
- component: Base,
30
- with: hoc
31
- });
32
- };
18
+ export function createDecorator(Base, hoc) {
19
+ const DecoratorPlugin = () => /*#__PURE__*/React.createElement(Compose, {
20
+ component: Base,
21
+ with: hoc
22
+ });
33
23
  if (isDecoratableComponent(Base)) {
34
24
  DecoratorPlugin.displayName = Base.displayName;
35
25
  }
@@ -1 +1 @@
1
- {"version":3,"names":["_react","_interopRequireDefault","require","_Compose","createComponentPlugin","Base","hoc","createDecorator","isDecoratableComponent","decoratable","DecoratorPlugin","default","createElement","Compose","component","with","displayName"],"sources":["createDecorator.tsx"],"sourcesContent":["import React from \"react\";\nimport {\n CanReturnNullOrElement,\n Decoratable,\n DecoratableComponent,\n DecoratableHook,\n Decorator\n} from \"~/types\";\nimport { Compose } from \"~/Compose\";\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> = T extends DecoratableHook<infer F>\n ? F\n : T extends DecoratableComponent<infer F>\n ? F\n : 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,IAAAA,MAAA,GAAAC,sBAAA,CAAAC,OAAA;AAQA,IAAAC,QAAA,GAAAD,OAAA;AAIA;AACA;AACA;AACA;AACA;AACO,SAASE,qBAAqBA,CACjCC,IAAO,EACPC,GAEmC,EACrC;EACE,OAAOC,eAAe,CAACF,IAAI,EAAEC,GAAG,CAAC;AACrC;;AAEA;AACA;;AAaA,IAAME,sBAAsB,GAAG,SAAzBA,sBAAsBA,CACxBC,WAAmD,EACb;EACtC,OAAO,aAAa,IAAIA,WAAW;AACvC,CAAC;AAEM,SAASF,eAAeA,CAC3BF,IAAO,EACPC,GAEmC,EACrC;EACE,IAAMI,eAAe,GAAG,SAAlBA,eAAeA,CAAA;IAAA,oBAASV,MAAA,CAAAW,OAAA,CAAAC,aAAA,CAACT,QAAA,CAAAU,OAAO;MAACC,SAAS,EAAET,IAAK;MAACU,IAAI,EAAET;IAAW,CAAE,CAAC;EAAA;EAC5E,IAAIE,sBAAsB,CAACH,IAAI,CAAC,EAAE;IAC9BK,eAAe,CAACM,WAAW,GAAGX,IAAI,CAACW,WAAW;EAClD;EACA,OAAON,eAAe;AAC1B","ignoreList":[]}
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\";\nimport { Compose } from \"~/Compose\";\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> = T extends DecoratableHook<infer F>\n ? F\n : T extends DecoratableComponent<infer F>\n ? F\n : 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;;AAaA,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 CHANGED
@@ -1,6 +1,6 @@
1
1
  import React from "react";
2
- import { GetDecoratee, GetDecorateeParams } from "./createDecorator";
3
- import { DecoratableComponent, GenericComponent, Decorator, GenericHook, DecoratableHook, ComponentDecorator } from "./types";
2
+ import type { GetDecoratee, GetDecorateeParams } from "./createDecorator";
3
+ import type { DecoratableComponent, GenericComponent, Decorator, GenericHook, DecoratableHook, ComponentDecorator } from "./types";
4
4
  export interface ShouldDecorate<TDecorator = any, TComponent = any> {
5
5
  (decoratorProps: TDecorator, componentProps: TComponent): boolean;
6
6
  }
package/decorators.js CHANGED
@@ -1,48 +1,37 @@
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
- exports.createHookDecoratorFactory = createHookDecoratorFactory;
10
- exports.withDecoratorFactory = withDecoratorFactory;
11
- exports.withHookDecoratorFactory = withHookDecoratorFactory;
12
- var _react = _interopRequireDefault(require("react"));
13
- var _Compose = require("./Compose");
14
- function createConditionalDecorator(shouldDecorate, decorator, decoratorProps) {
15
- return function (Original) {
16
- var DecoratedComponent = /*#__PURE__*/_react.default.memo(decorator(Original));
1
+ import React from "react";
2
+ import { Compose } from "./Compose";
3
+ export function createConditionalDecorator(shouldDecorate, decorator, decoratorProps) {
4
+ return Original => {
5
+ const DecoratedComponent = /*#__PURE__*/React.memo(decorator(Original));
17
6
  DecoratedComponent.displayName = Original.displayName;
18
7
  return function ShouldDecorate(props) {
19
8
  if (shouldDecorate(decoratorProps, props)) {
20
9
  // @ts-expect-error
21
- return /*#__PURE__*/_react.default.createElement(DecoratedComponent, props);
10
+ return /*#__PURE__*/React.createElement(DecoratedComponent, props);
22
11
  }
23
12
 
24
13
  // @ts-expect-error
25
- return /*#__PURE__*/_react.default.createElement(Original, props);
14
+ return /*#__PURE__*/React.createElement(Original, props);
26
15
  };
27
16
  };
28
17
  }
29
- var memoizedComponent = function memoizedComponent(decorator) {
30
- return function (decoratee) {
31
- return /*#__PURE__*/_react.default.memo(decorator(decoratee));
18
+ const memoizedComponent = decorator => {
19
+ return decoratee => {
20
+ return /*#__PURE__*/React.memo(decorator(decoratee));
32
21
  };
33
22
  };
34
- function createDecoratorFactory() {
23
+ export function createDecoratorFactory() {
35
24
  return function from(decoratable, shouldDecorate) {
36
25
  return function createDecorator(decorator) {
37
26
  return function DecoratorPlugin(props) {
38
27
  if (shouldDecorate) {
39
- var componentDecorator = createConditionalDecorator(shouldDecorate, decorator, props);
40
- return /*#__PURE__*/_react.default.createElement(_Compose.Compose, {
28
+ const componentDecorator = createConditionalDecorator(shouldDecorate, decorator, props);
29
+ return /*#__PURE__*/React.createElement(Compose, {
41
30
  function: decoratable,
42
31
  with: componentDecorator
43
32
  });
44
33
  }
45
- return /*#__PURE__*/_react.default.createElement(_Compose.Compose, {
34
+ return /*#__PURE__*/React.createElement(Compose, {
46
35
  function: decoratable,
47
36
  with: memoizedComponent(decorator)
48
37
  });
@@ -50,11 +39,11 @@ function createDecoratorFactory() {
50
39
  };
51
40
  };
52
41
  }
53
- function createHookDecoratorFactory() {
42
+ export function createHookDecoratorFactory() {
54
43
  return function from(decoratable) {
55
44
  return function createDecorator(decorator) {
56
45
  return function DecoratorPlugin() {
57
- return /*#__PURE__*/_react.default.createElement(_Compose.Compose, {
46
+ return /*#__PURE__*/React.createElement(Compose, {
58
47
  function: decoratable,
59
48
  with: decorator
60
49
  });
@@ -62,19 +51,19 @@ function createHookDecoratorFactory() {
62
51
  };
63
52
  };
64
53
  }
65
- function withDecoratorFactory() {
54
+ export function withDecoratorFactory() {
66
55
  return function WithDecorator(Component, shouldDecorate) {
67
- var createDecorator = createDecoratorFactory()(Component, shouldDecorate);
56
+ const createDecorator = createDecoratorFactory()(Component, shouldDecorate);
68
57
  return Object.assign(Component, {
69
- createDecorator: createDecorator
58
+ createDecorator
70
59
  });
71
60
  };
72
61
  }
73
- function withHookDecoratorFactory() {
62
+ export function withHookDecoratorFactory() {
74
63
  return function WithHookDecorator(hook) {
75
- var createDecorator = createHookDecoratorFactory()(hook);
64
+ const createDecorator = createHookDecoratorFactory()(hook);
76
65
  return Object.assign(hook, {
77
- createDecorator: createDecorator
66
+ createDecorator
78
67
  });
79
68
  };
80
69
  }
package/decorators.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"names":["_react","_interopRequireDefault","require","_Compose","createConditionalDecorator","shouldDecorate","decorator","decoratorProps","Original","DecoratedComponent","React","memo","displayName","ShouldDecorate","props","default","createElement","memoizedComponent","decoratee","createDecoratorFactory","from","decoratable","createDecorator","DecoratorPlugin","componentDecorator","Compose","function","with","createHookDecoratorFactory","withDecoratorFactory","WithDecorator","Component","Object","assign","withHookDecoratorFactory","WithHookDecorator","hook"],"sources":["decorators.tsx"],"sourcesContent":["import React from \"react\";\nimport { Compose } from \"~/Compose\";\nimport { GetDecoratee, GetDecorateeParams } from \"~/createDecorator\";\nimport {\n DecoratableComponent,\n GenericComponent,\n Decorator,\n GenericHook,\n DecoratableHook,\n ComponentDecorator\n} from \"~/types\";\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,IAAAA,MAAA,GAAAC,sBAAA,CAAAC,OAAA;AACA,IAAAC,QAAA,GAAAD,OAAA;AAeO,SAASE,0BAA0BA,CACtCC,cAA8B,EAC9BC,SAAgC,EAChCC,cAAuB,EACF;EACrB,OAAQ,UAAAC,QAAQ,EAAI;IAChB,IAAMC,kBAAkB,gBAAGC,cAAK,CAACC,IAAI,CAACL,SAAS,CAACE,QAAQ,CAAC,CAAC;IAC1DC,kBAAkB,CAACG,WAAW,GAAGJ,QAAQ,CAACI,WAAW;IAErD,OAAO,SAASC,cAAcA,CAACC,KAAc,EAAE;MAC3C,IAAIT,cAAc,CAACE,cAAc,EAAEO,KAAK,CAAC,EAAE;QACvC;QACA,oBAAOd,MAAA,CAAAe,OAAA,CAAAC,aAAA,CAACP,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,EAAEL,WAAY;UACtBM,IAAI,EAAErB;QAA+C,CACxD,CAAC;MAEV,CAAC;IACL,CAAC;EACL,CAAC;AACL;AAEO,SAASuB,oBAAoBA,CAAA,EAAe;EAC/C,OAAO,SAASC,aAAaA,CACzBC,SAAuB,EACvB1B,cAA2F,EAC7F;IACE,IAAMiB,eAAe,GAAGH,sBAAsB,CAAa,CAAC,CAACY,SAAS,EAAE1B,cAAc,CAAC;IAEvF,OAAO2B,MAAM,CAACC,MAAM,CAACF,SAAS,EAAE;MAAET,eAAe,EAAfA;IAAgB,CAAC,CAAC;EAGxD,CAAC;AACL;AAEO,SAASY,wBAAwBA,CAAA,EAAG;EACvC,OAAO,SAASC,iBAAiBA,CAAuCC,IAAkB,EAAE;IACxF,IAAMd,eAAe,GAAGM,0BAA0B,CAAC,CAAC,CAACQ,IAAI,CAAC;IAE1D,OAAOJ,MAAM,CAACC,MAAM,CAACG,IAAI,EAAE;MAAEd,eAAe,EAAfA;IAAgB,CAAC,CAAC;EAMnD,CAAC;AACL","ignoreList":[]}
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\";\nimport type { GetDecoratee, GetDecorateeParams } from \"~/createDecorator\";\nimport type {\n DecoratableComponent,\n GenericComponent,\n Decorator,\n GenericHook,\n DecoratableHook,\n ComponentDecorator\n} from \"~/types\";\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,SAASe,eAAeA,CAACR,KAAiB,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,CAAC;EAMnD,CAAC;AACL","ignoreList":[]}
package/index.js CHANGED
@@ -1,95 +1,10 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- var _Context = require("./Context");
7
- Object.keys(_Context).forEach(function (key) {
8
- if (key === "default" || key === "__esModule") return;
9
- if (key in exports && exports[key] === _Context[key]) return;
10
- Object.defineProperty(exports, key, {
11
- enumerable: true,
12
- get: function get() {
13
- return _Context[key];
14
- }
15
- });
16
- });
17
- var _Compose = require("./Compose");
18
- Object.keys(_Compose).forEach(function (key) {
19
- if (key === "default" || key === "__esModule") return;
20
- if (key in exports && exports[key] === _Compose[key]) return;
21
- Object.defineProperty(exports, key, {
22
- enumerable: true,
23
- get: function get() {
24
- return _Compose[key];
25
- }
26
- });
27
- });
28
- var _makeComposable = require("./makeComposable");
29
- Object.keys(_makeComposable).forEach(function (key) {
30
- if (key === "default" || key === "__esModule") return;
31
- if (key in exports && exports[key] === _makeComposable[key]) return;
32
- Object.defineProperty(exports, key, {
33
- enumerable: true,
34
- get: function get() {
35
- return _makeComposable[key];
36
- }
37
- });
38
- });
39
- var _makeDecoratable = require("./makeDecoratable");
40
- Object.keys(_makeDecoratable).forEach(function (key) {
41
- if (key === "default" || key === "__esModule") return;
42
- if (key in exports && exports[key] === _makeDecoratable[key]) return;
43
- Object.defineProperty(exports, key, {
44
- enumerable: true,
45
- get: function get() {
46
- return _makeDecoratable[key];
47
- }
48
- });
49
- });
50
- var _createDecorator = require("./createDecorator");
51
- Object.keys(_createDecorator).forEach(function (key) {
52
- if (key === "default" || key === "__esModule") return;
53
- if (key in exports && exports[key] === _createDecorator[key]) return;
54
- Object.defineProperty(exports, key, {
55
- enumerable: true,
56
- get: function get() {
57
- return _createDecorator[key];
58
- }
59
- });
60
- });
61
- var _decorators = require("./decorators");
62
- Object.keys(_decorators).forEach(function (key) {
63
- if (key === "default" || key === "__esModule") return;
64
- if (key in exports && exports[key] === _decorators[key]) return;
65
- Object.defineProperty(exports, key, {
66
- enumerable: true,
67
- get: function get() {
68
- return _decorators[key];
69
- }
70
- });
71
- });
72
- var _CompositionScope = require("./CompositionScope");
73
- Object.keys(_CompositionScope).forEach(function (key) {
74
- if (key === "default" || key === "__esModule") return;
75
- if (key in exports && exports[key] === _CompositionScope[key]) return;
76
- Object.defineProperty(exports, key, {
77
- enumerable: true,
78
- get: function get() {
79
- return _CompositionScope[key];
80
- }
81
- });
82
- });
83
- var _types = require("./types");
84
- Object.keys(_types).forEach(function (key) {
85
- if (key === "default" || key === "__esModule") return;
86
- if (key in exports && exports[key] === _types[key]) return;
87
- Object.defineProperty(exports, key, {
88
- enumerable: true,
89
- get: function get() {
90
- return _types[key];
91
- }
92
- });
93
- });
1
+ export * from "./Context";
2
+ export * from "./Compose";
3
+ export * from "./makeComposable";
4
+ export * from "./makeDecoratable";
5
+ export * from "./createDecorator";
6
+ export * from "./decorators";
7
+ export * from "./CompositionScope";
8
+ export * from "./types";
94
9
 
95
10
  //# sourceMappingURL=index.js.map
package/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"names":["_Context","require","Object","keys","forEach","key","exports","defineProperty","enumerable","get","_Compose","_makeComposable","_makeDecoratable","_createDecorator","_decorators","_CompositionScope","_types"],"sources":["index.ts"],"sourcesContent":["export * from \"./Context\";\nexport * from \"./Compose\";\nexport * from \"./makeComposable\";\nexport * from \"./makeDecoratable\";\nexport * from \"./createDecorator\";\nexport * from \"./decorators\";\nexport * from \"./CompositionScope\";\nexport * from \"./types\";\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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A,CAAAX,GAAA;EAAAH,MAAA,CAAAK,cAAA,CAAAD,OAAA,EAAAD,GAAA;IAAAG,UAAA;IAAAC,GAAA,WAAAA,IAAA;MAAA,OAAAO,MAAA,CAAAX,GAAA;IAAA;EAAA;AAAA","ignoreList":[]}
1
+ {"version":3,"names":[],"sources":["index.ts"],"sourcesContent":["export * from \"./Context\";\nexport * from \"./Compose\";\nexport * from \"./makeComposable\";\nexport * from \"./makeDecoratable\";\nexport * from \"./createDecorator\";\nexport * from \"./decorators\";\nexport * from \"./CompositionScope\";\nexport * from \"./types\";\n"],"mappings":"AAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA","ignoreList":[]}
@@ -1,5 +1,5 @@
1
1
  /// <reference types="react" />
2
- import { GenericComponent } from "./types";
2
+ import type { GenericComponent } from "./types";
3
3
  /**
4
4
  * @deprecated Use `makeDecoratable` instead.
5
5
  */
package/makeComposable.js CHANGED
@@ -1,22 +1,14 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.makeComposable = makeComposable;
7
- var _react = require("react");
8
- var _makeDecoratable = require("./makeDecoratable");
9
- var ComposableContext = /*#__PURE__*/(0, _react.createContext)([]);
1
+ import { createContext } from "react";
2
+ import { makeDecoratable } from "./makeDecoratable";
3
+ const ComposableContext = /*#__PURE__*/createContext([]);
10
4
  ComposableContext.displayName = "ComposableContext";
11
- var nullRenderer = function nullRenderer() {
12
- return null;
13
- };
5
+ const nullRenderer = () => null;
14
6
 
15
7
  /**
16
8
  * @deprecated Use `makeDecoratable` instead.
17
9
  */
18
- function makeComposable(name, Component) {
19
- return (0, _makeDecoratable.makeDecoratable)(name, Component ?? nullRenderer);
10
+ export function makeComposable(name, Component) {
11
+ return makeDecoratable(name, Component ?? nullRenderer);
20
12
  }
21
13
 
22
14
  //# sourceMappingURL=makeComposable.js.map
@@ -1 +1 @@
1
- {"version":3,"names":["_react","require","_makeDecoratable","ComposableContext","createContext","displayName","nullRenderer","makeComposable","name","Component","makeDecoratable"],"sources":["makeComposable.tsx"],"sourcesContent":["import { createContext } from \"react\";\nimport { GenericComponent } from \"~/types\";\nimport { makeDecoratable } from \"~/makeDecoratable\";\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,IAAAA,MAAA,GAAAC,OAAA;AAEA,IAAAC,gBAAA,GAAAD,OAAA;AAEA,IAAME,iBAAiB,gBAAG,IAAAC,oBAAa,EAAW,EAAE,CAAC;AACrDD,iBAAiB,CAACE,WAAW,GAAG,mBAAmB;AAEnD,IAAMC,YAAY,GAAG,SAAfA,YAAYA,CAAA;EAAA,OAAS,IAAI;AAAA;;AAE/B;AACA;AACA;AACO,SAASC,cAAcA,CAA6BC,IAAY,EAAEC,SAAa,EAAE;EACpF,OAAO,IAAAC,gCAAe,EAACF,IAAI,EAAEC,SAAS,IAAIH,YAAY,CAAC;AAC3D","ignoreList":[]}
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\";\nimport { makeDecoratable } from \"~/makeDecoratable\";\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":[]}
@@ -1,5 +1,5 @@
1
1
  import React from "react";
2
- import { DecoratableComponent, DecoratableHook, GenericComponent, GenericHook } from "./types";
2
+ import type { DecoratableComponent, DecoratableHook, GenericComponent, GenericHook } from "./types";
3
3
  declare function makeDecoratableComponent<T extends GenericComponent>(name: string, Component?: T): T & {
4
4
  original: T;
5
5
  originalName: string;
@@ -1,65 +1,49 @@
1
- "use strict";
2
-
3
- var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard").default;
4
- var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
5
- Object.defineProperty(exports, "__esModule", {
6
- value: true
7
- });
8
- exports.createVoidComponent = createVoidComponent;
9
- exports.makeDecoratable = makeDecoratable;
10
- exports.makeDecoratableHook = makeDecoratableHook;
11
- var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
12
- var _react = _interopRequireWildcard(require("react"));
13
- var _Context = require("./Context");
14
- var _decorators = require("./decorators");
15
- var ComposableContext = /*#__PURE__*/(0, _react.createContext)([]);
1
+ import React, { createContext, useContext, useMemo } from "react";
2
+ import { useComponent } from "./Context";
3
+ import { withDecoratorFactory, withHookDecoratorFactory } from "./decorators";
4
+ const ComposableContext = /*#__PURE__*/createContext([]);
16
5
  ComposableContext.displayName = "ComposableContext";
17
6
  function useComposableParents() {
18
- var context = (0, _react.useContext)(ComposableContext);
7
+ const context = useContext(ComposableContext);
19
8
  if (!context) {
20
9
  return [];
21
10
  }
22
11
  return context;
23
12
  }
24
- var nullRenderer = function nullRenderer() {
25
- return null;
26
- };
27
- function makeDecoratableComponent(name) {
28
- var Component = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : nullRenderer;
29
- var Decoratable = function Decoratable(props) {
30
- var parents = useComposableParents();
31
- var ComposedComponent = (0, _Context.useComponent)(Component);
32
- var context = (0, _react.useMemo)(function () {
33
- return [].concat((0, _toConsumableArray2.default)(parents), [name]);
34
- }, [parents, name]);
35
- return /*#__PURE__*/_react.default.createElement(ComposableContext.Provider, {
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, {
36
20
  value: context
37
- }, /*#__PURE__*/_react.default.createElement(ComposedComponent, props, props.children));
21
+ }, /*#__PURE__*/React.createElement(ComposedComponent, props, props.children));
38
22
  };
39
- var staticProps = {
23
+ const staticProps = {
40
24
  original: Component,
41
25
  originalName: name,
42
- displayName: "Decoratable<".concat(name, ">")
26
+ displayName: `Decoratable<${name}>`
43
27
  };
44
- return (0, _decorators.withDecoratorFactory)()(Object.assign(Decoratable, staticProps));
28
+ return withDecoratorFactory()(Object.assign(Decoratable, staticProps));
45
29
  }
46
- function makeDecoratableHook(hook) {
47
- var decoratableHook = function decoratableHook(params) {
48
- var composedHook = (0, _Context.useComponent)(hook);
30
+ export function makeDecoratableHook(hook) {
31
+ const decoratableHook = params => {
32
+ const composedHook = useComponent(hook);
49
33
  return composedHook(params);
50
34
  };
51
35
  decoratableHook.original = hook;
52
- return (0, _decorators.withHookDecoratorFactory)()(decoratableHook);
36
+ return withHookDecoratorFactory()(decoratableHook);
53
37
  }
54
- function createVoidComponent() {
38
+ export function createVoidComponent() {
55
39
  // eslint-disable-next-line @typescript-eslint/no-unused-vars
56
- return function (props) {
40
+ return props => {
57
41
  return null;
58
42
  };
59
43
  }
60
- function makeDecoratable(hookOrName, Component) {
44
+ export function makeDecoratable(hookOrName, Component) {
61
45
  if (Component) {
62
- var component = makeDecoratableComponent(hookOrName, /*#__PURE__*/_react.default.memo(Component));
46
+ const component = makeDecoratableComponent(hookOrName, /*#__PURE__*/React.memo(Component));
63
47
  component.original.displayName = hookOrName;
64
48
  return component;
65
49
  }
@@ -1 +1 @@
1
- {"version":3,"names":["_react","_interopRequireWildcard","require","_Context","_decorators","ComposableContext","createContext","displayName","useComposableParents","context","useContext","nullRenderer","makeDecoratableComponent","name","Component","arguments","length","undefined","Decoratable","props","parents","ComposedComponent","useComponent","useMemo","concat","_toConsumableArray2","default","createElement","Provider","value","children","staticProps","original","originalName","withDecoratorFactory","Object","assign","makeDecoratableHook","hook","decoratableHook","params","composedHook","withHookDecoratorFactory","createVoidComponent","makeDecoratable","hookOrName","component","React","memo"],"sources":["makeDecoratable.tsx"],"sourcesContent":["import React, { createContext, useContext, useMemo } from \"react\";\nimport { useComponent } from \"./Context\";\nimport { DecoratableComponent, DecoratableHook, GenericComponent, GenericHook } from \"~/types\";\nimport { withDecoratorFactory, withHookDecoratorFactory } from \"~/decorators\";\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,IAAAA,MAAA,GAAAC,uBAAA,CAAAC,OAAA;AACA,IAAAC,QAAA,GAAAD,OAAA;AAEA,IAAAE,WAAA,GAAAF,OAAA;AAEA,IAAMG,iBAAiB,gBAAG,IAAAC,oBAAa,EAAW,EAAE,CAAC;AACrDD,iBAAiB,CAACE,WAAW,GAAG,mBAAmB;AAEnD,SAASC,oBAAoBA,CAAA,EAAG;EAC5B,IAAMC,OAAO,GAAG,IAAAC,iBAAU,EAACL,iBAAiB,CAAC;EAC7C,IAAI,CAACI,OAAO,EAAE;IACV,OAAO,EAAE;EACb;EAEA,OAAOA,OAAO;AAClB;AAEA,IAAME,YAAY,GAAG,SAAfA,YAAYA,CAAA;EAAA,OAAS,IAAI;AAAA;AAE/B,SAASC,wBAAwBA,CAC7BC,IAAY,EAEd;EAAA,IADEC,SAAY,GAAAC,SAAA,CAAAC,MAAA,QAAAD,SAAA,QAAAE,SAAA,GAAAF,SAAA,MAAGJ,YAAY;EAE3B,IAAMO,WAAW,GAAG,SAAdA,WAAWA,CAAIC,KAA8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Ae,EAAE;EAC9D,IAAIA,SAAS,EAAE;IACX,IAAMgC,SAAS,GAAGlC,wBAAwB,CAACiC,UAAU,eAAEE,cAAK,CAACC,IAAI,CAAClC,SAAS,CAAC,CAAC;IAC7EgC,SAAS,CAACd,QAAQ,CAACzB,WAAW,GAAGsC,UAAU;IAC3C,OAAOC,SAAS;EACpB;EAEA,OAAOT,mBAAmB,CAACQ,UAAU,CAAC;AAC1C","ignoreList":[]}
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\";\nimport type { DecoratableComponent, DecoratableHook, GenericComponent, GenericHook } from \"~/types\";\nimport { withDecoratorFactory, withHookDecoratorFactory } from \"~/decorators\";\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;AAErB,SAASC,oBAAoB,EAAEC,wBAAwB;AAEvD,MAAMC,iBAAiB,gBAAGN,aAAa,CAAW,EAAE,CAAC;AACrDM,iBAAiB,CAACC,WAAW,GAAG,mBAAmB;AAEnD,SAASC,oBAAoBA,CAAA,EAAG;EAC5B,MAAMC,OAAO,GAAGR,UAAU,CAACK,iBAAiB,CAAC;EAC7C,IAAI,CAACG,OAAO,EAAE;IACV,OAAO,EAAE;EACb;EAEA,OAAOA,OAAO;AAClB;AAEA,MAAMC,YAAY,GAAGA,CAAA,KAAM,IAAI;AAE/B,SAASC,wBAAwBA,CAC7BC,IAAY,EACZC,SAAY,GAAGH,YAA4B,EAC7C;EACE,MAAMI,WAAW,GAAIC,KAA8B,IAAyB;IACxE,MAAMC,OAAO,GAAGR,oBAAoB,CAAC,CAAC;IACtC,MAAMS,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2B,UAAU;IAC3C,OAAOC,SAAS;EACpB;EAEA,OAAOR,mBAAmB,CAACO,UAAU,CAAC;AAC1C","ignoreList":[]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@webiny/react-composition",
3
- "version": "5.43.3",
3
+ "version": "6.0.0-alpha.1",
4
4
  "main": "index.js",
5
5
  "repository": {
6
6
  "type": "git",
@@ -20,7 +20,7 @@
20
20
  },
21
21
  "devDependencies": {
22
22
  "@testing-library/react": "15.0.7",
23
- "@webiny/project-utils": "5.43.3",
23
+ "@webiny/project-utils": "6.0.0-alpha.1",
24
24
  "typescript": "5.3.3"
25
25
  },
26
26
  "publishConfig": {
@@ -31,5 +31,5 @@
31
31
  "build": "node ../cli/bin.js run build",
32
32
  "watch": "node ../cli/bin.js run watch"
33
33
  },
34
- "gitHead": "c6d623eb1b715f3731434a8d3107b8fd8a1d6792"
34
+ "gitHead": "9bd236cf5e689f209a11bec089207dcc2d41a53c"
35
35
  }
package/types.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import React from "react";
1
+ import type React from "react";
2
2
  export type GenericHook<TParams = any, TReturn = any> = (...args: TParams[]) => TReturn;
3
3
  export type GenericComponent<T = any> = React.FunctionComponent<T>;
4
4
  export type ComposedFunction = GenericHook;
package/types.js CHANGED
@@ -1,7 +1,3 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
1
+ export {};
6
2
 
7
3
  //# sourceMappingURL=types.js.map
package/types.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"names":[],"sources":["types.ts"],"sourcesContent":["import 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":[]}
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":[]}