mirror of
https://github.com/ant-design/ant-design.git
synced 2024-11-29 21:59:41 +08:00
29 lines
805 B
TypeScript
29 lines
805 B
TypeScript
import * as React from 'react';
|
|
|
|
export const { isValidElement } = React;
|
|
|
|
export function isFragment(child: React.ReactElement): boolean {
|
|
return child && child.type === React.Fragment;
|
|
}
|
|
|
|
type AnyObject = Record<any, any>;
|
|
|
|
type RenderProps = undefined | AnyObject | ((originProps: AnyObject) => AnyObject | undefined);
|
|
|
|
export function replaceElement(
|
|
element: React.ReactNode,
|
|
replacement: React.ReactNode,
|
|
props: RenderProps,
|
|
): React.ReactNode {
|
|
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;
|
|
}
|