Skip to content

Commit d12f6d2

Browse files
committed
website: add examples in cheatsheet
1 parent 7b2c137 commit d12f6d2

File tree

1 file changed

+296
-1
lines changed

1 file changed

+296
-1
lines changed

website/docs/cheatsheet.md

Lines changed: 296 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,302 @@ If you are not familiar with ReasonML or BuckleScript, be sure to check
1010
- [BuckleScript Cheatsheet](https://bucklescript.github.io/docs/en/interop-cheatsheet)
1111
to learn how to interop with JavaScript
1212

13-
@todo
13+
## JSX String
14+
15+
### JavaScript JSX String
16+
17+
```javascript
18+
<Text>Hello, world!</Text>
19+
```
20+
21+
### Reason JSX String
22+
23+
```reason
24+
<Text> "Hello, world!"->React.string </Text>
25+
```
26+
27+
## JSX Number (int)
28+
29+
### JavaScript JSX Number (int)
30+
31+
```javascript
32+
<Text>42</Text>
33+
```
34+
35+
### Reason JSX Number (int)
36+
37+
```reason
38+
<Text>
39+
42
40+
->Js.Int.toString
41+
->React.string
42+
</Text>
43+
```
44+
45+
## JSX Number (float)
46+
47+
### JavaScript JSX Number (float)
48+
49+
```javascript
50+
<Text>4.2</Text>
51+
```
52+
53+
### Reason JSX Number (float)
54+
55+
```reason
56+
<Text>
57+
4.2
58+
->Js.Float.toString
59+
->React.string
60+
</Text>
61+
```
62+
63+
## JSX Array (of string)
64+
65+
### JavaScript JSX Array (of string)
66+
67+
```javascript
68+
{items.map((item, i) =>
69+
<Text key={i++item}>
70+
item
71+
</Text>
72+
)}
73+
```
74+
75+
### Reason JSX Array (of string)
76+
77+
```reason
78+
{items
79+
->Belt.Array.mapWithIndex((items, index) =>
80+
<Text
81+
key=((i->Js.Int.toString)++item)>
82+
item->React.string
83+
</Text>
84+
)
85+
->React.array}
86+
```
87+
88+
## JSX conditional string
89+
90+
### JavaScript JSX conditional string
91+
92+
```javascript
93+
<Text>{condition && something}</Text>
94+
```
95+
96+
### Reason JSX conditional string
97+
98+
```reason
99+
<Text>
100+
{
101+
condition ? something->React.string : React.null
102+
}
103+
</Text>
104+
```
105+
106+
## JSX optional value
107+
108+
### JavaScript JSX optional string
109+
110+
_Assuming `something` is a `string` that can be `undefined`._
111+
112+
```javascript
113+
{
114+
something && <Text>{something.toUpperCase()}</Text>;
115+
}
116+
```
117+
118+
### Reason JSX optional string
119+
120+
_Recommended: Assuming `something` is an
121+
[optional](https://reasonml.github.io/docs/en/null-undefined-option) `string`._
122+
123+
```reason
124+
{
125+
something
126+
->Belt.Option.map(thing =>
127+
<Text>
128+
thing
129+
->Js.String.toUpperCase
130+
->React.string
131+
</Text>
132+
)
133+
->Belt.Option.getWithDefault(React.null)
134+
}
135+
```
136+
137+
_If you have to work with JavaScript/JSON: Assuming `something` is a JavaScript
138+
`string` that can be `undefined`._
139+
140+
```reason
141+
{
142+
something
143+
->Js.Nullable.toOption /* convert undefined || string as option(string) */
144+
->Belt.Option.map(thing =>
145+
<Text>
146+
thing
147+
->Js.String.toUpperCase
148+
->React.string
149+
</Text>
150+
)
151+
->Belt.Option.getWithDefault(React.null)
152+
}
153+
```
154+
155+
## React Native StyleSheet
156+
157+
### JavaScript React Native StyleSheet
158+
159+
```javascript
160+
import { StyleSheet } from "react-native";
161+
162+
const styles = StyleSheet.create({
163+
container: {
164+
maxHeight: 600,
165+
width: 800,
166+
justifyContent: "flex-start",
167+
alignItems: "center",
168+
margin: "auto",
169+
},
170+
cornerThing: {
171+
position: "absolute",
172+
top: 100,
173+
right: -20,
174+
transform: [{ rotate: "4deg" }],
175+
},
176+
text: {
177+
textTransform: "uppercase",
178+
},
179+
});
180+
181+
console.log(StyleSheet.flatten([styles.container]));
182+
```
183+
184+
### Reason React Native StyleSheet
185+
186+
```reason
187+
open ReactNative;
188+
189+
let styles =
190+
Style.(
191+
/* = open Style; just between ()*/
192+
/* this is useful since all units & style methods comes from Style module */
193+
StyleSheet.create({
194+
"container":
195+
viewStyle(
196+
~maxHeight=600.->dp,
197+
~width=800.->pct,
198+
~justifyContent=`flexStart,
199+
~alignItems=`center,
200+
~margin=auto,
201+
(),
202+
),
203+
"cornerThing":
204+
viewStyle(
205+
~position=`absolute,
206+
~top=100.->dp,
207+
~right=(-20.)->dp,
208+
~transform=[|rotate(~rotate=4.->deg)|],
209+
(),
210+
),
211+
"text": textStyle(~textTransform=`uppercase, ()),
212+
})
213+
);
214+
215+
Js.log(StyleSheet.flatten([|styles##container|]));
216+
```
217+
218+
## Concatened styles
219+
220+
### JavaScript Concatened styles
221+
222+
```javascript
223+
<View style={[styles.container, styles.containerAdditionalStyles]} />
224+
```
225+
226+
### Reason Concatened styles
227+
228+
```reason
229+
<View
230+
style=Style.(array([|
231+
styles##container,
232+
styles##containerAdditionalStyles
233+
|]))
234+
/>
235+
```
236+
237+
## Optional styles
238+
239+
### JavaScript Optional styles
240+
241+
```javascript
242+
<View
243+
style={[
244+
styles.container,
245+
condition && styles.containerAdditionalStyles,
246+
condition2 && { width: 40 },
247+
]}
248+
/>
249+
```
250+
251+
### Reason Optional styles
252+
253+
```reason
254+
<View
255+
style=Style.(arrayOption([|
256+
Some(styles##container),
257+
condition ? Some(styles##containerAdditionalStyles) : None,
258+
condition2 ? Some(style(~width=40.->dp, ())) : None,
259+
|]))
260+
/>
261+
```
262+
263+
## Hello world
264+
265+
### JavaScript Hello World
266+
267+
```javascript
268+
/* App.js */
269+
import React, { Component } from "react";
270+
import { Text, View } from "react-native";
271+
272+
export default class HelloWorld extends Component {
273+
render() {
274+
return (
275+
<View
276+
style={{
277+
flex: 1,
278+
justifyContent: "center",
279+
alignItems: "center",
280+
}}
281+
>
282+
<Text>Hello, {this.props.name || "world"}!</Text>
283+
</View>
284+
);
285+
}
286+
}
287+
```
288+
289+
### Reason Hello World
290+
291+
```reason
292+
/* App.re */
293+
open Belt;
294+
open ReactNative;
295+
296+
[@react.component]
297+
let make = (~name=?) => {
298+
<View
299+
style=Style.(
300+
style(~flex=1., ~justifyContent=`center, ~alignItems=`center, ())
301+
)>
302+
<Text>
303+
{("Hello, " ++ name->Option.getWithDefault("world") ++ "!")
304+
->React.string}
305+
</Text>
306+
</View>;
307+
};
308+
```
14309

15310
---
16311

0 commit comments

Comments
 (0)