ant-design/components/_util/reactNode.ts
lijianan 67900a7aad
type: optimization AnyObject type (#43366)
* type: optimization AnyObject type

* fix lint

* rerun
2023-07-05 16:54:04 +08:00

29 lines
813 B
TypeScript

import * as React from 'react';
import type { AnyObject } from './type';
export const { isValidElement } = React;
export function isFragment(child: any): boolean {
return child && isValidElement(child) && child.type === React.Fragment;
}
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;
}