ant-design/components/_util/reactNode.ts

30 lines
816 B
TypeScript
Raw Normal View History

import * as React from 'react';
export const { isValidElement } = React;
export function isFragment(child: any): boolean {
return child && isValidElement(child) && child.type === React.Fragment;
}
2022-09-09 10:03:49 +08:00
type AnyObject = Record<PropertyKey, any>;
2022-09-09 10:03:49 +08:00
type RenderProps = AnyObject | ((originProps: AnyObject) => AnyObject | void);
export function replaceElement(
element: React.ReactNode,
replacement: React.ReactNode,
2022-09-09 10:03:49 +08:00
props?: RenderProps,
): React.ReactNode {
2022-09-09 10:03:49 +08:00
if (!isValidElement(element)) {
return replacement;
}
return React.cloneElement(
element,
typeof props === 'function' ? props(element.props || {}) : props,
);
}
export function cloneElement(element: React.ReactNode, props?: RenderProps): React.ReactElement {
return replaceElement(element, element, props) as React.ReactElement;
2019-05-07 14:57:32 +08:00
}