@webiny/react-composition 5.40.0-beta.1 → 5.40.0-beta.3

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
@@ -7,7 +7,7 @@ exports.Compose = void 0;
7
7
  var _react = require("react");
8
8
  var _Context = require("./Context");
9
9
  var _CompositionScope = require("./CompositionScope");
10
- var Compose = function Compose(props) {
10
+ var Compose = exports.Compose = function Compose(props) {
11
11
  var _useComposition = (0, _Context.useComposition)(),
12
12
  composeComponent = _useComposition.composeComponent;
13
13
  var scope = (0, _CompositionScope.useCompositionScope)();
@@ -22,6 +22,5 @@ var Compose = function Compose(props) {
22
22
  }, [props.with]);
23
23
  return null;
24
24
  };
25
- exports.Compose = Compose;
26
25
 
27
26
  //# sourceMappingURL=Compose.js.map
package/Compose.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"names":["_react","require","_Context","_CompositionScope","Compose","props","_useComposition","useComposition","composeComponent","scope","useCompositionScope","targetFn","function","component","useEffect","original","console","warn","concat","originalName","name","decorators","Array","isArray","with","exports"],"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(targetFn.original, decorators as Enumerable<ComposeWith>, scope);\n }, [props.with]);\n\n return null;\n};\n"],"mappings":";;;;;;AAAA,IAAAA,MAAA,GAAAC,OAAA;AACA,IAAAC,QAAA,GAAAD,OAAA;AACA,IAAAE,iBAAA,GAAAF,OAAA;AASO,IAAMG,OAAO,GAAG,SAAVA,OAAOA,CAAIC,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,CAACG,QAAQ,CAACI,QAAQ,EAAEM,UAAU,EAA6BZ,KAAK,CAAC;EAC5F,CAAC,EAAE,CAACJ,KAAK,CAACmB,IAAI,CAAC,CAAC;EAEhB,OAAO,IAAI;AACf,CAAC;AAACC,OAAA,CAAArB,OAAA,GAAAA,OAAA"}
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"],"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(targetFn.original, decorators as Enumerable<ComposeWith>, scope);\n }, [props.with]);\n\n return null;\n};\n"],"mappings":";;;;;;AAAA,IAAAA,MAAA,GAAAC,OAAA;AACA,IAAAC,QAAA,GAAAD,OAAA;AACA,IAAAE,iBAAA,GAAAF,OAAA;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,CAACG,QAAQ,CAACI,QAAQ,EAAEM,UAAU,EAA6BZ,KAAK,CAAC;EAC5F,CAAC,EAAE,CAACJ,KAAK,CAACmB,IAAI,CAAC,CAAC;EAEhB,OAAO,IAAI;AACf,CAAC","ignoreList":[]}
@@ -6,6 +6,6 @@ interface CompositionScopeProps {
6
6
  name: string;
7
7
  children: React.ReactNode;
8
8
  }
9
- export declare const CompositionScope: ({ name, children }: CompositionScopeProps) => JSX.Element;
9
+ export declare const CompositionScope: ({ name, children }: CompositionScopeProps) => React.JSX.Element;
10
10
  export declare function useCompositionScope(): string | undefined;
11
11
  export {};
@@ -8,7 +8,7 @@ exports.CompositionScope = void 0;
8
8
  exports.useCompositionScope = useCompositionScope;
9
9
  var _react = _interopRequireDefault(require("react"));
10
10
  var CompositionScopeContext = /*#__PURE__*/_react.default.createContext(undefined);
