-
-
Notifications
You must be signed in to change notification settings - Fork 196
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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) => { | ||
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 = { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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! There was a problem hiding this comment. Choose a reason for hiding this commentThe 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)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: I think There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 />; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,6 @@ open Revery_Core.Window; | |
|
||
type state = { | ||
value: string, | ||
placeholder: string, | ||
isFocused: bool, | ||
}; | ||
|
||
|
@@ -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 | ||
? { | ||
|
@@ -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 | ||
|
@@ -87,31 +90,36 @@ let make = | |
*/ | ||
let slots = | ||
React.Hooks.effect( | ||
OnMount, | ||
Always, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, | ||
|
@@ -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 | ||
|
@@ -236,4 +244,4 @@ let createElement = | |
~onChange, | ||
(), | ||
), | ||
); | ||
); |
There was a problem hiding this comment.
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: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.htmlNo changes necessary - just thought you might be interested in this 😎 Nice use of variants for this!
There was a problem hiding this comment.
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.