当前位置: 首页>>代码示例>>TypeScript>>正文


TypeScript react-is.isElement函数代码示例

本文整理汇总了TypeScript中react-is.isElement函数的典型用法代码示例。如果您正苦于以下问题:TypeScript isElement函数的具体用法?TypeScript isElement怎么用?TypeScript isElement使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了isElement函数的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。

示例1: findElementPaths

export function findElementPaths(
  node: React.ReactNode,
  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 [];
  }

  const element = node as React.ReactElement<any>;
  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];
}
开发者ID:skidding,项目名称:cosmos,代码行数:28,代码来源:findElementPaths.ts

示例2: getElementAtPath

export function getElementAtPath(
  node: React.ReactNode,
  elPath: string
): null | React.ReactElement<any> {
  if (!isElement(node) && !Array.isArray(node)) {
    return null;
  }

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

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

  return childNode as React.ReactElement<any>;
}
开发者ID:skidding,项目名称:cosmos,代码行数:17,代码来源:getElementAtPath.ts

示例3: isMultiFixture

export function isMultiFixture(
  fixtureExport: ReactFixtureExport
): fixtureExport is ReactFixtureMap {
  return (
    fixtureExport !== null &&
    typeof fixtureExport === 'object' &&
    !isElement(fixtureExport)
  );
}
开发者ID:skidding,项目名称:cosmos,代码行数:9,代码来源:isMultiFixture.ts

示例4: stripInternalElementAttrs

// Don't compare private element attrs like _owner and _store, which hold
// internal details and have auto increment-type attrs
function stripInternalElementAttrs(node: React.ReactNode): {} {
  if (Array.isArray(node)) {
    return node.map(n => stripInternalElementAttrs(n));
  }
  if (!isElement(node)) {
    return node;
  }

  const el = node as React.ReactElement<any>;
  return {
    ...pick(el, 'type', 'key', 'ref'),
    // children and other props can contain Elements
    props: mapValues(el.props, propValue =>
      stripInternalElementAttrs(propValue)
    )
  };
}
开发者ID:skidding,项目名称:cosmos,代码行数:19,代码来源:areNodesEqual.ts

示例5: cloneNode

function cloneNode(value: React.ReactNode): React.ReactNode {
  if (Array.isArray(value)) {
    return value.map(n => cloneNode(n));
  }

  if (isElement(value)) {
    const el = value as React.ReactElement<any>;
    const { children, ...otherProps } = el.props;

    return {
      ...el,
      props: {
        ...otherProps,
        children: cloneNode(children)
      }
    };
  }

  return value;
}
开发者ID:skidding,项目名称:cosmos,代码行数:20,代码来源:setElementAtPath.ts

示例6: stringifyValue

function stringifyValue(key: string, value: unknown): FixtureStateValue {
  try {
    // NOTE: Is this optimal?
    if (!isEqual(JSON.parse(JSON.stringify(value)), value)) {
      throw new Error('Unserializable value');
    }
  } catch (err) {
    return {
      serializable: false,
      key,
      // TODO: Enable custom stringifiers to plug in
      stringified: isElement(value)
        ? reactElementToJSXString(value)
        : String(value)
    };
  }

  return {
    serializable: true,
    key,
    stringified: JSON.stringify(value)
  };
}
开发者ID:skidding,项目名称:cosmos,代码行数:23,代码来源:shared.ts

示例7:

export const test = (val: any) => val && ReactIs.isElement(val);
开发者ID:Volune,项目名称:jest,代码行数:1,代码来源:ReactElement.ts


注:本文中的react-is.isElement函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。