How to use the react-cosmos-shared2/react.isReactElement function in react-cosmos-shared2

To help you get started, we’ve selected a few react-cosmos-shared2 examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github react-cosmos / react-cosmos / packages / react-cosmos-fixture / src / FixtureCapture / shared / nodeTree / findElementPaths.ts View on Github external
export function findElementPaths(
  node: React.ReactNode,
  curPath: string = ''
): string[] {
  if (Array.isArray(node)) {
    return flatten(
      node.map((child, idx) => findElementPaths(child, `${curPath}[${idx}]`))
    );
  }

  if (!isReactElement(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 [];
  }

  const { children } = node.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 node.type === Fragment ? childElPaths : [curPath, ...childElPaths];
}
github react-cosmos / react-cosmos / packages / react-cosmos-fixture / src / FixtureCapture / shared / nodeTree / getElementAtPath.ts View on Github external
export function getElementAtPath(
  node: React.ReactNode,
  elPath: string
): null | React.ReactElement {
  if (!isReactElement(node) && !Array.isArray(node)) {
    return null;
  }

  const rootNode = node as React.ReactElement | React.ReactNode[];
  const childNode = isRootPath(elPath) ? rootNode : get(rootNode, elPath);

  if (!isReactElement(childNode)) {
    return null;
  }

  return childNode;
}
github react-cosmos / react-cosmos / packages / react-cosmos-fixture / src / FixtureCapture / shared / nodeTree / getElementAtPath.ts View on Github external
export function getElementAtPath(
  node: React.ReactNode,
  elPath: string
): null | React.ReactElement {
  if (!isReactElement(node) && !Array.isArray(node)) {
    return null;
  }

  const rootNode = node as React.ReactElement | React.ReactNode[];
  const childNode = isRootPath(elPath) ? rootNode : get(rootNode, elPath);

  if (!isReactElement(childNode)) {
    return null;
  }

  return childNode;
}