-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclone-valid-element.ts
48 lines (47 loc) · 1.69 KB
/
clone-valid-element.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/**
* Please refer to the terms of the license agreement in the root of the project
*
* (c) 2024 Feedzai
*/
import { cloneElement, isValidElement, ReactElement, Attributes, ReactNode } from "react";
/**
* A type-safe wrapper around React's cloneElement that handles both valid React elements
* and other ReactNode types. This helper safely clones React elements while preserving
* their props types, and returns non-element nodes unchanged.
*
* @template Props - The type of props the element accepts
* @param element - The React element to clone or any other React node
* @param props - New props to merge into the cloned element
* @param children - New children to add to the cloned element
* @returns A cloned React element with merged props if input was a valid element,
* or the original input otherwise
*
* @example
* // Cloning an element with new props
* const button = <button type="button">Click me</button>;
* const clonedButton = cloneValidElement(button, { disabled: true });
*
* @example
* // Safely handling non-element nodes
* const textNode = "Just text";
* const result = cloneValidElement(textNode); // Returns "Just text" unchanged
*
* @example
* // With typed props
* interface MyComponentProps {
* title: string;
* count?: number;
* }
* const element = <MyComponent title="Original" />;
* const cloned = cloneValidElement<MyComponentProps>(
* element,
* { count: 42 }
* );
*/
export function cloneValidElement<Props>(
element: ReactElement<Props> | ReactNode,
props?: Partial<Props> & Attributes,
...children: ReactNode[]
): ReactElement<Props> | ReactNode {
return isValidElement(element) ? cloneElement(element, props, ...children) : element;
}