11
- var CompositionScope = function CompositionScope(_ref) {
11
+ var CompositionScope = exports.CompositionScope = function CompositionScope(_ref) {
12
12
  var name = _ref.name,
13
13
  children = _ref.children;
14
14
  return /*#__PURE__*/_react.default.createElement(CompositionScopeContext.Provider, {
@@ -17,7 +17,6 @@ var CompositionScope = function CompositionScope(_ref) {
17
17
  }
18
18
  }, children);
19
19
  };
20
- exports.CompositionScope = CompositionScope;
21
20
  function useCompositionScope() {
22
21
  var context = _react.default.useContext(CompositionScopeContext);
23
22
  if (!context) {
@@ -1 +1 @@
1
- {"version":3,"names":["_react","_interopRequireDefault","require","CompositionScopeContext","React","createContext","undefined","CompositionScope","_ref","name","children","default","createElement","Provider","value","exports","useCompositionScope","context","useContext"],"sources":["CompositionScope.tsx"],"sourcesContent":["import React from \"react\";\n\nexport interface CompositionScopeContext {\n name: string;\n}\n\nconst CompositionScopeContext = React.createContext<CompositionScopeContext | undefined>(undefined);\n\ninterface CompositionScopeProps {\n name: string;\n children: React.ReactNode;\n}\n\nexport const CompositionScope = ({ name, children }: CompositionScopeProps) => {\n return (\n <CompositionScopeContext.Provider value={{ name }}>\n {children}\n </CompositionScopeContext.Provider>\n );\n};\n\nexport function useCompositionScope() {\n const context = React.useContext(CompositionScopeContext);\n if (!context) {\n return undefined;\n }\n return context.name;\n}\n"],"mappings":";;;;;;;;AAAA,IAAAA,MAAA,GAAAC,sBAAA,CAAAC,OAAA;AAMA,IAAMC,uBAAuB,gBAAGC,cAAK,CAACC,aAAa,CAAsCC,SAAS,CAAC;AAO5F,IAAMC,gBAAgB,GAAG,SAAnBA,gBAAgBA,CAAAC,IAAA,EAAkD;EAAA,IAA5CC,IAAI,GAAAD,IAAA,CAAJC,IAAI;IAAEC,QAAQ,GAAAF,IAAA,CAARE,QAAQ;EAC7C,oBACIV,MAAA,CAAAW,OAAA,CAAAC,aAAA,CAACT,uBAAuB,CAACU,QAAQ;IAACC,KAAK,EAAE;MAAEL,IAAI,EAAJA;IAAK;EAAE,GAC7CC,QAC6B,CAAC;AAE3C,CAAC;AAACK,OAAA,CAAAR,gBAAA,GAAAA,gBAAA;AAEK,SAASS,mBAAmBA,CAAA,EAAG;EAClC,IAAMC,OAAO,GAAGb,cAAK,CAACc,UAAU,CAACf,uBAAuB,CAAC;EACzD,IAAI,CAACc,OAAO,EAAE;IACV,OAAOX,SAAS;EACpB;EACA,OAAOW,OAAO,CAACR,IAAI;AACvB"}
1
+ {"version":3,"names":["_react","_interopRequireDefault","require","CompositionScopeContext","React","createContext","undefined","CompositionScope","exports","_ref","name","children","default","createElement","Provider","value","useCompositionScope","context","useContext"],"sources":["CompositionScope.tsx"],"sourcesContent":["import React from \"react\";\n\nexport interface CompositionScopeContext {\n name: string;\n}\n\nconst CompositionScopeContext = React.createContext<CompositionScopeContext | undefined>(undefined);\n\ninterface CompositionScopeProps {\n name: string;\n children: React.ReactNode;\n}\n\nexport const CompositionScope = ({ name, children }: CompositionScopeProps) => {\n return (\n <CompositionScopeContext.Provider value={{ name }}>\n {children}\n </CompositionScopeContext.Provider>\n );\n};\n\nexport function useCompositionScope() {\n const context = React.useContext(CompositionScopeContext);\n if (!context) {\n return undefined;\n }\n return context.name;\n}\n"],"mappings":";;;;;;;;AAAA,IAAAA,MAAA,GAAAC,sBAAA,CAAAC,OAAA;AAMA,IAAMC,uBAAuB,gBAAGC,cAAK,CAACC,aAAa,CAAsCC,SAAS,CAAC;AAO5F,IAAMC,gBAAgB,GAAAC,OAAA,CAAAD,gBAAA,GAAG,SAAnBA,gBAAgBA,CAAAE,IAAA,EAAkD;EAAA,IAA5CC,IAAI,GAAAD,IAAA,CAAJC,IAAI;IAAEC,QAAQ,GAAAF,IAAA,CAARE,QAAQ;EAC7C,oBACIX,MAAA,CAAAY,OAAA,CAAAC,aAAA,CAACV,uBAAuB,CAACW,QAAQ;IAACC,KAAK,EAAE;MAAEL,IAAI,EAAJA;IAAK;EAAE,GAC7CC,QAC6B,CAAC;AAE3C,CAAC;AAEM,SAASK,mBAAmBA,CAAA,EAAG;EAClC,IAAMC,OAAO,GAAGb,cAAK,CAACc,UAAU,CAACf,uBAAuB,CAAC;EACzD,IAAI,CAACc,OAAO,EAAE;IACV,OAAOX,SAAS;EACpB;EACA,OAAOW,OAAO,CAACP,IAAI;AACvB","ignoreList":[]}
package/Context.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import React, { ComponentType } from "react";
2
- import { ComposedFunction, ComposeWith, DecoratableComponent, DecoratableHook, Enumerable, GenericComponent, Decorator, GenericHook } from "./types";
2
+ import { ComposedFunction, ComposeWith, DecoratableComponent, DecoratableHook, Decorator, Enumerable, GenericComponent, GenericHook } from "./types";
3
3
  export declare function compose<T>(...fns: Decorator<T>[]): (decoratee: T) => T;
4
4
  interface ComposedComponent {
5
5
  /**
@@ -24,16 +24,19 @@ export interface HigherOrderComponent<TProps = any, TOutput = TProps> {
24
24
  declare type ComposedComponents = Map<ComponentType<unknown>, ComposedComponent>;
25
25
  declare type ComponentScopes = Map<string, ComposedComponents>;
26
26
  export declare type DecoratableTypes = DecoratableComponent | DecoratableHook;
27
+ interface CompositionContextGetComponentCallable {
28
+ (component: ComponentType<unknown>, scope?: string): ComposedFunction | GenericComponent | undefined;
29
+ }
27
30
  interface CompositionContext {
28
31
  components: ComponentScopes;
29
- getComponent(component: ComponentType<unknown>, scope?: string): ComposedFunction | undefined;
32
+ getComponent: CompositionContextGetComponentCallable;
30
33
  composeComponent(component: ComponentType<unknown>, hocs: Enumerable<ComposeWith>, scope?: string): void;
31
34
  }
32
35
  declare const CompositionContext: React.Context<CompositionContext | undefined>;
33
36
  interface CompositionProviderProps {
34
37
  children: React.ReactNode;
35
38
  }
36
- export declare const CompositionProvider: ({ children }: CompositionProviderProps) => JSX.Element;
39
+ export declare const CompositionProvider: ({ children }: CompositionProviderProps) => React.JSX.Element;
37
40
  export declare function useComponent<T>(baseFunction: T): T;
38
41
  /**
39
42
  * This hook will throw an error if composition context doesn't exist.
package/Context.js CHANGED
@@ -30,7 +30,7 @@ function compose() {
30
30
  */
31
31
 
32
32
  var CompositionContext = /*#__PURE__*/(0, _react.createContext)(undefined);
33
- var CompositionProvider = function CompositionProvider(_ref) {
33
+ var CompositionProvider = exports.CompositionProvider = function CompositionProvider(_ref) {
34
34
  var children = _ref.children;
35
35
  var _useState = (0, _react.useState)(new Map()),
36
36
  _useState2 = (0, _slicedToArray2.default)(_useState, 2),
@@ -99,7 +99,6 @@ var CompositionProvider = function CompositionProvider(_ref) {
99
99
  value: context
100
100
  }, children);
101
101
  };
102
- exports.CompositionProvider = CompositionProvider;
103
102
  function useComponent(baseFunction) {
104
103
  var context = useOptionalComposition();
105
104
  var scope = (0, _CompositionScope.useCompositionScope)();
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","CompositionProvider","_ref","children","_useState","useState","Map","_useState2","_slicedToArray2","default","components","setComponents","composeComponent","useCallback","component","hocs","scope","prevComponents","scopeMap","get","recipe","newHocs","concat","_toConsumableArray2","set","apply","reverse","newHOCs","filter","hoc","includes","NewComponent","getComponent","Component","composedComponent","defaultScopeMap","defaultComponent","context","useMemo","createElement","Provider","value","exports","useComponent","baseFunction","useOptionalComposition","useCompositionScope","useComposition","useContext","Error"],"sources":["Context.tsx"],"sourcesContent":["import React, {\n ComponentType,\n useState,\n useCallback,\n createContext,\n useContext,\n useMemo\n} from \"react\";\nimport { useCompositionScope } from \"~/CompositionScope\";\nimport {\n ComposedFunction,\n ComposeWith,\n DecoratableComponent,\n DecoratableHook,\n Enumerable,\n GenericComponent,\n Decorator,\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);\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 CompositionContext {\n components: ComponentScopes;\n getComponent(component: ComponentType<unknown>, scope?: string): ComposedFunction | undefined;\n composeComponent(\n component: ComponentType<unknown>,\n hocs: Enumerable<ComposeWith>,\n scope?: string\n ): void;\n}\n\nconst CompositionContext = createContext<CompositionContext | undefined>(undefined);\n\ninterface CompositionProviderProps {\n children: React.ReactNode;\n}\n\nexport const CompositionProvider = ({ children }: CompositionProviderProps) => {\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] 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 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<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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ACHN,YAAY,EAAZA,YAAY;MACZpB,gBAAgB,EAAhBA,gBAAgB;MAChBF,UAAU,EAAVA;IACJ,CAAC;EAAA,CAAC,EACF,CAACA,UAAU,EAAEE,gBAAgB,CACjC,CAAC;EAED,oBAAO5B,MAAA,CAAAyB,OAAA,CAAA8B,aAAA,CAACzC,kBAAkB,CAAC0C,QAAQ;IAACC,KAAK,EAAEJ;EAAQ,GAAElC,QAAsC,CAAC;AAChG,CAAC;AAACuC,OAAA,CAAAzC,mBAAA,GAAAA,mBAAA;AAEK,SAAS0C,YAAYA,CAAIC,YAAe,EAAE;EAC7C,IAAMP,OAAO,GAAGQ,sBAAsB,CAAC,CAAC;EACxC,IAAM7B,KAAK,GAAG,IAAA8B,qCAAmB,EAAC,CAAC;EAEnC,IAAI,CAACT,OAAO,EAAE;IACV,OAAOO,YAAY;EACvB;EAEA,OAAQP,OAAO,CAACL,YAAY,CAACY,YAAY,EAAS5B,KAAK,CAAC,IAAI4B,YAAY;AAC5E;;AAEA;AACA;AACA;AACO,SAASG,cAAcA,CAAA,EAAG;EAC7B,IAAMV,OAAO,GAAG,IAAAW,iBAAU,EAAClD,kBAAkB,CAAC;EAC9C,IAAI,CAACuC,OAAO,EAAE;IACV,MAAM,IAAIY,KAAK,gFAEf,CAAC;EACL;EAEA,OAAOZ,OAAO;AAClB;;AAEA;AACA;AACA;AACO,SAASQ,sBAAsBA,CAAA,EAAG;EACrC,OAAO,IAAAG,iBAAU,EAAClD,kBAAkB,CAAC;AACzC"}
1
+ {"version":3,"names":["_react","_interopRequireWildcard","require","_CompositionScope","compose","_len","arguments","length","fns","Array","_key","decoratee","reduceRight","decorator","CompositionContext","createContext","undefined","CompositionProvider","exports","_ref","children","_useState","useState","Map","_useState2","_slicedToArray2","default","components","setComponents","composeComponent","useCallback","component","hocs","scope","prevComponents","scopeMap","get","recipe","newHocs","concat","_toConsumableArray2","set","apply","reverse","newHOCs","filter","hoc","includes","NewComponent","getComponent","Component","composedComponent","defaultScopeMap","defaultComponent","context","useMemo","createElement","Provider","value","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 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);\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\ninterface CompositionProviderProps {\n children: React.ReactNode;\n}\n\nexport const CompositionProvider = ({ children }: CompositionProviderProps) => {\n const [components, setComponents] = useState<ComponentScopes>(new Map());\n\n const composeComponent = useCallback(\n (\n component: GenericHook | GenericComponent,\n hocs: HigherOrderComponent<any, any>[],\n scope: string | undefined = \"*\"\n ) => {\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] 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 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 = useCallback<CompositionContextGetComponentCallable>(\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<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;AAYO,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,IAAMG,kBAAkB,gBAAG,IAAAC,oBAAa,EAAiCC,SAAS,CAAC;AAM5E,IAAMC,mBAAmB,GAAAC,OAAA,CAAAD,mBAAA,GAAG,SAAtBA,mBAAmBA,CAAAE,IAAA,EAA+C;EAAA,IAAzCC,QAAQ,GAAAD,IAAA,CAARC,QAAQ;EAC1C,IAAAC,SAAA,GAAoC,IAAAC,eAAQ,EAAkB,IAAIC,GAAG,CAAC,CAAC,CAAC;IAAAC,UAAA,OAAAC,eAAA,CAAAC,OAAA,EAAAL,SAAA;IAAjEM,UAAU,GAAAH,UAAA;IAAEI,aAAa,GAAAJ,UAAA;EAEhC,IAAMK,gBAAgB,GAAG,IAAAC,kBAAW,EAChC,UACIC,SAAyC,EACzCC,IAAsC,EAErC;IAAA,IADDC,KAAyB,GAAA3B,SAAA,CAAAC,MAAA,QAAAD,SAAA,QAAAU,SAA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CAACpB,SAAS,GAAGf,SAAS;EACtE,CAAC,EACD,CAACW,UAAU,CACf,CAAC;EAED,IAAM2B,OAA2B,GAAG,IAAAC,cAAO,EACvC;IAAA,OAAO;MACHN,YAAY,EAAZA,YAAY;MACZpB,gBAAgB,EAAhBA,gBAAgB;MAChBF,UAAU,EAAVA;IACJ,CAAC;EAAA,CAAC,EACF,CAACA,UAAU,EAAEE,gBAAgB,CACjC,CAAC;EAED,oBAAO7B,MAAA,CAAA0B,OAAA,CAAA8B,aAAA,CAAC1C,kBAAkB,CAAC2C,QAAQ;IAACC,KAAK,EAAEJ;EAAQ,GAAElC,QAAsC,CAAC;AAChG,CAAC;AAEM,SAASuC,YAAYA,CAAIC,YAAe,EAAE;EAC7C,IAAMN,OAAO,GAAGO,sBAAsB,CAAC,CAAC;EACxC,IAAM5B,KAAK,GAAG,IAAA6B,qCAAmB,EAAC,CAAC;EAEnC,IAAI,CAACR,OAAO,EAAE;IACV,OAAOM,YAAY;EACvB;EAEA,OAAQN,OAAO,CAACL,YAAY,CAACW,YAAY,EAAS3B,KAAK,CAAC,IAAI2B,YAAY;AAC5E;;AAEA;AACA;AACA;AACO,SAASG,cAAcA,CAAA,EAAG;EAC7B,IAAMT,OAAO,GAAG,IAAAU,iBAAU,EAAClD,kBAAkB,CAAC;EAC9C,IAAI,CAACwC,OAAO,EAAE;IACV,MAAM,IAAIW,KAAK,gFAEf,CAAC;EACL;EAEA,OAAOX,OAAO;AAClB;;AAEA;AACA;AACA;AACO,SAASO,sBAAsBA,CAAA,EAAG;EACrC,OAAO,IAAAG,iBAAU,EAAClD,kBAAkB,CAAC;AACzC","ignoreList":[]}
@@ -1,4 +1,4 @@
1
- /// <reference types="react" />
1
+ import React from "react";
2
2
  import { CanReturnNull, Decoratable, DecoratableComponent, DecoratableHook, Decorator } from "./types";
3
3
  declare type GetBaseFunction<T> = T extends DecoratableComponent<infer F> ? F : never;
4
4
  /**
@@ -7,13 +7,13 @@ declare type GetBaseFunction<T> = T extends DecoratableComponent<infer F> ? F :
7
7
  * For more information, visit https://www.webiny.com/docs/admin-area/basics/framework.
8
8
  */
9
9
  export declare function createComponentPlugin<T extends Decoratable>(Base: T, hoc: T extends DecoratableComponent ? Decorator<CanReturnNull<GetBaseFunction<T>>> : Decorator<GetBaseFunction<T>>): {
10
- (): JSX.Element;
11
- displayName: string | undefined;
10
+ (): React.JSX.Element;
11
+ displayName: string;
12
12
  };
13
13
  export declare type GetDecorateeParams<T> = T extends (params?: infer P1) => any ? P1 : T extends (params: infer P2) => any ? P2 : any;
14
14
  export declare type GetDecoratee<T> = T extends DecoratableHook<infer F> ? F : T extends DecoratableComponent<infer F> ? F : never;
15
15
  export declare function createDecorator<T extends Decoratable>(Base: T, hoc: T extends DecoratableComponent ? Decorator<CanReturnNull<GetBaseFunction<T>>> : Decorator<GetBaseFunction<T>>): {
16
- (): JSX.Element;
17
- displayName: string | undefined;
16
+ (): React.JSX.Element;
17
+ displayName: string;
18
18
  };
19
19
  export {};
@@ -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 CanReturnNull,\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<CanReturnNull<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<CanReturnNull<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"}
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 CanReturnNull,\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<CanReturnNull<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<CanReturnNull<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":[]}
package/decorators.d.ts CHANGED
@@ -1,14 +1,22 @@
1
- /// <reference types="react" />
1
+ import React from "react";
2
2
  import { GetDecoratee, GetDecorateeParams } from "./createDecorator";
3
- import { DecoratableComponent, GenericComponent, Decorator, CanReturnNull } from "./types";
3
+ import { DecoratableComponent, GenericComponent, Decorator, CanReturnNull, GenericHook, DecoratableHook } from "./types";
4
4
  export interface ShouldDecorate<TDecorator = any, TComponent = any> {
5
5
  (decoratorProps: TDecorator, componentProps: TComponent): boolean;
6
6
  }
7
7
  export declare function createConditionalDecorator<TDecoratee extends GenericComponent>(shouldDecorate: ShouldDecorate, decorator: Decorator<TDecoratee>, decoratorProps: unknown): Decorator<TDecoratee>;
8
- export declare function createDecoratorFactory<TDecorator>(): <TDecoratable extends DecoratableComponent<GenericComponent<any>>>(decoratable: TDecoratable, shouldDecorate?: ShouldDecorate<TDecorator, GetDecorateeParams<GetDecoratee<TDecoratable>>> | undefined) => (decorator: Decorator<CanReturnNull<GetDecoratee<TDecoratable>>>) => (props: TDecorator) => JSX.Element;
8
+ export declare function createDecoratorFactory<TDecorator>(): <TDecoratable extends DecoratableComponent<GenericComponent<any>>>(decoratable: TDecoratable, shouldDecorate?: ShouldDecorate<TDecorator, GetDecorateeParams<GetDecoratee<TDecoratable>>> | undefined) => (decorator: Decorator<CanReturnNull<GetDecoratee<TDecoratable>>>) => (props: TDecorator) => React.JSX.Element;
9
+ export declare function createHookDecoratorFactory(): <TDecoratable extends DecoratableHook<GenericHook<any, any>>>(decoratable: TDecoratable) => (decorator: Decorator<GetDecoratee<TDecoratable>>) => () => React.JSX.Element;
9
10
  export declare function withDecoratorFactory<TDecorator>(): <TDecoratable extends DecoratableComponent<GenericComponent<any>>>(Component: TDecoratable, shouldDecorate?: ShouldDecorate<TDecorator, GetDecorateeParams<GetDecoratee<TDecoratable>>> | undefined) => GenericComponent<GetDecorateeParams<GetDecoratee<TDecoratable>>> & {
10
11
  original: GenericComponent<GetDecorateeParams<GetDecoratee<TDecoratable>>>;
11
12
  originalName: string;
13
+ displayName: string;
12
14
  } & {
13
- createDecorator: (decorator: Decorator<CanReturnNull<GetDecoratee<TDecoratable>>>) => (props: TDecorator) => JSX.Element;
15
+ createDecorator: (decorator: Decorator<CanReturnNull<GetDecoratee<TDecoratable>>>) => (props: TDecorator) => React.JSX.Element;
16
+ };
17
+ export declare function withHookDecoratorFactory(): <TDecoratable extends DecoratableHook<GenericHook<any, any>>>(hook: TDecoratable) => GenericHook<GetDecorateeParams<GetDecoratee<TDecoratable>>, ReturnType<GetDecoratee<TDecoratable>>> & {
18
+ original: GenericHook<GetDecorateeParams<GetDecoratee<TDecoratable>>, ReturnType<GetDecoratee<TDecoratable>>>;
19
+ originalName: string;
20
+ } & {
21
+ createDecorator: (decorator: Decorator<GetDecoratee<TDecoratable>>) => () => React.JSX.Element;
14
22
  };
package/decorators.js CHANGED
@@ -6,12 +6,15 @@ Object.defineProperty(exports, "__esModule", {
6
6
  });
7
7
  exports.createConditionalDecorator = createConditionalDecorator;
8
8
  exports.createDecoratorFactory = createDecoratorFactory;
9
+ exports.createHookDecoratorFactory = createHookDecoratorFactory;
9
10
  exports.withDecoratorFactory = withDecoratorFactory;
11
+ exports.withHookDecoratorFactory = withHookDecoratorFactory;
10
12
  var _react = _interopRequireDefault(require("react"));
11
13
  var _Compose = require("./Compose");
12
14
  function createConditionalDecorator(shouldDecorate, decorator, decoratorProps) {
13
15
  return function (Original) {
14
- var DecoratedComponent = decorator(Original);
16
+ var DecoratedComponent = /*#__PURE__*/_react.default.memo(decorator(Original));
17
+ DecoratedComponent.displayName = Original.displayName;
15
18
  return function ShouldDecorate(props) {
16
19
  if (shouldDecorate(decoratorProps, props)) {
17
20
  // @ts-expect-error
@@ -23,6 +26,11 @@ function createConditionalDecorator(shouldDecorate, decorator, decoratorProps) {
23
26
  };
24
27
  };
25
28
  }
29
+ var memoizedComponent = function memoizedComponent(decorator) {
30
+ return function (decoratee) {
31
+ return /*#__PURE__*/_react.default.memo(decorator(decoratee));
32
+ };
33
+ };
26
34
  function createDecoratorFactory() {
27
35
  return function from(decoratable, shouldDecorate) {
28
36
  return function createDecorator(decorator) {
@@ -34,6 +42,18 @@ function createDecoratorFactory() {
34
42
  with: componentDecorator
35
43
  });
36
44
  }
45
+ return /*#__PURE__*/_react.default.createElement(_Compose.Compose, {
46
+ function: decoratable,
47
+ with: memoizedComponent(decorator)
48
+ });
49
+ };
50
+ };
51
+ };
52
+ }
53
+ function createHookDecoratorFactory() {
54
+ return function from(decoratable) {
55
+ return function createDecorator(decorator) {
56
+ return function DecoratorPlugin() {
37
57
  return /*#__PURE__*/_react.default.createElement(_Compose.Compose, {
38
58
  function: decoratable,
39
59
  with: decorator
@@ -50,5 +70,13 @@ function withDecoratorFactory() {
50
70
  });
51
71
  };
52
72
  }
73
+ function withHookDecoratorFactory() {
74
+ return function WithHookDecorator(hook) {
75
+ var createDecorator = createHookDecoratorFactory()(hook);
76
+ return Object.assign(hook, {
77
+ createDecorator: createDecorator
78
+ });
79
+ };
80
+ }
53
81
 
54
82
  //# sourceMappingURL=decorators.js.map
package/decorators.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"names":["_react","_interopRequireDefault","require","_Compose","createConditionalDecorator","shouldDecorate","decorator","decoratorProps","Original","DecoratedComponent","ShouldDecorate","props","default","createElement","createDecoratorFactory","from","decoratable","createDecorator","DecoratorPlugin","componentDecorator","Compose","function","with","withDecoratorFactory","WithDecorator","Component","Object","assign"],"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 CanReturnNull,\n GenericHook\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 = decorator(Original);\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\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(\n decorator: Decorator<CanReturnNull<GetDecoratee<TDecoratable>>>\n ) {\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={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 unknown as DecoratableComponent<\n GenericComponent<GetDecorateeParams<GetDecoratee<TDecoratable>>>\n > & { createDecorator: typeof createDecorator };\n };\n}\n"],"mappings":";;;;;;;;;AAAA,IAAAA,MAAA,GAAAC,sBAAA,CAAAC,OAAA;AACA,IAAAC,QAAA,GAAAD,OAAA;AAcO,SAASE,0BAA0BA,CACtCC,cAA8B,EAC9BC,SAAgC,EAChCC,cAAuB,EACF;EACrB,OAAQ,UAAAC,QAAQ,EAAI;IAChB,IAAMC,kBAAkB,GAAGH,SAAS,CAACE,QAAQ,CAAC;IAE9C,OAAO,SAASE,cAAcA,CAACC,KAAc,EAAE;MAC3C,IAAIN,cAAc,CAACE,cAAc,EAAEI,KAAK,CAAC,EAAE;QACvC;QACA,oBAAOX,MAAA,CAAAY,OAAA,CAAAC,aAAA,CAACJ,kBAAkB,EAAKE,KAAQ,CAAC;MAC5C;;MAEA;MACA,oBAAOX,MAAA,CAAAY,OAAA,CAAAC,aAAA,CAACL,QAAQ,EAAKG,KAAQ,CAAC;IAClC,CAAC;EACL,CAAC;AACL;AAEO,SAASG,sBAAsBA,CAAA,EAAe;EACjD,OAAO,SAASC,IAAIA,CAChBC,WAAyB,EACzBX,cAA2F,EAC7F;IACE,OAAO,SAASY,eAAeA,CAC3BX,SAA+D,EACjE;MACE,OAAO,SAASY,eAAeA,CAACP,KAAiB,EAAE;QAC/C,IAAIN,cAAc,EAAE;UAChB,IAAMc,kBAAkB,GAAGf,0BAA0B,CACjDC,cAAc,EACdC,SAAS,EACTK,KACJ,CAAC;UAED,oBAAOX,MAAA,CAAAY,OAAA,CAAAC,aAAA,CAACV,QAAA,CAAAiB,OAAO;YAACC,QAAQ,EAAEL,WAAY;YAACM,IAAI,EAAEH;UAAmB,CAAE,CAAC;QACvE;QAEA,oBACInB,MAAA,CAAAY,OAAA,CAAAC,aAAA,CAACV,QAAA,CAAAiB,OAAO;UACJC,QAAQ,EAAEL,WAAY;UACtBM,IAAI,EAAEhB;QAA+C,CACxD,CAAC;MAEV,CAAC;IACL,CAAC;EACL,CAAC;AACL;AAEO,SAASiB,oBAAoBA,CAAA,EAAe;EAC/C,OAAO,SAASC,aAAaA,CACzBC,SAAuB,EACvBpB,cAA2F,EAC7F;IACE,IAAMY,eAAe,GAAGH,sBAAsB,CAAa,CAAC,CAACW,SAAS,EAAEpB,cAAc,CAAC;IAEvF,OAAOqB,MAAM,CAACC,MAAM,CAACF,SAAS,EAAE;MAAER,eAAe,EAAfA;IAAgB,CAAC,CAAC;EAGxD,CAAC;AACL"}
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 CanReturnNull,\n GenericHook,\n DecoratableHook\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(\n decorator: Decorator<CanReturnNull<GetDecoratee<TDecoratable>>>\n ) {\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 unknown as DecoratableComponent<\n GenericComponent<GetDecorateeParams<GetDecoratee<TDecoratable>>>\n > & { createDecorator: typeof createDecorator };\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,kBAAkB,EAAKK,KAAQ,CAAC;MAC5C;;MAEA;MACA,oBAAOd,MAAA,CAAAe,OAAA,CAAAC,aAAA,CAACR,QAAQ,EAAKM,KAAQ,CAAC;IAClC,CAAC;EACL,CAAC;AACL;AAEA,IAAMG,iBAAiB,GAAG,SAApBA,iBAAiBA,CAAgCX,SAAuB,EAAK;EAC/E,OAAO,UAACY,SAAY,EAAK;IACrB,oBAAOR,cAAK,CAACC,IAAI,CAACL,SAAS,CAACY,SAAS,CAAC,CAAC;EAC3C,CAAC;AACL,CAAC;AAEM,SAASC,sBAAsBA,CA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AGxD,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":[]}
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,UAAA;IAAAC,GAAA,WAAAA,IAAA;MAAA,OAAAT,QAAA,CAAAK,GAAA;IAAA;EAAA;AAAA;AACA,IAAAK,QAAA,GAAAT,OAAA;AAAAC,MAAA,CAAAC,IAAA,CAAAO,QAAA,EAAAN,OAAA,WAAAC,GAAA;EAAA,IAAAA,GAAA,kBAAAA,GAAA;EAAA,IAAAA,GAAA,IAAAC,OAAA,IAAAA,OAAA,CAAAD,GAAA,MAAAK,QAAA,CAAAL,GAAA;EAAAH,MAAA,CAAAK,cAAA,CAAAD,OAAA,EAAA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AAA,CAAAC,IAAA,CAAAW,WAAA,EAAAV,OAAA,WAAAC,GAAA;EAAA,IAAAA,GAAA,kBAAAA,GAAA;EAAA,IAAAA,GAAA,IAAAC,OAAA,IAAAA,OAAA,CAAAD,GAAA,MAAAS,WAAA,CAAAT,GAAA;EAAAH,MAAA,CAAAK,cAAA,CAAAD,OAAA,EAAAD,GAAA;IAAAG,UAAA;IAAAC,GAAA,WAAAA,IAAA;MAAA,OAAAK,WAAA,CAAAT,GAAA;IAAA;EAAA;AAAA;AACA,IAAAU,iBAAA,GAAAd,OAAA;AAAAC,MAAA,CAAAC,IAAA,CAAAY,iBAAA,EAAAX,OAAA,WAAAC,GAAA;EAAA,IAAAA,GAAA,kBAAAA,GAAA;EAAA,IAAAA,GAAA,IAAAC,OAAA,IAAAA,OAAA,CAAAD,GAAA,MAAAU,iBAAA,CAAAV,GAAA;EAAAH,MAAA,CAAAK,cAAA,CAAAD,OAAA,EAAAD,GAAA;IAAAG,UAAA;IAAAC,GAAA,WAAAA,IAAA;MAAA,OAAAM,iBAAA,CAAAV,GAAA;IAAA;EAAA;AAAA;AACA,IAAAW,MAAA,GAAAf,OAAA;AAAAC,MAAA,CAAAC,IAAA,CAAAa,MAAA,EAAAZ,OAAA,WAAAC,GAAA;EAAA,IAAAA,GAAA,kBAAAA,GAAA;EAAA,IAAAA,GAAA,IAAAC,OAAA,IAAAA,OAAA,CAAAD,GAAA,MAAAW,MAAA,CAAAX,GAAA;EAAAH,MAAA,CAAAK,cAAA,CAAAD,OAAA,EAAAD,GAAA;IAAAG,UAAA;IAAAC,GAAA,WAAAA,IAAA;MAAA,OAAAO,MAAA,CAAAX,GAAA;IAAA;EAAA;AAAA"}
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,UAAA;IAAAC,GAAA,WAAAA,IAAA;MAAA,OAAAT,QAAA,CAAAK,GAAA;IAAA;EAAA;AAAA;AACA,IAAAK,QAAA,GAAAT,OAAA;AAAAC,MAAA,CAAAC,IAAA,CAAAO,QAAA,EAAAN,OAAA,WAAAC,GAAA;EAAA,IAAAA,GAAA,kBAAAA,GAAA;EAAA,IAAAA,GAAA,IAAAC,OAAA,IAAAA,OAAA,CAAAD,GAAA,MAAAK,QAAA,CAAAL,GAAA;EAAAH,MAAA,CAAAK,cAAA,CAAAD,OAAA,EAAAD,GAAA;IAAAG,UAAA;IAAAC,GAAA,WAAAA,IAAA;MAAA,OAAAC,QAAA,CAAAL,GAAA;IAAA;EAAA;AAAA;AACA,IAAAM,eAAA,GAAAV,OAAA;AAAAC,MAAA,CAAAC,IAAA,CAAAQ,eAAA,EAAAP,OAAA,WAAAC,GAAA;EAAA,IAAAA,GAAA,kBAAAA,GAAA;EAAA,IAAAA,GAAA,IAAAC,OAAA,IAAAA,OAAA,CAAAD,GAAA,MAAAM,eAAA,CAAAN,GAAA;EAAAH,MAAA,CAAAK,cAAA,CAAAD,OAAA,EAAAD,GAAA;IAAAG,UAAA;IAAAC,GAAA,WAAAA,IAAA;MAAA,OAAAE,eAAA,CAAAN,GAAA;IAAA;EAAA;AAAA;AACA,IAAAO,gBAAA,GAAAX,OAAA;AAAAC,M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AAAD,GAAA,MAAAU,iBAAA,CAAAV,GAAA;EAAAH,MAAA,CAAAK,cAAA,CAAAD,OAAA,EAAAD,GAAA;IAAAG,UAAA;IAAAC,GAAA,WAAAA,IAAA;MAAA,OAAAM,iBAAA,CAAAV,GAAA;IAAA;EAAA;AAAA;AACA,IAAAW,MAAA,GAAAf,OAAA;AAAAC,MAAA,CAAAC,IAAA,CAAAa,MAAA,EAAAZ,OAAA,WAAAC,GAAA;EAAA,IAAAA,GAAA,kBAAAA,GAAA;EAAA,IAAAA,GAAA,IAAAC,OAAA,IAAAA,OAAA,CAAAD,GAAA,MAAAW,MAAA,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":[]}
@@ -16,11 +16,12 @@ export declare function makeComposable<T extends GenericComponent>(name: string,
16
16
  displayName: string;
17
17
  }>>>>;
18
18
  originalName: string;
19
+ displayName: string;
19
20
  } & {
20
21
  createDecorator: (decorator: import("./types").Decorator<import("./types").CanReturnNull<import("./createDecorator").GetDecoratee<import("./types").DecoratableComponent<{
21
22
  (props: [] | Parameters<NonNullable<T>> extends [infer First] ? undefined extends First ? any : First : any): JSX.Element | null;
22
23
  original: (() => null) | NonNullable<T>;
23
24
  originalName: string;
24
25
  displayName: string;
25
- }>>>>) => (props: unknown) => JSX.Element;
26
+ }>>>>) => (props: unknown) => import("react").JSX.Element;
26
27
  };
@@ -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"}
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,10 +1,8 @@
1
- /// <reference types="react" />
1
+ import React from "react";
2
2
  import { DecoratableComponent, DecoratableHook, GenericComponent, GenericHook } from "./types";
3
3
  declare type NoProps = any;
4
4
  declare type GetProps<T extends (...args: any) => any> = Parameters<T> extends [infer First] ? undefined extends First ? NoProps : First : NoProps;
5
- export declare function makeDecoratableHook<T extends GenericHook>(hook: T): DecoratableHook<T>;
6
- export declare function createVoidComponent<T>(): (props: T) => JSX.Element | null;
7
- export declare function makeDecoratable<T extends GenericComponent>(name: string, Component: T): GenericComponent<import("./createDecorator").GetDecorateeParams<import("./createDecorator").GetDecoratee<DecoratableComponent<{
5
+ declare function makeDecoratableComponent<T extends GenericComponent>(name: string, Component?: T): GenericComponent<import("./createDecorator").GetDecorateeParams<import("./createDecorator").GetDecoratee<DecoratableComponent<{
8
6
  (props: GetProps<T>): JSX.Element | null;
9
7
  original: T;
10
8
  originalName: string;
@@ -17,12 +15,22 @@ export declare function makeDecoratable<T extends GenericComponent>(name: string
17
15
  displayName: string;
18
16
  }>>>>;
19
17
  originalName: string;
18
+ displayName: string;
20
19
  } & {
21
20
  createDecorator: (decorator: import("./types").Decorator<import("./types").CanReturnNull<import("./createDecorator").GetDecoratee<DecoratableComponent<{
22
21
  (props: GetProps<T>): JSX.Element | null;
23
22
  original: T;
24
23
  originalName: string;
25
24
  displayName: string;
26
- }>>>>) => (props: unknown) => JSX.Element;
25
+ }>>>>) => (props: unknown) => React.JSX.Element;
26
+ };
27
+ export declare function makeDecoratableHook<T extends GenericHook>(hook: T): GenericHook<import("./createDecorator").GetDecorateeParams<import("./createDecorator").GetDecoratee<DecoratableHook<T>>>, ReturnType<import("./createDecorator").GetDecoratee<DecoratableHook<T>>>> & {
28
+ original: GenericHook<import("./createDecorator").GetDecorateeParams<import("./createDecorator").GetDecoratee<DecoratableHook<T>>>, ReturnType<import("./createDecorator").GetDecoratee<DecoratableHook<T>>>>;
29
+ originalName: string;
30
+ } & {
31
+ createDecorator: (decorator: import("./types").Decorator<import("./createDecorator").GetDecoratee<DecoratableHook<T>>>) => () => React.JSX.Element;
27
32
  };
33
+ export declare function createVoidComponent<T>(): (props: T) => JSX.Element | null;
34
+ export declare function makeDecoratable<T extends GenericHook>(hook: T): ReturnType<typeof makeDecoratableHook<T>>;
35
+ export declare function makeDecoratable<T extends GenericComponent>(name: string, Component: T): ReturnType<typeof makeDecoratableComponent<T>>;
28
36
  export {};
@@ -50,7 +50,7 @@ function makeDecoratableHook(hook) {
50
50
  return composedHook(params);
51
51
  };
52
52
  decoratableHook.original = hook;
53
- return decoratableHook;
53
+ return (0, _decorators.withHookDecoratorFactory)()(decoratableHook);
54
54
  }
55
55
  function createVoidComponent() {
56
56
  // eslint-disable-next-line @typescript-eslint/no-unused-vars
@@ -58,8 +58,11 @@ function createVoidComponent() {
58
58
  return null;
59
59
  };
60
60
  }
61
- function makeDecoratable(name, Component) {
62
- return makeDecoratableComponent(name, Component);
61
+ function makeDecoratable(hookOrName, Component) {
62
+ if (Component) {
63
+ return makeDecoratableComponent(hookOrName, /*#__PURE__*/_react.default.memo(Component));
64
+ }
65
+ return makeDecoratableHook(hookOrName);
63
66
  }
64
67
 
65
68
  //# sourceMappingURL=makeDecoratable.js.map
@@ -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","original","originalName","withDecoratorFactory","makeDecoratableHook","hook","decoratableHook","params","composedHook","createVoidComponent","makeDecoratable"],"sources":["makeDecoratable.tsx"],"sourcesContent":["import React, { createContext, useContext, useMemo } from \"react\";\nimport { useComponent } from \"./Context\";\nimport { DecoratableComponent, DecoratableHook, GenericComponent, GenericHook } from \"~/types\";\nimport { withDecoratorFactory } 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\n// Maybe there's a better way to mark props as non-existent, but for now I left it as `any`.\ntype NoProps = any;\n\ntype GetProps<T extends (...args: any) => any> = Parameters<T> extends [infer First]\n ? undefined extends First\n ? NoProps\n : First\n : NoProps;\n\nfunction makeDecoratableComponent<T extends GenericComponent>(\n name: string,\n Component: T = nullRenderer as unknown as T\n) {\n const Decoratable = (props: GetProps<T>): JSX.Element | null => {\n const parents = useComposableParents();\n const ComposedComponent = useComponent(Component);\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 Decoratable.original = Component;\n Decoratable.originalName = name;\n Decoratable.displayName = `Decoratable<${name}>`;\n\n return withDecoratorFactory()(Decoratable as DecoratableComponent<typeof Decoratable>);\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 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 GenericComponent>(name: string, Component: T) {\n return makeDecoratableComponent(name, Component);\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,C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gB,mBAAmBA,CAAwBC,IAAO,EAAE;EAChE,IAAMC,eAAe,GAAG,SAAlBA,eAAeA,CAAIC,MAAqB,EAAK;IAC/C,IAAMC,YAAY,GAAG,IAAAhB,qBAAY,EAACa,IAAI,CAAC;IAEvC,OAAOG,YAAY,CAACD,MAAM,CAAC;EAC/B,CAAC;EAEDD,eAAe,CAACL,QAAQ,GAAGI,IAAI;EAE/B,OAAOC,eAAe;AAC1B;AAEO,SAASG,mBAAmBA,CAAA,EAAM;EACrC;EACA,OAAO,UAACpB,KAAQ,EAAyB;IACrC,OAAO,IAAI;EACf,CAAC;AACL;AAEO,SAASqB,eAAeA,CAA6B3B,IAAY,EAAEC,SAAY,EAAE;EACpF,OAAOF,wBAAwB,CAACC,IAAI,EAAEC,SAAS,CAAC;AACpD"}
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","original","originalName","withDecoratorFactory","makeDecoratableHook","hook","decoratableHook","params","composedHook","withHookDecoratorFactory","createVoidComponent","makeDecoratable","hookOrName","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\n// Maybe there's a better way to mark props as non-existent, but for now I left it as `any`.\ntype NoProps = any;\n\ntype GetProps<T extends (...args: any) => any> = Parameters<T> extends [infer First]\n ? undefined extends First\n ? NoProps\n : First\n : NoProps;\n\nfunction makeDecoratableComponent<T extends GenericComponent>(\n name: string,\n Component: T = nullRenderer as unknown as T\n) {\n const Decoratable = (props: GetProps<T>): JSX.Element | null => {\n const parents = useComposableParents();\n const ComposedComponent = useComponent(Component);\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 Decoratable.original = Component;\n Decoratable.originalName = name;\n Decoratable.displayName = `Decoratable<${name}>`;\n\n return withDecoratorFactory()(Decoratable as DecoratableComponent<typeof Decoratable>);\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 return makeDecoratableComponent(hookOrName, React.memo(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,IAAA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Y,GAAG,IAAAhB,qBAAY,EAACa,IAAI,CAAC;IAEvC,OAAOG,YAAY,CAACD,MAAM,CAAC;EAC/B,CAAC;EAEDD,eAAe,CAACL,QAAQ,GAAGI,IAAI;EAE/B,OAAO,IAAAI,oCAAwB,EAAC,CAAC,CAACH,eAAqC,CAAC;AAC5E;AAEO,SAASI,mBAAmBA,CAAA,EAAM;EACrC;EACA,OAAO,UAACrB,KAAQ,EAAyB;IACrC,OAAO,IAAI;EACf,CAAC;AACL;AASO,SAASsB,eAAeA,CAACC,UAAe,EAAE5B,SAAe,EAAE;EAC9D,IAAIA,SAAS,EAAE;IACX,OAAOF,wBAAwB,CAAC8B,UAAU,eAAEC,cAAK,CAACC,IAAI,CAAC9B,SAAS,CAAC,CAAC;EACtE;EAEA,OAAOoB,mBAAmB,CAACQ,UAAU,CAAC;AAC1C","ignoreList":[]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@webiny/react-composition",
3
- "version": "5.40.0-beta.1",
3
+ "version": "5.40.0-beta.3",
4
4
  "main": "index.js",
5
5
  "repository": {
6
6
  "type": "git",
@@ -14,19 +14,19 @@
14
14
  ],
15
15
  "license": "MIT",
16
16
  "dependencies": {
17
- "@babel/runtime": "7.22.6",
18
- "@types/react": "17.0.39",
19
- "react": "17.0.2",
20
- "react-dom": "17.0.2"
17
+ "@babel/runtime": "7.24.1",
18
+ "@types/react": "18.2.79",
19
+ "react": "18.2.0",
20
+ "react-dom": "18.2.0"
21
21
  },
22
22
  "devDependencies": {
23
- "@babel/cli": "7.22.6",
24
- "@babel/core": "7.22.8",
25
- "@babel/preset-env": "7.22.7",
26
- "@babel/preset-typescript": "7.22.5",
27
- "@testing-library/react": "12.1.5",
28
- "@webiny/cli": "5.40.0-beta.1",
29
- "@webiny/project-utils": "5.40.0-beta.1",
23
+ "@babel/cli": "7.24.1",
24
+ "@babel/core": "7.24.3",
25
+ "@babel/preset-env": "7.24.3",
26
+ "@babel/preset-typescript": "7.24.1",
27
+ "@testing-library/react": "15.0.7",
28
+ "@webiny/cli": "5.40.0-beta.3",
29
+ "@webiny/project-utils": "5.40.0-beta.3",
30
30
  "ttypescript": "1.5.15",
31
31
  "typescript": "4.7.4"
32
32
  },
@@ -38,5 +38,5 @@
38
38
  "build": "yarn webiny run build",
39
39
  "watch": "yarn webiny run watch"
40
40
  },
