Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ppx: support "custom children" in uppercase components without having to wrap in array literal #823

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
* Wrap the `React` library, exposing just a single top-level module
(@anmonteiro in [#783](https://github.com/reasonml/reason-react/pull/783))
* Re-organise toplevel modules (@davesnx in [#794](https://github.com/reasonml/reason-react/pull/794))
* BREAKING, ppx: Allow passing an array of custom children to a component
without having to wrap in array literal ([@jchavarri in #748](https://github.com/reasonml/reason-react/pull/823))

# 0.12.0

Expand Down
18 changes: 14 additions & 4 deletions ppx/reason_react_ppx.ml
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,14 @@ let makePropsType ~loc namedTypeList =
};
]

let jsxExprAndChildren ~ident ~loc ~ctxt mapper ~keyProps children =
type component_type = Uppercase | Lowercase

let jsxExprAndChildren ~component_type ~loc ~ctxt mapper ~keyProps children =
let ident =
match component_type with
| Uppercase -> Lident "React"
| Lowercase -> Lident "ReactDOM"
in
let childrenExpr =
Option.map (transformChildrenIfListUpper ~loc ~mapper ~ctxt) children
in
Expand Down Expand Up @@ -477,16 +484,19 @@ let jsxExprAndChildren ~ident ~loc ~ctxt mapper ~keyProps children =
children *)
( Builder.pexp_ident ~loc { loc; txt = Ldot (ident, "jsxs") },
None,
Some (Binding.React.array ~loc children) )
Some
(match component_type with
| Uppercase -> children
| Lowercase -> Binding.React.array ~loc children) )
| None, (label, key) :: _ ->
( Builder.pexp_ident ~loc { loc; txt = Ldot (ident, "jsxKeyed") },
Some (label, key),
None )
| None, [] ->
(Builder.pexp_ident ~loc { loc; txt = Ldot (ident, "jsx") }, None, None)

let reactJsxExprAndChildren = jsxExprAndChildren ~ident:(Lident "React")
let reactDomJsxExprAndChildren = jsxExprAndChildren ~ident:(Lident "ReactDOM")
let reactJsxExprAndChildren = jsxExprAndChildren ~component_type:Uppercase
let reactDomJsxExprAndChildren = jsxExprAndChildren ~component_type:Lowercase

(* Builds an AST node for the entire `external` definition of props *)
let makeExternalDecl fnName loc namedArgListWithKeyAndRef namedTypeList =
Expand Down
5 changes: 1 addition & 4 deletions ppx/test/upper.t/run.t
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@
let upper_children_single = foo =>
React.jsx(Upper.make, Upper.makeProps(~children=foo, ()));
let upper_children_multiple = (foo, bar) =>
React.jsxs(
Upper.make,
Upper.makeProps(~children=React.array([|foo, bar|]), ()),
);
React.jsxs(Upper.make, Upper.makeProps(~children=[|foo, bar|], ()));
let upper_children =
React.jsx(
Page.make,
Expand Down
37 changes: 31 additions & 6 deletions test/React__test.re
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@ module DummyComponentThatMapsChildren = {
[@react.component]
let make = (~children, ()) => {
<div>
{children->React.Children.mapWithIndex((element, index) => {
React.cloneElement(
element,
{"key": {j|$index|j}, "data-index": index},
)
})}
{children
->React.array
->React.Children.mapWithIndex((element, index) => {
React.cloneElement(
element,
{"key": {j|$index|j}, "data-index": index},
)
})}
</div>;
};
};
Expand Down Expand Up @@ -357,4 +359,27 @@ describe("React", () => {
/* We catch the exception here to not populate the error to the toplevel */
()
};

test("Can define components with custom children", () => {
let container = getContainer(container);
let root = ReactDOM.Client.createRoot(container);

module Test = {
type t = {name: string};
[@react.component]
let make = (~children) => {
Array.map(children, c => <div> {React.string(c.name)} </div>)
->React.array;
};
};

act(() => {
ReactDOM.Client.render(
root,
<Test> {Test.name: "foo"} {name: "bar"} </Test>,
)
});

expect(container->DOM.findBySelector("img")->Option.isSome)->toBe(true);
});
});
Loading