Skip to content

Commit

Permalink
[React.cloneElement] Raise in any other case than lowercase element (
Browse files Browse the repository at this point in the history
  • Loading branch information
davesnx committed Aug 28, 2024
1 parent f138d12 commit fb68360
Showing 1 changed file with 17 additions and 13 deletions.
30 changes: 17 additions & 13 deletions packages/react/src/React.ml
Original file line number Diff line number Diff line change
Expand Up @@ -471,9 +471,9 @@ let createElement tag attributes children =
| true -> Lower_case_element { tag; attributes; children = [] }
| false -> create_element_inner tag attributes children

(* cloneElements overrides childrens and props but is not clear
what to do with other components that are not lower_case_elements
Provider, Consumer or Suspense. TODO: Check original (JS) implementation *)
(* `cloneElement` overrides childrens and props on lower case components, It raises Invalid_argument for the rest.
React.js can clone uppercase components, since it stores their props on each element's object but since we just store the fn and don't have the props, we can't clone them).
TODO: Check original implementation for exact error message/exception type *)
let cloneElement element new_attributes =
match element with
| Lower_case_element { tag; attributes; children } ->
Expand All @@ -483,16 +483,20 @@ let cloneElement element new_attributes =
attributes = clone_attributes attributes new_attributes;
children;
}
| Fragment _childrens -> Fragment _childrens
| Text t -> Text t
| InnerHtml t -> InnerHtml t
| Empty -> Empty
| List l -> List l
| Provider child -> Provider child
| Consumer child -> Consumer child
| Upper_case_component f -> Upper_case_component f
| Async_component f -> Async_component f
| Suspense { fallback; children } -> Suspense { fallback; children }
| Upper_case_component _ ->
raise
(Invalid_argument "In server-reason-react, a component can't be cloned")
| Fragment _ -> raise (Invalid_argument "can't clone a fragment")
| Text _ -> raise (Invalid_argument "can't clone a text element")
| InnerHtml _ ->
raise (Invalid_argument "can't clone a dangerouslySetInnerHTML element")
| Empty -> raise (Invalid_argument "can't clone a null element")
| List _ -> raise (Invalid_argument "can't clone an array element")
| Provider _ -> raise (Invalid_argument "can't clone a Provider")
| Consumer _ -> raise (Invalid_argument "can't clone a Consumer")
| Async_component _ ->
raise (Invalid_argument "can't clone an async component")
| Suspense _ -> raise (Invalid_argument "can't clone a Supsense component")

module Fragment = struct
let make ~children ?key:_ () = Fragment children
Expand Down

0 comments on commit fb68360

Please sign in to comment.