Skip to content

Commit 69b750e

Browse files
committed
fix uncurried forwardRef component
1 parent 5626e30 commit 69b750e

File tree

3 files changed

+176
-4
lines changed

3 files changed

+176
-4
lines changed

res_syntax/cli/reactjs_jsx_v4.ml

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -817,12 +817,21 @@ let transformStructureItem ~config mapper item =
817817
React_jsx_common.raiseErrorMultipleReactComponent ~loc:pstr_loc
818818
else (
819819
config.hasReactComponent <- true;
820-
let binding =
821-
match binding.pvb_expr.pexp_desc with
820+
let rec removeArityRecord expr =
821+
match expr.pexp_desc with
822822
| Pexp_record
823823
([({txt = Ldot (Ldot (Lident "Js", "Fn"), _)}, e)], None) ->
824-
{binding with pvb_expr = e}
825-
| _ -> binding
824+
e
825+
| Pexp_apply (forwardRef, [(label, e)]) ->
826+
{
827+
expr with
828+
pexp_desc =
829+
Pexp_apply (forwardRef, [(label, removeArityRecord e)]);
830+
}
831+
| _ -> expr
832+
in
833+
let binding =
834+
{binding with pvb_expr = removeArityRecord binding.pvb_expr}
826835
in
827836
let coreTypeOfAttr =
828837
React_jsx_common.coreTypeOfAttrs binding.pvb_attributes

res_syntax/tests/ppx/react/expected/forwardRef.res.txt

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,63 @@ module V4C = {
124124
}
125125
}
126126

127+
module V4CUncurried = {
128+
module FancyInput = {
129+
type props<'className, 'children, 'ref> = {
130+
className?: 'className,
131+
children: 'children,
132+
ref?: 'ref,
133+
}
134+
135+
@react.component
136+
let make = (
137+
{?className, children, _}: props<'className, 'children, ReactRef.currentDomRef>,
138+
ref: Js.Nullable.t<ReactRef.currentDomRef>,
139+
) =>
140+
ReactDOM.createDOMElementVariadic(
141+
"div",
142+
[
143+
ReactDOM.createDOMElementVariadic(
144+
"input",
145+
~props={
146+
type_: "text",
147+
?className,
148+
ref: ?Js.Nullable.toOption(ref)->Belt.Option.map(React.Ref.domRef),
149+
},
150+
[],
151+
),
152+
children,
153+
],
154+
)
155+
let make = React.forwardRef({
156+
let \"ForwardRef$V4CUncurried$FancyInput" = (props: props<_>, ref) => make(props, ref)
157+
158+
\"ForwardRef$V4CUncurried$FancyInput"
159+
})
160+
}
161+
type props = {}
162+
163+
@react.component
164+
let make = (_: props) => {
165+
let input = React.useRef(Js.Nullable.null)
166+
167+
ReactDOM.createDOMElementVariadic(
168+
"div",
169+
[
170+
React.createElement(
171+
FancyInput.make,
172+
{ref: input, children: {React.string("Click to focus")}},
173+
),
174+
],
175+
)
176+
}
177+
let make = {
178+
let \"ForwardRef$V4CUncurried" = props => make(props)
179+
180+
\"ForwardRef$V4CUncurried"
181+
}
182+
}
183+
127184
@@jsxConfig({version: 4, mode: "automatic"})
128185

129186
module V4A = {
@@ -181,3 +238,59 @@ module V4A = {
181238
\"ForwardRef$V4A"
182239
}
183240
}
241+
242+
module V4AUncurried = {
243+
module FancyInput = {
244+
type props<'className, 'children, 'ref> = {
245+
className?: 'className,
246+
children: 'children,
247+
ref?: 'ref,
248+
}
249+
250+
@react.component
251+
let make = (
252+
{?className, children, _}: props<'className, 'children, ReactDOM.Ref.currentDomRef>,
253+
ref,
254+
) =>
255+
ReactDOM.jsxs(
256+
"div",
257+
{
258+
children: React.array([
259+
ReactDOM.jsx(
260+
"input",
261+
{
262+
type_: "text",
263+
?className,
264+
ref: ?Js.Nullable.toOption(ref)->Belt.Option.map(ReactDOM.Ref.domRef),
265+
},
266+
),
267+
children,
268+
]),
269+
},
270+
)
271+
let make = React.forwardRef({
272+
let \"ForwardRef$V4AUncurried$FancyInput" = (props: props<_>, ref) => make(props, ref)
273+
274+
\"ForwardRef$V4AUncurried$FancyInput"
275+
})
276+
}
277+
type props = {}
278+
279+
@react.component
280+
let make = (_: props) => {
281+
let input = React.useRef(Js.Nullable.null)
282+
ReactDOM.jsx(
283+
"div",
284+
{
285+
children: ?ReactDOM.someElement(
286+
React.jsx(FancyInput.make, {ref: input, children: {React.string("Click to focus")}}),
287+
),
288+
},
289+
)
290+
}
291+
let make = {
292+
let \"ForwardRef$V4AUncurried" = props => make(props)
293+
294+
\"ForwardRef$V4AUncurried"
295+
}
296+
}

res_syntax/tests/ppx/react/forwardRef.res

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,31 @@ module V4C = {
5252
}
5353
}
5454

55+
module V4CUncurried = {
56+
module FancyInput = {
57+
@react.component
58+
let make = React.forwardRef((. ~className=?, ~children, ref: Js.Nullable.t<ReactRef.currentDomRef>) =>
59+
<div>
60+
<input
61+
type_="text"
62+
?className
63+
ref=?{Js.Nullable.toOption(ref)->Belt.Option.map(React.Ref.domRef)}
64+
/>
65+
children
66+
</div>
67+
)
68+
}
69+
70+
@react.component
71+
let make = () => {
72+
let input = React.useRef(Js.Nullable.null)
73+
74+
<div>
75+
<FancyInput ref=input> {React.string("Click to focus")} </FancyInput>
76+
</div>
77+
}
78+
}
79+
5580
@@jsxConfig({version: 4, mode: "automatic"})
5681

5782
module V4A = {
@@ -78,3 +103,28 @@ module V4A = {
78103
</div>
79104
}
80105
}
106+
107+
module V4AUncurried = {
108+
module FancyInput = {
109+
@react.component
110+
let make = React.forwardRef((. ~className=?, ~children, ref) =>
111+
<div>
112+
<input
113+
type_="text"
114+
?className
115+
ref=?{Js.Nullable.toOption(ref)->Belt.Option.map(ReactDOM.Ref.domRef)}
116+
/>
117+
children
118+
</div>
119+
)
120+
}
121+
122+
@react.component
123+
let make = () => {
124+
let input = React.useRef(Js.Nullable.null)
125+
126+
<div>
127+
<FancyInput ref=input> {React.string("Click to focus")} </FancyInput>
128+
</div>
129+
}
130+
}

0 commit comments

Comments
 (0)