Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Example: Adding a very basic version of a todo app #311

Merged
merged 4 commits into from
Feb 9, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion examples/Examples.re
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ let state: state = {
{name: "Game Of Life", render: _ => GameOfLife.render()},
{name: "Screen Capture", render: w => ScreenCapture.render(w)},
{name: "Tree View", render: w => TreeView.render(w)},
{name: "ToDoMVC", render: w => TodoExample.render(w)},
],
selectedExample: "Animation",
};
Expand Down Expand Up @@ -198,4 +199,4 @@ let init = app => {
UI.start(win, render);
};

App.startWithState(state, reducer, init);
App.startWithState(state, reducer, init);
3 changes: 0 additions & 3 deletions examples/Hello.re
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,6 @@ module AnimatedText = {

let render = () =>
<View
onMouseWheel={evt =>
print_endline("onMouseWheel: " ++ string_of_float(evt.deltaY))
}
style=Style.[
position(`Absolute),
justifyContent(`Center),
Expand Down
225 changes: 225 additions & 0 deletions examples/TodoExample.re
Original file line number Diff line number Diff line change
@@ -0,0 +1,225 @@
open Revery.UI;
open Revery.Core;
open Revery.UI.Components;

type todo = {
id: int,
task: string,
isDone: bool,
};

type filter =
| All
| Completed
| NotCompleted;

let textOfFilter = (filter: filter) => {
Copy link
Member

Choose a reason for hiding this comment

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

One convention that comes up in OCaml (and that Reason inherited) is a convention of using a type t with a module, especially if there are functions that use it. So you could potentially refactor this to something like:

module Filter {
   type t =
   | All
   | Completed
   | NotCompleted;

   let show = (v: t) => switch(v) {
   | All => "All"
   | Completed => "Completed"
   | NotCompleted => "NotCompleted"
   };
};

Not something that is strictly necessary, but it's a convention that I found interesting as I start diving into Reason - the ubiquity of a module plus a type t.

It's a little bit different than thinking in an OO language - a lot of times the Module will have a core type + functions that operate on that top. So comes up a lot in the standard modules - like there is a Hashtbl.t('a, 'b) that the Hashtbl module provides functions for: https://caml.inria.fr/pub/docs/manual-ocaml/libref/Hashtbl.html

No changes necessary - just thought you might be interested in this 😎 Nice use of variants for this!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Very useful. Thanks for sharing. One goal I have in helping here is to ramp up on Reason, so thanks for this.

switch (filter) {
| All => "All"
| Completed => "Completed"
| NotCompleted => "NotCompleted"
};
};

type state = {
todos: list(todo),
filter,
inputValue: string,
nextId: int,
};

type action =
| AddTodo
| ChangeFilter(filter)
| UpdateInputTextValue(string)
| ChangeTaskState(int, bool);

let reducer = (action: action, state: state) => {
switch (action) {
| AddTodo => {
...state,
todos: [
{id: state.nextId, task: state.inputValue, isDone: false},
...state.todos,
],
nextId: state.nextId + 1,
}
| UpdateInputTextValue(text) => {...state, inputValue: text}
| ChangeTaskState(id, isDone) =>
let todos =
List.map(
item => item.id == id ? {...item, isDone} : item,
state.todos,
);
{...state, todos};
| ChangeFilter(filter) => {...state, filter}
};
};

module FilterSection = {
Copy link
Member

Choose a reason for hiding this comment

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

One thing you might be interested in - @jchavarri and @wokalski are looking at ways to streamline the component creation to a function / remove boiler plate here: briskml/brisk-reconciler#6

Nothing to be changed since we don't have it yet, but would be great to get your feedback on that since you have a fresh perspective!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That will be great. I will check this out. Most of the code to create a basic component didn't feel natural. glad you are looking into it.

let component = React.component("FilterSection");

let make = (_children, currentFilier, onPickingFilter) =>
component((_slots: React.Hooks.empty) =>
<View
style=Style.[
flexDirection(`Row),
width(500),
alignItems(`Center),
justifyContent(`Center),
]>
<Button
height=50
width=150
fontSize=15
title="All"
color={
switch (currentFilier) {
| All => Colors.dodgerBlue
| _ => Colors.lightSkyBlue
}
}
onClick={() => onPickingFilter(All)}
/>
<Button
height=50
width=150
fontSize=15
title="Completed"
color={
switch (currentFilier) {
| Completed => Colors.dodgerBlue
| _ => Colors.lightSkyBlue
}
}
onClick={() => onPickingFilter(Completed)}
/>
<Button
height=50
width=150
fontSize=15
title="Not Completed"
color={
switch (currentFilier) {
| NotCompleted => Colors.dodgerBlue
| _ => Colors.lightSkyBlue
}
}
onClick={() => onPickingFilter(NotCompleted)}
/>
</View>
);

let createElement = (~children, ~currentFilier, ~onPickingFilter, ()) =>
React.element(make(children, currentFilier, onPickingFilter));
Copy link
Member

Choose a reason for hiding this comment

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

Nit: I think currentFilier should be currentFilter?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks. Fixed it.

};

module Example = {
let component = React.component("TodoMVC");

let make = window =>
component(slots => {
let ({todos, inputValue, filter, _}, dispatch, slots) =
React.Hooks.reducer(
~initialState={
todos: [],
filter: All,
inputValue: "",
nextId: 0,
},
reducer,
slots,
);

let _slots: React.Hooks.empty =
React.Hooks.effect(
OnMount,
() => {
let unsubscribe = () => ();
Some(unsubscribe);
},
slots,
);

let renderTodo = task => {
<View style=Style.[flexDirection(`Row)]>
<Checkbox
checked={task.isDone}
onChange={checked => dispatch(ChangeTaskState(task.id, checked))}
/>
<Text
style=Style.[
color(Colors.black),
fontFamily("Roboto-Regular.ttf"),
fontSize(20),
margin(4),
]
text={task.task}
/>
</View>;
};

let filteredList =
List.filter(
task =>
switch (filter) {
| All => true
| Completed => task.isDone
| NotCompleted => !task.isDone
},
todos,
);

let listOfTodos = List.map(renderTodo, filteredList);
<View
style=Style.[
position(`Absolute),
top(0),
bottom(0),
left(0),
right(0),
alignItems(`Center),
justifyContent(`Center),
flexDirection(`Column),
backgroundColor(Colors.white),
]>
<FilterSection
currentFilier=filter
onPickingFilter={filter => dispatch(ChangeFilter(filter))}
/>
<View style=Style.[flexDirection(`Row)]>
<Input
style=Style.[width(400)]
window
placeholder="Add your Todo here"
onChange={(~value) => dispatch(UpdateInputTextValue(value))}
/>
<Button
width=50
height=50
disabled={
switch (inputValue) {
| "" => true
| _ => false
}
}
title="+"
onClick={() => dispatch(AddTodo)}
/>
</View>
<ScrollView
style=Style.[
height(200),
width(450),
border(~width=1, ~color=Colors.black),
]>
<View> ...listOfTodos </View>
</ScrollView>
</View>;
});

let createElement = (~window, ~children as _, ()) =>
React.element(make(window));
};

let render = window => <Example window />;
40 changes: 24 additions & 16 deletions src/UI_Components/Input.re
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ open Revery_Core.Window;

type state = {
value: string,
placeholder: string,
isFocused: bool,
};

Expand All @@ -28,7 +27,8 @@ let reducer = (action, state) =>
switch (action) {
| SetFocus(isFocused) => {...state, isFocused}
| UpdateText(t) =>
state.isFocused ? {...state, value: addCharacter(state.value, t)} : state
state.isFocused
? {isFocused: true, value: addCharacter(state.value, t)} : state
| Backspace =>
state.isFocused
? {
Expand Down Expand Up @@ -67,17 +67,20 @@ let make =
(
~window,
~style,
~value,
~value as valueParam,
~placeholder,
~cursorColor,
~placeholderColor,
~onChange,
(),
) =>
component(slots => {
let initialState = {value, placeholder, isFocused: false};
let (state, dispatch, slots) =
React.Hooks.reducer(~initialState, reducer, slots);
React.Hooks.reducer(
~initialState={value: valueParam, isFocused: false},
reducer,
slots,
);

/*
TODO: Setting the hook to run only on mount means that the onChange
Expand All @@ -87,31 +90,36 @@ let make =
*/
let slots =
React.Hooks.effect(
OnMount,
Always,
Copy link
Member

Choose a reason for hiding this comment

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

Nice catch on this issue @faisalil - thanks for investigating & fixing it!

() =>
Some(
Event.subscribe(
window.onKeyPress,
event => {
dispatch(UpdateText(event.character));
onChange(~value);
onChange(~value=addCharacter(state.value, event.character));
},
),
),
slots,
);

let handleKeyDown = (~dispatch, event: Events.keyEvent) =>
switch (event.key) {
| Key.KEY_BACKSPACE => {
dispatch(Backspace);
onChange(~value = removeCharacter(state.value))
}
| _ => ()
};

let slots =
React.Hooks.effect(
OnMount,
Always,
() =>
Some(
Event.subscribe(
window.onKeyDown,
event => {
handleKeyDown(~dispatch, event);
onChange(~value=state.value);
},
Event.subscribe(window.onKeyDown, event =>
handleKeyDown(~dispatch, event)
),
),
slots,
Expand All @@ -132,7 +140,7 @@ let make =

let hasPlaceholder = String.length(state.value) < 1;

let content = hasPlaceholder ? state.placeholder : state.value;
let content = hasPlaceholder ? placeholder : state.value;

/*
computed styles
Expand Down Expand Up @@ -236,4 +244,4 @@ let createElement =
~onChange,
(),
),
);
);