Skip to content

Commit 427fb8a

Browse files
cknittcristianoc
andauthored
Convert test sources from .re to .res (#5678)
* Remove re_first_test * Convert test sources from .re to .res * Hack to disable Pexp_object usage in old reason-react sources in tests * Regenerate build.ninja * Doc comments changed during conversion * Adjust tests. Co-authored-by: Cristiano Calcagno <cristianoc@users.noreply.github.com>
1 parent 7a09428 commit 427fb8a

File tree

105 files changed

+5276
-6436
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

105 files changed

+5276
-6436
lines changed

jscomp/bsb/templates/basic-reason/src/Demo.re

-1
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Js.log("Hello, ReScript!")

jscomp/bsb/templates/react-hooks/src/BlinkingGreeting/BlinkingGreeting.re

-25
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
@react.component
2+
let make = (~children) => {
3+
let (show, setShow) = React.useState(() => true)
4+
5+
// Notice that instead of `useEffect`, we have `useEffect0`. See
6+
// reasonml.github.io/reason-react/docs/en/components#hooks for more info
7+
React.useEffect0(() => {
8+
let id = Js.Global.setInterval(() => setShow(previousShow => !previousShow), 1000)
9+
10+
Some(() => Js.Global.clearInterval(id))
11+
})
12+
13+
let style = if show {
14+
ReactDOMRe.Style.make(~opacity="1", ~transition="opacity 1s", ())
15+
} else {
16+
ReactDOMRe.Style.make(~opacity="0", ~transition="opacity 1s", ())
17+
}
18+
19+
<div style> children </div>
20+
}

jscomp/bsb/templates/react-hooks/src/ExampleStyles.re jscomp/bsb/templates/react-hooks/src/ExampleStyles.res

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
let reasonReactBlue = "#48a9dc";
1+
let reasonReactBlue = "#48a9dc"
22

33
// The {j|...|j} feature is just string interpolation, from
44
// bucklescript.github.io/docs/en/interop-cheatsheet#string-unicode-interpolation
55
// This allows us to conveniently write CSS, together with variables, by
66
// constructing a string
7-
let style = {j|
7+
let style = j`
88
body {
99
background-color: rgb(224, 226, 229);
1010
display: flex;
@@ -41,4 +41,4 @@ let style = {j|
4141
padding: 16px;
4242
border-radius: 0px 0px 12px 12px;
4343
}
44-
|j};
44+
`

jscomp/bsb/templates/react-hooks/src/FetchedDogPictures/FetchedDogPictures.re

-70
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
@val external fetch: string => Js.Promise.t<'a> = "fetch"
2+
3+
type state =
4+
| LoadingDogs
5+
| ErrorFetchingDogs
6+
| LoadedDogs(array<string>)
7+
8+
@react.component
9+
let make = () => {
10+
let (state, setState) = React.useState(() => LoadingDogs)
11+
12+
// Notice that instead of `useEffect`, we have `useEffect0`. See
13+
// reasonml.github.io/reason-react/docs/en/components#hooks for more info
14+
React.useEffect0(() => {
15+
{
16+
open Js.Promise
17+
fetch("https://dog.ceo/api/breeds/image/random/3")
18+
|> then_(response => response["json"]())
19+
|> then_(jsonResponse => {
20+
setState(_previousState => LoadedDogs(jsonResponse["message"]))
21+
Js.Promise.resolve()
22+
})
23+
|> catch(_err => {
24+
setState(_previousState => ErrorFetchingDogs)
25+
Js.Promise.resolve()
26+
})
27+
|> ignore
28+
}
29+
30+
// Returning None, instead of Some(() => ...), means we don't have any
31+
// cleanup to do before unmounting. That's not 100% true. We should
32+
// technically cancel the promise. Unofortunately, there's currently no
33+
// way to cancel a promise. Promises in general should be way less used
34+
// for React components; but since folks do use them, we provide such an
35+
// example here. In reality, this fetch should just be a plain callback,
36+
// with a cancellation API
37+
None
38+
})
39+
40+
<div
41+
style={ReactDOMRe.Style.make(
42+
~height="120px",
43+
~display="flex",
44+
~alignItems="center",
45+
~justifyContent="center",
46+
(),
47+
)}>
48+
{switch state {
49+
| ErrorFetchingDogs => React.string("An error occurred!")
50+
| LoadingDogs => React.string("Loading...")
51+
| LoadedDogs(dogs) =>
52+
dogs
53+
->Belt.Array.mapWithIndex((i, dog) => {
54+
let imageStyle = ReactDOMRe.Style.make(
55+
~height="120px",
56+
~width="100%",
57+
~marginRight=i === Js.Array.length(dogs) - 1 ? "0px" : "8px",
58+
~borderRadius="8px",
59+
~boxShadow="0px 4px 16px rgb(200, 200, 200)",
60+
~backgroundSize="cover",
61+
~backgroundImage=j`url($dog)`,
62+
~backgroundPosition="center",
63+
(),
64+
)
65+
<div key=dog style=imageStyle />
66+
})
67+
->React.array
68+
}}
69+
</div>
70+
}

jscomp/bsb/templates/react-hooks/src/Index.re

-51
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Entry point
2+
3+
@val external document: {..} = "document"
4+
5+
// We're using raw DOM manipulations here, to avoid making you read
6+
// ReasonReact when you might precisely be trying to learn it for the first
7+
// time through the examples later.
8+
let style = document["createElement"]("style")
9+
document["head"]["appendChild"](style)
10+
style["innerHTML"] = ExampleStyles.style
11+
12+
let makeContainer = text => {
13+
let container = document["createElement"]("div")
14+
container["className"] = "container"
15+
16+
let title = document["createElement"]("div")
17+
title["className"] = "containerTitle"
18+
title["innerText"] = text
19+
20+
let content = document["createElement"]("div")
21+
content["className"] = "containerContent"
22+
23+
let () = container["appendChild"](title)
24+
let () = container["appendChild"](content)
25+
let () = document["body"]["appendChild"](container)
26+
27+
content
28+
}
29+
30+
// All 4 examples.
31+
ReactDOMRe.render(
32+
<BlinkingGreeting> {React.string("Hello!")} </BlinkingGreeting>,
33+
makeContainer("Blinking Greeting"),
34+
)
35+
36+
ReactDOMRe.render(<ReducerFromReactJSDocs />, makeContainer("Reducer From ReactJS Docs"))
37+
38+
ReactDOMRe.render(<FetchedDogPictures />, makeContainer("Fetched Dog Pictures"))
39+
40+
ReactDOMRe.render(<ReasonUsingJSUsingReason />, makeContainer("Reason Using JS Using Reason"))

jscomp/bsb/templates/react-hooks/src/ReasonUsingJSUsingReason/ReasonReactCard.re jscomp/bsb/templates/react-hooks/src/ReasonUsingJSUsingReason/ReasonReactCard.res

+2-4
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,5 @@
44
// - ReactJSCard.js can be used by ReasonReact, through bindings in ReasonUsingJSUsingReason.re
55
// - ReasonUsingJSUsingReason.re is used by Index.re
66

7-
[@react.component]
8-
let make = (~style) => {
9-
<div style> {React.string("This is a ReasonReact card")} </div>;
10-
};
7+
@react.component
8+
let make = (~style) => <div style> {React.string("This is a ReasonReact card")} </div>

jscomp/bsb/templates/react-hooks/src/ReasonUsingJSUsingReason/ReasonUsingJSUsingReason.re jscomp/bsb/templates/react-hooks/src/ReasonUsingJSUsingReason/ReasonUsingJSUsingReason.res

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@
66

77
// All you need to do to use a ReactJS component in ReasonReact, is to write the lines below!
88
// reasonml.github.io/reason-react/docs/en/components#import-from-js
9-
[@react.component] [@bs.module]
10-
external make: unit => React.element = "./ReactJSCard";
9+
@react.component @module
10+
external make: unit => React.element = "./ReactJSCard"

jscomp/bsb/templates/react-hooks/src/ReducerFromReactJSDocs/ReducerFromReactJSDocs.re jscomp/bsb/templates/react-hooks/src/ReducerFromReactJSDocs/ReducerFromReactJSDocs.res

+19-18
Original file line numberDiff line numberDiff line change
@@ -2,36 +2,37 @@
22
// https://reactjs.org/docs/hooks-reference.html#usereducer
33

44
// A little extra we've put, because the ReactJS example has no styling
5-
let leftButtonStyle = ReactDOMRe.Style.make(~borderRadius="4px 0px 0px 4px", ~width="48px", ());
6-
let rightButtonStyle = ReactDOMRe.Style.make(~borderRadius="0px 4px 4px 0px", ~width="48px", ());
7-
let containerStyle = ReactDOMRe.Style.make(~display="flex", ~alignItems="center", ~justifyContent="space-between", ());
5+
let leftButtonStyle = ReactDOMRe.Style.make(~borderRadius="4px 0px 0px 4px", ~width="48px", ())
6+
let rightButtonStyle = ReactDOMRe.Style.make(~borderRadius="0px 4px 4px 0px", ~width="48px", ())
7+
let containerStyle = ReactDOMRe.Style.make(
8+
~display="flex",
9+
~alignItems="center",
10+
~justifyContent="space-between",
11+
(),
12+
)
813

914
// Record and variant need explicit declarations.
10-
type state = {count: int};
15+
type state = {count: int}
1116

1217
type action =
1318
| Increment
14-
| Decrement;
19+
| Decrement
1520

16-
let initialState = {count: 0};
21+
let initialState = {count: 0}
1722

18-
let reducer = (state, action) => {
19-
switch (action) {
23+
let reducer = (state, action) =>
24+
switch action {
2025
| Increment => {count: state.count + 1}
2126
| Decrement => {count: state.count - 1}
22-
};
23-
};
27+
}
2428

25-
[@react.component]
29+
@react.component
2630
let make = () => {
27-
let (state, dispatch) = React.useReducer(reducer, initialState);
31+
let (state, dispatch) = React.useReducer(reducer, initialState)
2832

2933
// We can use a fragment here, but we don't, because we want to style the counter
3034
<div style=containerStyle>
31-
<div>
32-
{React.string("Count: ")}
33-
{React.string(string_of_int(state.count))}
34-
</div>
35+
<div> {React.string("Count: ")} {React.string(string_of_int(state.count))} </div>
3536
<div>
3637
<button style=leftButtonStyle onClick={_event => dispatch(Decrement)}>
3738
{React.string("-")}
@@ -40,5 +41,5 @@ let make = () => {
4041
{React.string("+")}
4142
</button>
4243
</div>
43-
</div>;
44-
};
44+
</div>
45+
}

0 commit comments

Comments
 (0)