Skip to content

Commit b802764

Browse files
committed
fix: rename generated example module
Rename the temp example source from _tempFile to Example so derived JS output no longer leaks the temp filename. Refresh the JSX preserved-output docs that are in scope for the new three-tab ReScript CodeTab flow.
1 parent 90dd597 commit b802764

20 files changed

Lines changed: 1570 additions & 192 deletions

markdown-pages/docs/manual/tagged-templates.mdx

Lines changed: 58 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ function s(strings, parameters) {
9898

9999
Now that you have defined your tag function, you can use it this way:
100100

101-
<CodeTab labels={["ReScript", "JS Output"]}>
101+
<CodeTab labels={["ReScript", "JS Output", "JSX Preserved Output"]}>
102102

103103
```res
104104
module Greetings = {
@@ -137,7 +137,7 @@ function s(strings, parameters) {
137137
);
138138
}
139139

140-
function _tempFile$Greetings(props) {
140+
function Example$Greetings(props) {
141141
return JsxRuntime.jsx("div", {
142142
children: s(
143143
[`hello `, ` you're `, ` year old!`],
@@ -156,7 +156,62 @@ function _tempFile$Greetings(props) {
156156
}
157157

158158
let Greetings = {
159-
make: _tempFile$Greetings,
159+
make: Example$Greetings,
160+
};
161+
162+
export { s, Greetings };
163+
```
164+
165+
```jsx
166+
import * as Stdlib_Array from "@rescript/runtime/lib/es6/Stdlib_Array.js";
167+
import * as JsxRuntime from "react/jsx-runtime";
168+
169+
function s(strings, parameters) {
170+
return Stdlib_Array.reduceWithIndex(
171+
parameters,
172+
strings[0],
173+
(acc, param, i) => {
174+
let s = strings[(i + 1) | 0];
175+
let p;
176+
switch (param.TAG) {
177+
case "I":
178+
case "F":
179+
p = param._0.toString();
180+
break;
181+
case "S":
182+
p = param._0;
183+
break;
184+
case "Bool":
185+
p = param._0 ? "true" : "false";
186+
break;
187+
}
188+
return acc + p + s;
189+
},
190+
);
191+
}
192+
193+
function Example$Greetings(props) {
194+
return (
195+
<div>
196+
{s(
197+
[`hello `, ` you're `, ` year old!`],
198+
[
199+
{
200+
TAG: "S",
201+
_0: props.name,
202+
},
203+
{
204+
TAG: "I",
205+
_0: props.age,
206+
},
207+
],
208+
)}
209+
</div>
210+
);
211+
}
212+
213+
let Greetings = {
214+
make: Example$Greetings,
160215
};
161216

162217
export { s, Greetings };

markdown-pages/docs/react/arrays-and-keys.mdx

Lines changed: 167 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ Whenever we are transforming data into an array of elements and put it in our Re
1818

1919
Arrays require a special function `React.array` to convert an `array<Jsx.element>` to render as `Jsx.element`.
2020

21-
<CodeTab labels={["ReScript", "JS Output"]}>
21+
<CodeTab labels={["ReScript", "JS Output", "JSX Preserved Output"]}>
2222

2323
```res
2424
type todo = {id: string, text: string}
@@ -38,7 +38,7 @@ let make = () => {
3838
```js
3939
import * as JsxRuntime from "react/jsx-runtime";
4040

41-
function _tempFile(props) {
41+
function Example(props) {
4242
let todos = [
4343
{
4444
id: "todo1",
@@ -63,7 +63,30 @@ function _tempFile(props) {
6363
});
6464
}
6565

66-
let make = _tempFile;
66+
let make = Example;
67+
68+
export { make };
69+
```
70+
71+
```jsx
72+
import * as JsxRuntime from "react/jsx-runtime";
73+
74+
function Example(props) {
75+
let todos = [
76+
{
77+
id: "todo1",
78+
text: "Todo 1",
79+
},
80+
{
81+
id: "todo2",
82+
text: "Todo 2",
83+
},
84+
];
85+
let items = todos.map((todo) => <li key={todo.id}>{todo.text}</li>);
86+
return <ul>{items}</ul>;
87+
}
88+
89+
let make = Example;
6790

6891
export { make };
6992
```
@@ -74,7 +97,7 @@ export { make };
7497

7598
Keys help React identify which elements have been changed, added, or removed throughout each render. Keys should be given to elements inside the array to give the elements a stable identity:
7699

77-
<CodeTab labels={["ReScript", "JS Output"]}>
100+
<CodeTab labels={["ReScript", "JS Output", "JSX Preserved Output"]}>
78101

79102
```res
80103
let numbers = [1, 2, 3, 4, 5]
@@ -102,6 +125,16 @@ let items = numbers.map((number) =>
102125
export { numbers, items };
103126
```
104127

128+
```jsx
129+
import * as JsxRuntime from "react/jsx-runtime";
130+
131+
let numbers = [1, 2, 3, 4, 5];
132+
133+
let items = numbers.map((number) => <li key={number.toString()}>{number}</li>);
134+
135+
export { numbers, items };
136+
```
137+
105138
</CodeTab>
106139

107140
The best way to pick a key is to use a string that uniquely identifies a list item among its siblings. Most often you would use IDs from your data as keys:
@@ -148,7 +181,7 @@ var items = todos.map(function (todo) {
148181

149182
If you don’t have stable IDs for rendered items, you may use the item index as a key as a last resort:
150183

151-
<CodeTab labels={["ReScript", "JS Output"]}>
184+
<CodeTab labels={["ReScript", "JS Output", "JSX Preserved Output"]}>
152185

153186
```res {1..3}
154187
let items = Array.mapWithIndex(todos, (todo, i) => {
@@ -196,13 +229,34 @@ let items = todos.map((todo, i) =>
196229
export { todos, items };
197230
```
198231

232+
```jsx
233+
import * as JsxRuntime from "react/jsx-runtime";
234+
235+
let todos = [
236+
{
237+
id: "todo1",
238+
text: "Todo 1",
239+
},
240+
{
241+
id: "todo2",
242+
text: "Todo 2",
243+
},
244+
];
245+
246+
todos.map((todo) => <li key={todo.id}>{todo.text}</li>);
247+
248+
let items = todos.map((todo, i) => <li key={i.toString()}>{todo.text}</li>);
249+
250+
export { todos, items };
251+
```
252+
199253
</CodeTab>
200254

201255
### Keys Must Only Be Unique Among Siblings
202256

203257
Keys used within arrays should be unique among their siblings. However they don’t need to be globally unique. We can use the same keys when we produce two different arrays:
204258

205-
<CodeTab labels={["ReScript", "JS Output"]}>
259+
<CodeTab labels={["ReScript", "JS Output", "JSX Preserved Output"]}>
206260

207261
```res {6,10,17,18,25,27}
208262
type post = {id: string, title: string, content: string}
@@ -272,7 +326,7 @@ let items = todos.map((todo) =>
272326
),
273327
);
274328

275-
function _tempFile$Blog(props) {
329+
function Example$Blog(props) {
276330
let posts = props.posts;
277331
let sidebar = JsxRuntime.jsx("ul", {
278332
children: posts.map((post) =>
@@ -307,7 +361,7 @@ function _tempFile$Blog(props) {
307361
}
308362

309363
let Blog = {
310-
make: _tempFile$Blog,
364+
make: Example$Blog,
311365
};
312366

313367
let posts = [
@@ -323,20 +377,82 @@ let posts = [
323377
},
324378
];
325379

326-
let blog = JsxRuntime.jsx(_tempFile$Blog, {
380+
let blog = JsxRuntime.jsx(Example$Blog, {
327381
posts: posts,
328382
});
329383

330384
export { todos, items, Blog, posts, blog };
331385
```
332386

387+
```jsx
388+
import * as JsxRuntime from "react/jsx-runtime";
389+
390+
let todos = [
391+
{
392+
id: "todo1",
393+
text: "Todo 1",
394+
},
395+
{
396+
id: "todo2",
397+
text: "Todo 2",
398+
},
399+
];
400+
401+
let items = todos.map((todo) => <li key={todo.id}>{todo.text}</li>);
402+
403+
function Example$Blog(props) {
404+
let posts = props.posts;
405+
let sidebar = (
406+
<ul>
407+
{posts.map((post) => (
408+
<li key={post.id}>{post.title}</li>
409+
))}
410+
</ul>
411+
);
412+
let content = posts.map((post) => (
413+
<div key={post.id}>
414+
<h3>{post.title}</h3>
415+
<p>{post.content}</p>
416+
</div>
417+
));
418+
return (
419+
<div>
420+
{sidebar}
421+
<hr />
422+
{content}
423+
</div>
424+
);
425+
}
426+
427+
let Blog = {
428+
make: Example$Blog,
429+
};
430+
431+
let posts = [
432+
{
433+
id: "1",
434+
title: "Hello World",
435+
content: "Welcome to learning ReScript & React!",
436+
},
437+
{
438+
id: "2",
439+
title: "Installation",
440+
content: "You can install reason-react from npm.",
441+
},
442+
];
443+
444+
let blog = <Blog.make posts={posts} />;
445+
446+
export { todos, items, Blog, posts, blog };
447+
```
448+
333449
</CodeTab>
334450

335451
## Rendering `list` Values
336452

337453
In case you ever want to render a `list` of items, you can do something like this:
338454

339-
<CodeTab labels={["ReScript", "JS Output"]}>
455+
<CodeTab labels={["ReScript", "JS Output", "JSX Preserved Output"]}>
340456

341457
```res
342458
@react.component
@@ -383,7 +499,7 @@ let items = todos.map((todo) =>
383499
),
384500
);
385501

386-
function _tempFile(props) {
502+
function Example(props) {
387503
let items = Stdlib_List.toArray({
388504
hd: {
389505
id: "todo1",
@@ -410,7 +526,46 @@ function _tempFile(props) {
410526
});
411527
}
412528

413-
let make = _tempFile;
529+
let make = Example;
530+
531+
export { todos, items, make };
532+
```
533+
534+
```jsx
535+
import * as Stdlib_List from "@rescript/runtime/lib/es6/Stdlib_List.js";
536+
import * as JsxRuntime from "react/jsx-runtime";
537+
538+
let todos = [
539+
{
540+
id: "todo1",
541+
text: "Todo 1",
542+
},
543+
{
544+
id: "todo2",
545+
text: "Todo 2",
546+
},
547+
];
548+
549+
let items = todos.map((todo) => <li key={todo.id}>{todo.text}</li>);
550+
551+
function Example(props) {
552+
let items = Stdlib_List.toArray({
553+
hd: {
554+
id: "todo1",
555+
text: "Todo 1",
556+
},
557+
tl: {
558+
hd: {
559+
id: "todo2",
560+
text: "Todo 2",
561+
},
562+
tl: /* [] */ 0,
563+
},
564+
}).map((todo) => <li key={todo.id}>{todo.text}</li>);
565+
return <div>{items}</div>;
566+
}
567+
568+
let make = Example;
414569

415570
export { todos, items, make };
416571
```

0 commit comments

Comments
 (0)