-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTodo.re
57 lines (53 loc) · 1.45 KB
/
Todo.re
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
type todo = {
name: string,
id: int,
};
// in the ocaml documentation
let filterArray = (~f, arr) =>
arr |> ArrayLabels.to_list |> ListLabels.filter(~f) |> ArrayLabels.of_list;
[@react.component]
let make = () => {
let (input, setInput) = React.useState(() => "");
// ml langs have array
let (todos, setTodo) = React.useState(() => [||]);
<div>
<h1 style={ReactDOM.Style.make(~color="red", ())}>
{React.string("TODO App")}
</h1>
<input
value=input
type_="text"
placeholder="add to todo"
onChange={evt => {
//Js.log(evt);
let newValue = React.Event.Form.target(evt)##value;
setInput(_ => newValue);
}}
/>
<button
onClick={_e => {
// find array length
let newTodo = {name: input, id: Array.length(todos)};
setInput(_ => "");
let updatedTodos = Array.append(todos, [|newTodo|]);
setTodo(_ => updatedTodos);
}}>
{React.string("Add Todo")}
</button>
<ul>
{todos
|> Array.map(todo =>
<li
onClick={_ => {
let newTodos = filterArray(~f=t => t.id !== todo.id, todos);
setTodo(_ => newTodos);
}}
key={string_of_int(todo.id)}>
{React.string(todo.name)}
</li>
)
//|> Array.of_list // Convert list to array
|> React.array}
</ul>
</div>; // Convert array to React.element
};