41
- "gitHead": "cadaa7aa80ddff01df7ecb3b356f2be9432e14e0"
41
+ "gitHead": "638d8b84063906cd1aa979ed6e0487ad26fbcf2f"
42
42
  }
package/types.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import React from "react";
2
- export declare type GenericHook = (...args: any) => any;
2
+ export declare type GenericHook<TParams = any, TReturn = any> = (...args: TParams[]) => TReturn;
3
3
  export declare type GenericComponent<T = any> = React.FunctionComponent<T>;
4
4
  export declare type ComposedFunction = GenericHook;
5
5
  export declare type Decorator<T> = (decoratee: T) => T;
@@ -20,6 +20,7 @@ export declare type DecoratableHook<T extends GenericHook = GenericHook> = T & {
20
20
  export declare type DecoratableComponent<T = GenericComponent> = T & {
21
21
  original: T;
22
22
  originalName: string;
23
+ displayName: string;
23
24
  };
24
25
  export declare type Decoratable = DecoratableComponent | DecoratableHook;
25
26
  /**
package/types.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"names":[],"sources":["types.ts"],"sourcesContent":["import React from \"react\";\n\nexport type GenericHook = (...args: any) => any;\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 * @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};\n\nexport type Decoratable = DecoratableComponent | DecoratableHook;\n\n/**\n * @internal Add `null` to the ReturnType of the given function.\n */\nexport type CanReturnNull<T> = T extends (...args: any) => any\n ? (...args: Parameters<T>) => ReturnType<T> | null\n : never;\n"],"mappings":""}
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 * @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 CanReturnNull<T> = T extends (...args: any) => any\n ? (...args: Parameters<T>) => ReturnType<T> | null\n : never;\n"],"mappings":"","ignoreList":[]}