react-cosmos-renderer 6.1.2-canary.db623aa.0 → 6.2.0
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/dist/client.d.ts +2 -1
- package/dist/client.js +2 -1
- package/dist/fixture/ClassStateMock.d.ts +13 -0
- package/dist/fixture/ClassStateMock.js +72 -0
- package/dist/fixture/FixtureCapture/shared/nodeTree/shared.d.ts +1 -1
- package/dist/fixtureModule/DecoratedFixture.d.ts +2 -1
- package/dist/fixtureModule/DecoratedFixture.js +4 -4
- package/dist/fixtureModule/FixtureModule.d.ts +1 -2
- package/dist/fixtureModule/FixtureModule.js +5 -3
- package/dist/fixtureModule/FixtureProvider.d.ts +1 -0
- package/dist/fixtureModule/FixtureProvider.js +2 -1
- package/dist/fixtureModule/createFixtureNode.js +15 -6
- package/dist/fixtureModule/decorateFixture.d.ts +1 -1
- package/dist/fixtureModule/decorateFixture.js +2 -2
- package/package.json +4 -4
package/dist/client.d.ts
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
|
+
export * from './fixture/ClassStateMock.js';
|
|
1
2
|
export * from './fixture/FixtureCapture/FixtureCapture.js';
|
|
2
3
|
export * from './fixture/FixtureContext.js';
|
|
3
|
-
export * from './fixture/Viewport.js';
|
|
4
4
|
export * from './fixture/useFixtureInput/useFixtureInput.js';
|
|
5
5
|
export * from './fixture/useFixtureInput/useValue.js';
|
|
6
6
|
export * from './fixture/useFixtureSelect/useFixtureSelect.js';
|
|
7
7
|
export * from './fixture/useFixtureSelect/useSelect.js';
|
|
8
8
|
export * from './fixture/useFixtureState.js';
|
|
9
|
+
export * from './fixture/Viewport.js';
|
|
9
10
|
export * from './fixtureLoaders/ClientFixtureLoader.js';
|
|
10
11
|
export * from './rendererConnect/RendererContext.js';
|
|
11
12
|
export * from './rendererConnect/RendererProvider.js';
|
package/dist/client.js
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
|
+
export * from './fixture/ClassStateMock.js';
|
|
1
2
|
export * from './fixture/FixtureCapture/FixtureCapture.js';
|
|
2
3
|
export * from './fixture/FixtureContext.js';
|
|
3
|
-
export * from './fixture/Viewport.js';
|
|
4
4
|
export * from './fixture/useFixtureInput/useFixtureInput.js';
|
|
5
5
|
export * from './fixture/useFixtureInput/useValue.js';
|
|
6
6
|
export * from './fixture/useFixtureSelect/useFixtureSelect.js';
|
|
7
7
|
export * from './fixture/useFixtureSelect/useSelect.js';
|
|
8
8
|
export * from './fixture/useFixtureState.js';
|
|
9
|
+
export * from './fixture/Viewport.js';
|
|
9
10
|
export * from './fixtureLoaders/ClientFixtureLoader.js';
|
|
10
11
|
export * from './rendererConnect/RendererContext.js';
|
|
11
12
|
export * from './rendererConnect/RendererProvider.js';
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
type Props = {
|
|
3
|
+
children: React.ClassicElement<unknown>;
|
|
4
|
+
state?: {};
|
|
5
|
+
};
|
|
6
|
+
export declare class ClassStateMock extends React.Component<Props> {
|
|
7
|
+
static cosmosCapture: boolean;
|
|
8
|
+
childRef: React.Component | null;
|
|
9
|
+
render(): React.CElement<unknown, React.Component<{}, {}, any>>;
|
|
10
|
+
componentDidUpdate(prevProps: Props): void;
|
|
11
|
+
handleRef: (childRef: React.Component | null) => void;
|
|
12
|
+
}
|
|
13
|
+
export {};
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { isEqual } from 'lodash-es';
|
|
2
|
+
import React from 'react';
|
|
3
|
+
// Compied from https://github.com/skidding/react-mock/blob/c33dfa1d6f0c9ce7b3eaba073618d61731a0e82e/packages/state/src/index.js
|
|
4
|
+
export class ClassStateMock extends React.Component {
|
|
5
|
+
static cosmosCapture = false;
|
|
6
|
+
childRef = null;
|
|
7
|
+
render() {
|
|
8
|
+
const { children } = this.props;
|
|
9
|
+
if (Array.isArray(children)) {
|
|
10
|
+
throw new Error('ClassStateMock only accepts a single child element');
|
|
11
|
+
}
|
|
12
|
+
return React.cloneElement(children, { ref: this.handleRef });
|
|
13
|
+
}
|
|
14
|
+
componentDidUpdate(prevProps) {
|
|
15
|
+
const { childRef } = this;
|
|
16
|
+
const { state } = this.props;
|
|
17
|
+
if (!childRef) {
|
|
18
|
+
throw new Error('childRef missing in ClassStateMock.componentDidUpdate');
|
|
19
|
+
}
|
|
20
|
+
if (state && !isEqual(state, prevProps.state)) {
|
|
21
|
+
replaceState(childRef, state);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
handleRef = (childRef) => {
|
|
25
|
+
const prevRef = this.props.children.ref;
|
|
26
|
+
this.childRef = childRef;
|
|
27
|
+
if (!childRef) {
|
|
28
|
+
if (prevRef)
|
|
29
|
+
manuallyCallRef(prevRef, childRef);
|
|
30
|
+
// Nothing else to do on the unmount branch (when refs are set to NULL)
|
|
31
|
+
return;
|
|
32
|
+
}
|
|
33
|
+
if (this.props.state) {
|
|
34
|
+
// Wait until state has been set to call prev ref. This will give the
|
|
35
|
+
// impression that the mocked state is the initial state.
|
|
36
|
+
replaceState(childRef, this.props.state, () => {
|
|
37
|
+
manuallyCallRef(prevRef, childRef);
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
else {
|
|
41
|
+
manuallyCallRef(prevRef, childRef);
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
function replaceState(childRef, state, cb) {
|
|
46
|
+
// We need to unset existing state keys because React doesn't provide a
|
|
47
|
+
// replaceState method (anymore)
|
|
48
|
+
// https://reactjs.org/docs/react-component.html#setstate
|
|
49
|
+
const nextState = resetOriginalKeys(childRef.state, state);
|
|
50
|
+
if (!isEqual(nextState, childRef.state)) {
|
|
51
|
+
childRef.setState(nextState, cb);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
function resetOriginalKeys(original, current) {
|
|
55
|
+
const { keys } = Object;
|
|
56
|
+
return keys(original).reduce((result, key) => keys(result).indexOf(key) === -1
|
|
57
|
+
? { ...result, [key]: undefined }
|
|
58
|
+
: result, current);
|
|
59
|
+
}
|
|
60
|
+
function manuallyCallRef(ref, elRef) {
|
|
61
|
+
if (typeof ref === 'string') {
|
|
62
|
+
throw new Error('StateMock does not support string refs');
|
|
63
|
+
}
|
|
64
|
+
// https://reactjs.org/docs/refs-and-the-dom.html#creating-refs
|
|
65
|
+
if (typeof ref === 'function') {
|
|
66
|
+
ref(elRef);
|
|
67
|
+
}
|
|
68
|
+
else if (ref && typeof ref === 'object') {
|
|
69
|
+
// @ts-ignore
|
|
70
|
+
ref.current = elRef;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
export declare function getChildrenPath(elPath: string): string;
|
|
2
|
-
export declare function isRootPath(elPath: string):
|
|
2
|
+
export declare function isRootPath(elPath: string): elPath is "";
|
|
@@ -2,8 +2,9 @@ import React from 'react';
|
|
|
2
2
|
import { ReactDecorator, ReactDecoratorModule, ReactFixture } from 'react-cosmos-core';
|
|
3
3
|
type Props = {
|
|
4
4
|
fixture: ReactFixture;
|
|
5
|
+
fixtureOptions: {};
|
|
5
6
|
userDecoratorModules: ReactDecoratorModule[];
|
|
6
7
|
globalDecorators?: ReactDecorator[];
|
|
7
8
|
};
|
|
8
|
-
export declare function DecoratedFixture({ fixture, userDecoratorModules, globalDecorators, }: Props): React.ReactElement<any, string | React.JSXElementConstructor<any>>;
|
|
9
|
+
export declare function DecoratedFixture({ fixture, fixtureOptions, userDecoratorModules, globalDecorators, }: Props): React.ReactElement<any, string | React.JSXElementConstructor<any>>;
|
|
9
10
|
export {};
|
|
@@ -2,12 +2,12 @@ import React, { useMemo } from 'react';
|
|
|
2
2
|
import { FixtureCapture } from '../fixture/FixtureCapture/FixtureCapture.js';
|
|
3
3
|
import { createFixtureNode } from './createFixtureNode.js';
|
|
4
4
|
import { decorateFixture } from './decorateFixture.js';
|
|
5
|
-
export function DecoratedFixture({ fixture, userDecoratorModules, globalDecorators = [], }) {
|
|
5
|
+
export function DecoratedFixture({ fixture, fixtureOptions, userDecoratorModules, globalDecorators = [], }) {
|
|
6
6
|
return useMemo(() => {
|
|
7
7
|
const decorators = [
|
|
8
8
|
...globalDecorators,
|
|
9
|
-
...userDecoratorModules.map(m => m.default),
|
|
9
|
+
...userDecoratorModules.map(m => m.default).flat(),
|
|
10
10
|
];
|
|
11
|
-
return decorateFixture(React.createElement(FixtureCapture, { decoratorId: "root" }, createFixtureNode(fixture)), decorators);
|
|
12
|
-
}, [fixture, globalDecorators, userDecoratorModules]);
|
|
11
|
+
return decorateFixture(React.createElement(FixtureCapture, { decoratorId: "root" }, createFixtureNode(fixture)), fixtureOptions, decorators);
|
|
12
|
+
}, [fixture, fixtureOptions, globalDecorators, userDecoratorModules]);
|
|
13
13
|
}
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
/// <reference types="react/experimental" />
|
|
2
1
|
import React from 'react';
|
|
3
2
|
import { FixtureId, FixtureState, ReactDecorator, ReactDecoratorModule, ReactFixtureModule } from 'react-cosmos-core';
|
|
4
3
|
type Props = {
|
|
@@ -11,5 +10,5 @@ type Props = {
|
|
|
11
10
|
lazy: boolean;
|
|
12
11
|
renderMessage: (msg: string) => React.ReactNode;
|
|
13
12
|
};
|
|
14
|
-
export declare function FixtureModule({ fixtureModule, decoratorModules, globalDecorators, fixtureId, initialFixtureState, renderKey, lazy, renderMessage, }: Props): string | number | boolean | Iterable<React.ReactNode> | React.
|
|
13
|
+
export declare function FixtureModule({ fixtureModule, decoratorModules, globalDecorators, fixtureId, initialFixtureState, renderKey, lazy, renderMessage, }: Props): string | number | bigint | boolean | Iterable<React.ReactNode> | Promise<React.AwaitedReactNode> | React.JSX.Element | null | undefined;
|
|
15
14
|
export {};
|
|
@@ -1,14 +1,16 @@
|
|
|
1
1
|
import React from 'react';
|
|
2
|
-
import { getFixtureFromExport, getFixtureItemFromExport, stringifyFixtureId, } from 'react-cosmos-core';
|
|
2
|
+
import { getFixtureFromExport, getFixtureItemFromExport, pickSerializableValues, stringifyFixtureId, } from 'react-cosmos-core';
|
|
3
3
|
import { DecoratedFixture } from './DecoratedFixture.js';
|
|
4
4
|
import { FixtureProvider } from './FixtureProvider.js';
|
|
5
5
|
export function FixtureModule({ fixtureModule, decoratorModules, globalDecorators, fixtureId, initialFixtureState, renderKey, lazy, renderMessage, }) {
|
|
6
6
|
const fixtureItem = React.useMemo(() => getFixtureItemFromExport(fixtureModule.default), [fixtureModule.default]);
|
|
7
7
|
const fixtureKey = React.useMemo(() => `${stringifyFixtureId(fixtureId)}-${renderKey}`, [fixtureId, renderKey]);
|
|
8
8
|
const fixture = getFixtureFromExport(fixtureModule.default, fixtureId.name);
|
|
9
|
+
const { options = {} } = fixtureModule;
|
|
10
|
+
const serializableOptions = React.useMemo(() => pickSerializableValues(options), [options]);
|
|
9
11
|
if (typeof fixture === 'undefined') {
|
|
10
12
|
return renderMessage(`Invalid fixture name: ${fixtureId.name}`);
|
|
11
13
|
}
|
|
12
|
-
return (React.createElement(FixtureProvider, { key: fixtureKey, fixtureId: fixtureId, initialFixtureState: initialFixtureState, fixtureItem: fixtureItem, lazy: lazy },
|
|
13
|
-
React.createElement(DecoratedFixture, { fixture: fixture, userDecoratorModules: decoratorModules, globalDecorators: globalDecorators })));
|
|
14
|
+
return (React.createElement(FixtureProvider, { key: fixtureKey, fixtureId: fixtureId, initialFixtureState: initialFixtureState, fixtureItem: fixtureItem, fixtureOptions: serializableOptions, lazy: lazy },
|
|
15
|
+
React.createElement(DecoratedFixture, { fixture: fixture, fixtureOptions: options, userDecoratorModules: decoratorModules, globalDecorators: globalDecorators })));
|
|
14
16
|
}
|
|
@@ -25,9 +25,10 @@ export function FixtureProvider(props) {
|
|
|
25
25
|
payload: {
|
|
26
26
|
rendererId,
|
|
27
27
|
fixture: props.fixtureItem,
|
|
28
|
+
fixtureOptions: props.fixtureOptions,
|
|
28
29
|
},
|
|
29
30
|
});
|
|
30
|
-
}, [props.fixtureItem, rendererConnect, rendererId]);
|
|
31
|
+
}, [props.fixtureItem, props.fixtureOptions, rendererConnect, rendererId]);
|
|
31
32
|
React.useEffect(() => {
|
|
32
33
|
if (!isEqual(state.fixtureState, state.syncedFixtureState)) {
|
|
33
34
|
rendererConnect.postMessage({
|
|
@@ -1,17 +1,26 @@
|
|
|
1
1
|
import React from 'react';
|
|
2
|
+
import { isElement } from 'react-is';
|
|
2
3
|
export function createFixtureNode(fixture) {
|
|
3
4
|
// Warning: In a React Server Components setup this function is called on the
|
|
4
5
|
// server. When a fixture module uses the 'use client' directive, the fixture
|
|
5
|
-
//
|
|
6
|
-
// module contents. In this scenario, the fixture module will be automatically
|
|
7
|
-
// rendered on the client side, where React expects a component default export.
|
|
6
|
+
// export will be a Promise wrapper (imbued with magical properties methinks).
|
|
8
7
|
// This results in the following limitation: In a React Server Components
|
|
9
8
|
// setup, Client fixtures have to export a single function component. They
|
|
10
9
|
// can't be multi fixtures and they can't export React elements directly.
|
|
11
|
-
return
|
|
10
|
+
return isNodeFixture(fixture) ? (fixture) : (React.createElement(FixtureElement, { Component: fixture }));
|
|
12
11
|
}
|
|
13
|
-
function
|
|
14
|
-
|
|
12
|
+
function isNodeFixture(fixture) {
|
|
13
|
+
// If you're curious what the exact type of ReactNode is:
|
|
14
|
+
// https://github.com/DefinitelyTyped/DefinitelyTyped/blob/6fc4839a810335dee15374e6bc82dbbc2bbdff58/types/react/index.d.ts#L478-L489
|
|
15
|
+
return (fixture === undefined ||
|
|
16
|
+
fixture === null ||
|
|
17
|
+
typeof fixture === 'string' ||
|
|
18
|
+
typeof fixture === 'number' ||
|
|
19
|
+
typeof fixture === 'boolean' ||
|
|
20
|
+
Array.isArray(fixture) ||
|
|
21
|
+
// If you're curious what isElement checks:
|
|
22
|
+
// https://github.com/facebook/react/blob/1b0132c05acabae5aebd32c2cadddfb16bda70bc/packages/react-is/src/ReactIs.js#L108-L114
|
|
23
|
+
isElement(fixture));
|
|
15
24
|
}
|
|
16
25
|
function FixtureElement({ Component }) {
|
|
17
26
|
return React.createElement(Component, null);
|
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
import React from 'react';
|
|
2
2
|
import { ReactDecorator } from 'react-cosmos-core';
|
|
3
|
-
export declare function decorateFixture(fixtureNode: React.ReactNode, decorators: ReactDecorator[]): React.ReactElement;
|
|
3
|
+
export declare function decorateFixture(fixtureNode: React.ReactNode, fixtureOptions: {}, decorators: ReactDecorator[]): React.ReactElement;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
import React from 'react';
|
|
2
|
-
export function decorateFixture(fixtureNode, decorators) {
|
|
3
|
-
return (React.createElement(React.Fragment, null, [...decorators].reverse().reduce((prevElement, Decorator) => (React.createElement(Decorator,
|
|
2
|
+
export function decorateFixture(fixtureNode, fixtureOptions, decorators) {
|
|
3
|
+
return (React.createElement(React.Fragment, null, [...decorators].reverse().reduce((prevElement, Decorator) => (React.createElement(Decorator, { options: fixtureOptions }, prevElement)), fixtureNode)));
|
|
4
4
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "react-cosmos-renderer",
|
|
3
|
-
"version": "6.
|
|
3
|
+
"version": "6.2.0",
|
|
4
4
|
"description": "React Cosmos Renderer",
|
|
5
5
|
"repository": "https://github.com/react-cosmos/react-cosmos/tree/main/packages/react-cosmos-renderer",
|
|
6
6
|
"license": "MIT",
|
|
@@ -12,8 +12,8 @@
|
|
|
12
12
|
},
|
|
13
13
|
"dependencies": {
|
|
14
14
|
"lodash-es": "4.17.21",
|
|
15
|
-
"react-cosmos-core": "6.
|
|
16
|
-
"react-is": "18.
|
|
15
|
+
"react-cosmos-core": "^6.2.0",
|
|
16
|
+
"react-is": "18.3.1"
|
|
17
17
|
},
|
|
18
|
-
"gitHead": "
|
|
18
|
+
"gitHead": "965bf0e13230324699560be6eabb642c51c6d945"
|
|
19
19
|
}
|