@webiny/react-composition 6.0.0-beta.0 → 6.0.0-rc.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 +4 -3
- package/Compose.js +63 -21
- package/Compose.js.map +1 -1
- package/CompositionScope.d.ts +9 -3
- package/CompositionScope.js +18 -19
- package/CompositionScope.js.map +1 -1
- package/Context.d.ts +17 -31
- package/Context.js +66 -104
- package/Context.js.map +1 -1
- package/README.md +9 -6
- package/createDecorator.d.ts +6 -6
- package/createDecorator.js +9 -19
- package/createDecorator.js.map +1 -1
- package/decorators.d.ts +7 -14
- package/decorators.js +22 -33
- package/decorators.js.map +1 -1
- package/domain/CompositionStore.d.ts +15 -0
- package/domain/CompositionStore.js +102 -0
- package/domain/CompositionStore.js.map +1 -0
- package/index.d.ts +9 -8
- package/index.js +8 -93
- package/index.js.map +1 -1
- package/makeComposable.d.ts +30 -14
- package/makeComposable.js +6 -14
- package/makeComposable.js.map +1 -1
- package/makeDecoratable.d.ts +10 -15
- package/makeDecoratable.js +29 -44
- package/makeDecoratable.js.map +1 -1
- package/package.json +6 -15
- package/types.d.ts +18 -12
- package/types.js +1 -5
- package/types.js.map +1 -1
package/makeDecoratable.js
CHANGED
|
@@ -1,66 +1,51 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
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.js";
|
|
3
|
+
import { withDecoratorFactory, withHookDecoratorFactory } from "./decorators.js";
|
|
4
|
+
const ComposableContext = /*#__PURE__*/createContext([]);
|
|
16
5
|
ComposableContext.displayName = "ComposableContext";
|
|
17
6
|
function useComposableParents() {
|
|
18
|
-
|
|
7
|
+
const context = useContext(ComposableContext);
|
|
19
8
|
if (!context) {
|
|
20
9
|
return [];
|
|
21
10
|
}
|
|
22
11
|
return context;
|
|
23
12
|
}
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
var Component = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : nullRenderer;
|
|
32
|
-
var Decoratable = function Decoratable(props) {
|
|
33
|
-
var parents = useComposableParents();
|
|
34
|
-
var ComposedComponent = (0, _Context.useComponent)(Component);
|
|
35
|
-
var context = (0, _react.useMemo)(function () {
|
|
36
|
-
return [].concat((0, _toConsumableArray2.default)(parents), [name]);
|
|
37
|
-
}, [parents, name]);
|
|
38
|
-
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, {
|
|
39
20
|
value: context
|
|
40
|
-
}, /*#__PURE__*/
|
|
21
|
+
}, /*#__PURE__*/React.createElement(ComposedComponent, props, props.children));
|
|
22
|
+
};
|
|
23
|
+
const staticProps = {
|
|
24
|
+
original: Component,
|
|
25
|
+
originalName: name,
|
|
26
|
+
displayName: `Decoratable<${name}>`
|
|
41
27
|
};
|
|
42
|
-
Decoratable
|
|
43
|
-
Decoratable.originalName = name;
|
|
44
|
-
Decoratable.displayName = "Decoratable<".concat(name, ">");
|
|
45
|
-
return (0, _decorators.withDecoratorFactory)()(Decoratable);
|
|
28
|
+
return withDecoratorFactory()(Object.assign(Decoratable, staticProps));
|
|
46
29
|
}
|
|
47
|
-
function makeDecoratableHook(hook) {
|
|
48
|
-
|
|
49
|
-
|
|
30
|
+
export function makeDecoratableHook(hook) {
|
|
31
|
+
const decoratableHook = params => {
|
|
32
|
+
const composedHook = useComponent(hook);
|
|
50
33
|
return composedHook(params);
|
|
51
34
|
};
|
|
52
35
|
decoratableHook.original = hook;
|
|
53
|
-
return
|
|
36
|
+
return withHookDecoratorFactory()(decoratableHook);
|
|
54
37
|
}
|
|
55
|
-
function createVoidComponent() {
|
|
38
|
+
export function createVoidComponent() {
|
|
56
39
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
57
|
-
return
|
|
40
|
+
return props => {
|
|
58
41
|
return null;
|
|
59
42
|
};
|
|
60
43
|
}
|
|
61
|
-
function makeDecoratable(hookOrName, Component) {
|
|
44
|
+
export function makeDecoratable(hookOrName, Component) {
|
|
62
45
|
if (Component) {
|
|
63
|
-
|
|
46
|
+
const component = makeDecoratableComponent(hookOrName, /*#__PURE__*/React.memo(Component));
|
|
47
|
+
component.original.displayName = hookOrName;
|
|
48
|
+
return component;
|
|
64
49
|
}
|
|
65
50
|
return makeDecoratableHook(hookOrName);
|
|
66
51
|
}
|
package/makeDecoratable.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["
|
|
1
|
+
{"version":3,"names":["React","createContext","useContext","useMemo","useComponent","withDecoratorFactory","withHookDecoratorFactory","ComposableContext","displayName","useComposableParents","context","nullRenderer","makeDecoratableComponent","name","Component","Decoratable","props","parents","ComposedComponent","createElement","Provider","value","children","staticProps","original","originalName","Object","assign","makeDecoratableHook","hook","decoratableHook","params","composedHook","createVoidComponent","makeDecoratable","hookOrName","component","memo"],"sources":["makeDecoratable.tsx"],"sourcesContent":["import React, { createContext, useContext, useMemo } from \"react\";\nimport { useComponent } from \"./Context.js\";\nimport type {\n DecoratableComponent,\n DecoratableHook,\n GenericComponent,\n GenericHook\n} from \"~/types.js\";\nimport { withDecoratorFactory, withHookDecoratorFactory } from \"~/decorators.js\";\n\nconst ComposableContext = createContext<string[]>([]);\nComposableContext.displayName = \"ComposableContext\";\n\nfunction useComposableParents() {\n const context = useContext(ComposableContext);\n if (!context) {\n return [];\n }\n\n return context;\n}\n\nconst nullRenderer = () => null;\n\nfunction makeDecoratableComponent<T extends GenericComponent>(\n name: string,\n Component: T = nullRenderer as unknown as T\n) {\n const Decoratable = (props: React.ComponentProps<T>): JSX.Element | null => {\n const parents = useComposableParents();\n const ComposedComponent = useComponent(Component) as GenericComponent<\n React.ComponentProps<T>\n >;\n\n const context = useMemo(() => [...parents, name], [parents, name]);\n\n return (\n <ComposableContext.Provider value={context}>\n <ComposedComponent {...props}>{props.children}</ComposedComponent>\n </ComposableContext.Provider>\n );\n };\n\n const staticProps = {\n original: Component,\n originalName: name,\n displayName: `Decoratable<${name}>`\n };\n\n return withDecoratorFactory()(\n Object.assign(Decoratable, staticProps) as DecoratableComponent<\n typeof Component & typeof staticProps\n >\n );\n}\n\nexport function makeDecoratableHook<T extends GenericHook>(hook: T) {\n const decoratableHook = (params: Parameters<T>) => {\n const composedHook = useComponent(hook);\n\n return composedHook(params) as DecoratableHook<T>;\n };\n\n decoratableHook.original = hook;\n\n return withHookDecoratorFactory()(decoratableHook as DecoratableHook<T>);\n}\n\nexport function createVoidComponent<T>() {\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\n return (props: T): JSX.Element | null => {\n return null;\n };\n}\n\nexport function makeDecoratable<T extends GenericHook>(\n hook: T\n): ReturnType<typeof makeDecoratableHook<T>>;\nexport function makeDecoratable<T extends GenericComponent>(\n name: string,\n Component: T\n): ReturnType<typeof makeDecoratableComponent<T>>;\nexport function makeDecoratable(hookOrName: any, Component?: any) {\n if (Component) {\n const component = makeDecoratableComponent(hookOrName, React.memo(Component));\n component.original.displayName = hookOrName;\n return component;\n }\n\n return makeDecoratableHook(hookOrName);\n}\n"],"mappings":"AAAA,OAAOA,KAAK,IAAIC,aAAa,EAAEC,UAAU,EAAEC,OAAO,QAAQ,OAAO;AACjE,SAASC,YAAY;AAOrB,SAASC,oBAAoB,EAAEC,wBAAwB;AAEvD,MAAMC,iBAAiB,gBAAGN,aAAa,CAAW,EAAE,CAAC;AACrDM,iBAAiB,CAACC,WAAW,GAAG,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,iBAAiB,GAAGd,YAAY,CAACU,SAAS,CAE/C;IAED,MAAMJ,OAAO,GAAGP,OAAO,CAAC,MAAM,CAAC,GAAGc,OAAO,EAAEJ,IAAI,CAAC,EAAE,CAACI,OAAO,EAAEJ,IAAI,CAAC,CAAC;IAElE,oBACIb,KAAA,CAAAmB,aAAA,CAACZ,iBAAiB,CAACa,QAAQ;MAACC,KAAK,EAAEX;IAAQ,gBACvCV,KAAA,CAAAmB,aAAA,CAACD,iBAAiB,EAAKF,KAAK,EAAGA,KAAK,CAACM,QAA4B,CACzC,CAAC;EAErC,CAAC;EAED,MAAMC,WAAW,GAAG;IAChBC,QAAQ,EAAEV,SAAS;IACnBW,YAAY,EAAEZ,IAAI;IAClBL,WAAW,EAAE,eAAeK,IAAI;EACpC,CAAC;EAED,OAAOR,oBAAoB,CAAC,CAAC,CACzBqB,MAAM,CAACC,MAAM,CAACZ,WAAW,EAAEQ,WAAW,CAG1C,CAAC;AACL;AAEA,OAAO,SAASK,mBAAmBA,CAAwBC,IAAO,EAAE;EAChE,MAAMC,eAAe,GAAIC,MAAqB,IAAK;IAC/C,MAAMC,YAAY,GAAG5B,YAAY,CAACyB,IAAI,CAAC;IAEvC,OAAOG,YAAY,CAACD,MAAM,CAAC;EAC/B,CAAC;EAEDD,eAAe,CAACN,QAAQ,GAAGK,IAAI;EAE/B,OAAOvB,wBAAwB,CAAC,CAAC,CAACwB,eAAqC,CAAC;AAC5E;AAEA,OAAO,SAASG,mBAAmBA,CAAA,EAAM;EACrC;EACA,OAAQjB,KAAQ,IAAyB;IACrC,OAAO,IAAI;EACf,CAAC;AACL;AASA,OAAO,SAASkB,eAAeA,CAACC,UAAe,EAAErB,SAAe,EAAE;EAC9D,IAAIA,SAAS,EAAE;IACX,MAAMsB,SAAS,GAAGxB,wBAAwB,CAACuB,UAAU,eAAEnC,KAAK,CAACqC,IAAI,CAACvB,SAAS,CAAC,CAAC;IAC7EsB,SAAS,CAACZ,QAAQ,CAAChB,WAAW,GAAG2B,UAAU;IAC3C,OAAOC,SAAS;EACpB;EAEA,OAAOR,mBAAmB,CAACO,UAAU,CAAC;AAC1C","ignoreList":[]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@webiny/react-composition",
|
|
3
|
-
"version": "6.0.0-
|
|
3
|
+
"version": "6.0.0-rc.1",
|
|
4
|
+
"type": "module",
|
|
4
5
|
"main": "index.js",
|
|
5
6
|
"repository": {
|
|
6
7
|
"type": "git",
|
|
@@ -14,29 +15,19 @@
|
|
|
14
15
|
],
|
|
15
16
|
"license": "MIT",
|
|
16
17
|
"dependencies": {
|
|
17
|
-
"@babel/runtime": "7.24.1",
|
|
18
18
|
"@types/react": "18.2.79",
|
|
19
19
|
"react": "18.2.0",
|
|
20
20
|
"react-dom": "18.2.0"
|
|
21
21
|
},
|
|
22
22
|
"devDependencies": {
|
|
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
23
|
"@testing-library/react": "15.0.7",
|
|
28
|
-
"@webiny/
|
|
29
|
-
"
|
|
30
|
-
"
|
|
31
|
-
"typescript": "4.7.4"
|
|
24
|
+
"@webiny/build-tools": "6.0.0-rc.1",
|
|
25
|
+
"typescript": "5.9.3",
|
|
26
|
+
"vitest": "4.0.18"
|
|
32
27
|
},
|
|
33
28
|
"publishConfig": {
|
|
34
29
|
"access": "public",
|
|
35
30
|
"directory": "dist"
|
|
36
31
|
},
|
|
37
|
-
"
|
|
38
|
-
"build": "yarn webiny run build",
|
|
39
|
-
"watch": "yarn webiny run watch"
|
|
40
|
-
},
|
|
41
|
-
"gitHead": "aa8dbfbbd5ad13ec271adba6f2431e02991a300f"
|
|
32
|
+
"gitHead": "36d702721ff9ed39fb21d6f5fe7922a2a8716e63"
|
|
42
33
|
}
|
package/types.d.ts
CHANGED
|
@@ -1,29 +1,35 @@
|
|
|
1
|
-
import React from "react";
|
|
2
|
-
export
|
|
3
|
-
export
|
|
4
|
-
export
|
|
5
|
-
export
|
|
1
|
+
import type React from "react";
|
|
2
|
+
export type GenericHook<TParams = any, TReturn = any> = (...args: TParams[]) => TReturn;
|
|
3
|
+
export type GenericComponent<T = any> = React.FunctionComponent<T>;
|
|
4
|
+
export type ComposedFunction = GenericHook;
|
|
5
|
+
export type Decorator<T> = (decoratee: T) => T;
|
|
6
|
+
/**
|
|
7
|
+
* Some decoratable components will always return `null`, by design.
|
|
8
|
+
* To allow you to decorate these components, we must tell TS that the decorator is allowed to return not just `null`
|
|
9
|
+
* (which is inferred from the component type), but also a JSX.Element.
|
|
10
|
+
*/
|
|
11
|
+
export type ComponentDecorator<T> = (decoratee: T) => CanReturnNullOrElement<T>;
|
|
6
12
|
/**
|
|
7
13
|
* @deprecated
|
|
8
14
|
*/
|
|
9
|
-
export
|
|
15
|
+
export type ComposableFC<T> = T & {
|
|
10
16
|
displayName?: string;
|
|
11
17
|
original: T;
|
|
12
18
|
originalName: string;
|
|
13
19
|
};
|
|
14
|
-
export
|
|
15
|
-
export
|
|
16
|
-
export
|
|
20
|
+
export type Enumerable<T> = T extends Array<infer D> ? Array<D> : never;
|
|
21
|
+
export type ComposeWith = Decorator<GenericComponent> | Decorator<GenericComponent>[] | Decorator<GenericHook> | Decorator<GenericHook>[];
|
|
22
|
+
export type DecoratableHook<T extends GenericHook = GenericHook> = T & {
|
|
17
23
|
original: T;
|
|
18
24
|
originalName: string;
|
|
19
25
|
};
|
|
20
|
-
export
|
|
26
|
+
export type DecoratableComponent<T = GenericComponent> = T & {
|
|
21
27
|
original: T;
|
|
22
28
|
originalName: string;
|
|
23
29
|
displayName: string;
|
|
24
30
|
};
|
|
25
|
-
export
|
|
31
|
+
export type Decoratable = DecoratableComponent | DecoratableHook;
|
|
26
32
|
/**
|
|
27
33
|
* @internal Add `null` to the ReturnType of the given function.
|
|
28
34
|
*/
|
|
29
|
-
export
|
|
35
|
+
export type CanReturnNullOrElement<T> = T extends (...args: any) => any ? (...args: Parameters<T>) => JSX.Element | null : never;
|
package/types.js
CHANGED
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 * @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
|
|
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":[]}
|