mirror of
https://gitee.com/ant-design/ant-design.git
synced 2024-12-15 09:21:22 +08:00
ac5a06027e
This reverts commit ea8ed28209
.
30 lines
816 B
TypeScript
30 lines
816 B
TypeScript
import * as React from 'react';
|
|
|
|
export const { isValidElement } = React;
|
|
|
|
export function isFragment(child: any): boolean {
|
|
return child && isValidElement(child) && child.type === React.Fragment;
|
|
}
|
|
|
|
type AnyObject = Record<PropertyKey, any>;
|
|
|
|
type RenderProps = AnyObject | ((originProps: AnyObject) => AnyObject | void);
|
|
|
|
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;
|
|
}
|