-
-
Notifications
You must be signed in to change notification settings - Fork 346
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enable prop/state capture in JSX decorators (#917)
- Loading branch information
Showing
20 changed files
with
219 additions
and
177 deletions.
There are no files selected for viewing
20 changes: 20 additions & 0 deletions
20
examples/jsx-fixtures/components/WelcomeMessage/cosmos.decorator.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// @flow | ||
|
||
import React from 'react'; | ||
import { FixtureCapture } from 'react-cosmos-fixture'; | ||
|
||
export default ({ children }: { children: React$Node }) => ( | ||
<FixtureCapture decoratorId="bgDecorator"> | ||
<BgDecorator backgroundColor="lightgrey">{children}</BgDecorator> | ||
</FixtureCapture> | ||
); | ||
|
||
function BgDecorator({ | ||
children, | ||
backgroundColor | ||
}: { | ||
children: React$Node, | ||
backgroundColor: string | ||
}) { | ||
return <div style={{ backgroundColor }}>{children}</div>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 0 additions & 36 deletions
36
packages/react-cosmos-fixture/src/FixtureCapture/childrenTree/areChildrenEqual.js
This file was deleted.
Oops, something went wrong.
39 changes: 0 additions & 39 deletions
39
packages/react-cosmos-fixture/src/FixtureCapture/childrenTree/findElementPaths.js
This file was deleted.
Oops, something went wrong.
9 changes: 0 additions & 9 deletions
9
packages/react-cosmos-fixture/src/FixtureCapture/childrenTree/shared.js
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 5 additions & 5 deletions
10
packages/react-cosmos-fixture/src/FixtureCapture/findRelevantElementPaths.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
33 changes: 33 additions & 0 deletions
33
packages/react-cosmos-fixture/src/FixtureCapture/nodeTree/areNodesEqual.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// @flow | ||
|
||
import { isEqual, pick, mapValues } from 'lodash'; | ||
import { isElement } from 'react-is'; | ||
|
||
import type { Node, Element } from 'react'; | ||
|
||
export function areNodesEqual(a: Node, b: Node): boolean { | ||
return isEqual(stripInternalElementAttrs(a), stripInternalElementAttrs(b)); | ||
} | ||
|
||
// Don't compare private element attrs like _owner and _store, which hold | ||
// internal details and have auto increment-type attrs | ||
function stripInternalElementAttrs(node: mixed) { | ||
if (Array.isArray(node)) { | ||
return node.map(n => stripInternalElementAttrs(n)); | ||
} | ||
|
||
if (isElement(node)) { | ||
// $FlowFixMe Flow can't get cues from react-is package | ||
const el: Element<any> = node; | ||
|
||
return { | ||
...pick(el, 'type', 'key', 'ref'), | ||
// children and other props can contain Elements | ||
props: mapValues(el.props, propValue => | ||
stripInternalElementAttrs(propValue) | ||
) | ||
}; | ||
} | ||
|
||
return node; | ||
} |
39 changes: 39 additions & 0 deletions
39
packages/react-cosmos-fixture/src/FixtureCapture/nodeTree/findElementPaths.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// @flow | ||
|
||
import { flatten } from 'lodash'; | ||
import { isElement } from 'react-is'; | ||
import { Fragment } from 'react'; | ||
import { isRootPath } from './shared'; | ||
|
||
import type { Node, Element } from 'react'; | ||
|
||
export function findElementPaths(node: Node, curPath: string = ''): string[] { | ||
if (Array.isArray(node)) { | ||
return flatten( | ||
node.map((child, idx) => findElementPaths(child, `${curPath}[${idx}]`)) | ||
); | ||
} | ||
|
||
if (!isElement(node)) { | ||
// At this point the node can be null, boolean, string, number, Portal, etc. | ||
// https://github.com/facebook/flow/blob/172d28f542f49bbc1e765131c9dfb9e31780f3a2/lib/react.js#L13-L20 | ||
return []; | ||
} | ||
|
||
// $FlowFixMe Flow can't get cues from react-is package | ||
const element: Element<any> = node; | ||
const { children } = element.props; | ||
|
||
const childElPaths = | ||
// Props of elements returned by render functions can't be read here | ||
typeof children !== 'function' | ||
? findElementPaths(children, getChildrenPath(curPath)) | ||
: []; | ||
|
||
// Ignore Fragment elements, but include their children | ||
return element.type === Fragment ? childElPaths : [curPath, ...childElPaths]; | ||
} | ||
|
||
function getChildrenPath(curPath) { | ||
return isRootPath(curPath) ? 'props.children' : `${curPath}.props.children`; | ||
} |
Oops, something went wrong.