Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions packages/demo/bsconfig.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
{
"name": "reason-scripts",
"sources": [
"src"
],
"sources": {
"dir" : "src",
"subdirs" : true
},
"bs-dependencies": [
"bs-reform",
"reason-react",
Expand Down
3 changes: 3 additions & 0 deletions packages/demo/src/App.re
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,13 @@ let make = (~message) => {
<a href="/new-post"> "New post form demo "->React.string </a>
<br />
<a href="/fav-colors"> "Form with array field demo"->React.string </a>
<br />
<a href="/todo"> "Form with array todo demo"->React.string </a>
<div style={ReactDOMRe.Style.make(~padding="100px", ())}>
{switch (url.path) {
| ["new-post"] => <PostAddNext />
| ["fav-colors"] => <FavoriteColorsForm />
| ["todo"] => <TodoListForm />
| _ =>
<p className="App-intro">
{ReasonReact.string("Say hello to ReForm")}
Expand Down
91 changes: 91 additions & 0 deletions packages/demo/src/modules/TodoListForm.re
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
open BsReform;

module TodoLenses = [%lenses
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ops forgot to say to remove this one as it is redundant for now

type todo = {
content: string,
completed: bool,
}
];

module StateLenses = [%lenses type state = {todos: array(TodoLenses.todo)}];

module TodoForm = ReForm.Make(StateLenses);

[@react.component]
let make = () => {
let {state, submit, arrayPush, arrayUpdateByIndex, arrayRemoveByIndex}: TodoForm.api =
TodoForm.use(
~schema={
TodoForm.Validation.Schema([|
Custom(
Todos,
({todos}) => {
let length = Array.length(todos);

length < 0
|| Belt.Array.some(todos, item =>
Js.String.length(item.content) == 0
)
? Error("Invalid todo") : Valid;
},
),
|]);
},
~onSubmit=
({state}) => {
Js.log2("State", state);
None;
},
~initialState={todos: [||]},
(),
);

<div>
<form
onSubmit={event => {
ReactEvent.Synthetic.preventDefault(event);
submit();
}}>
<button
onClick={_ => arrayPush(Todos, {content: "", completed: false})}>
{"Add Task" |> React.string}
</button>
{state.values.todos
->Belt.Array.mapWithIndex((index, todo) =>
<>
<hr />
<label>
<span> {" Task description " |> React.string} </span>
<input
value={todo.content}
onChange={BsReform.Helpers.handleChange(content =>
arrayUpdateByIndex(
~field=Todos,
~index,
{...todo, content},
)
)}
/>
</label>
<span>
{(todo.completed ? "Done" : "Not done") |> React.string}
</span>
<button
onClick={_ =>
arrayUpdateByIndex(
~field=Todos,
~index,
{...todo, completed: !todo.completed},
)
}>
{"Toggle" |> React.string}
</button>
<button onClick={_ => arrayRemoveByIndex(Todos, index)}>
{"Remove" |> React.string}
</button>
</>
)
->React.array}
</form>
</div>;
};