2019-04-24 12:21:02 +08:00
|
|
|
import * as React from 'react';
|
|
|
|
|
2020-06-08 18:01:50 +08:00
|
|
|
export const { isValidElement } = React;
|
2019-04-24 12:21:02 +08:00
|
|
|
|
2022-10-20 16:37:35 +08:00
|
|
|
export function isFragment(child: any): boolean {
|
2022-10-17 16:23:45 +08:00
|
|
|
return child && isValidElement(child) && child.type === React.Fragment;
|
2022-08-16 09:59:27 +08:00
|
|
|
}
|
|
|
|
|
2022-09-09 10:03:49 +08:00
|
|
|
type AnyObject = Record<PropertyKey, any>;
|
2020-11-17 23:03:07 +08:00
|
|
|
|
2022-09-09 10:03:49 +08:00
|
|
|
type RenderProps = AnyObject | ((originProps: AnyObject) => AnyObject | void);
|
2020-11-17 23:03:07 +08:00
|
|
|
|
2020-05-14 20:54:49 +08:00
|
|
|
export function replaceElement(
|
|
|
|
element: React.ReactNode,
|
|
|
|
replacement: React.ReactNode,
|
2022-09-09 10:03:49 +08:00
|
|
|
props?: RenderProps,
|
2020-05-14 20:54:49 +08:00
|
|
|
): React.ReactNode {
|
2022-09-09 10:03:49 +08:00
|
|
|
if (!isValidElement(element)) {
|
|
|
|
return replacement;
|
|
|
|
}
|
2020-11-21 19:00:11 +08:00
|
|
|
return React.cloneElement(
|
|
|
|
element,
|
|
|
|
typeof props === 'function' ? props(element.props || {}) : props,
|
|
|
|
);
|
2020-05-14 20:54:49 +08:00
|
|
|
}
|
|
|
|
|
2020-11-17 23:03:07 +08:00
|
|
|
export function cloneElement(element: React.ReactNode, props?: RenderProps): React.ReactElement {
|
2020-05-14 20:54:49 +08:00
|
|
|
return replaceElement(element, element, props) as React.ReactElement;
|
2019-05-07 14:57:32 +08:00
|
|
|
}
|