-
-
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,221 @@ | ||
open Revery.UI; | ||
open Revery.Core; | ||
open Revery.UI.Components; | ||
|
||
type todo = { | ||
id: int, | ||
task: string, | ||
isDone: bool, | ||
}; | ||
|
||
module Filter = { | ||
type t = | ||
| All | ||
| Completed | ||
| NotCompleted; | ||
|
||
let show = (v: t) => | ||
switch (v) { | ||
| All => "All" | ||
| Completed => "Completed" | ||
| NotCompleted => "NotCompleted" | ||
}; | ||
}; | ||
|
||
type state = { | ||
todos: list(todo), | ||
filter: Filter.t, | ||
inputValue: string, | ||
nextId: int, | ||
}; | ||
|
||
type action = | ||
| AddTodo | ||
| ChangeFilter(Filter.t) | ||
| 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 = { | ||
let component = React.component("FilterSection"); | ||
|
||
let make = (_children, currentFilter, 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={Filter.show(Filter.All)} | ||
color={ | ||
switch (currentFilter) { | ||
| Filter.All => Colors.dodgerBlue | ||
| _ => Colors.lightSkyBlue | ||
} | ||
} | ||
onClick={() => onPickingFilter(Filter.All)} | ||
/> | ||
<Button | ||
height=50 | ||
width=150 | ||
fontSize=15 | ||
title={Filter.show(Completed)} | ||
color={ | ||
switch (currentFilter) { | ||
| Completed => Colors.dodgerBlue | ||
| _ => Colors.lightSkyBlue | ||
} | ||
} | ||
onClick={() => onPickingFilter(Completed)} | ||
/> | ||
<Button | ||
height=50 | ||
width=150 | ||
fontSize=15 | ||
title={Filter.show(NotCompleted)} | ||
color={ | ||
switch (currentFilter) { | ||
| NotCompleted => Colors.dodgerBlue | ||
| _ => Colors.lightSkyBlue | ||
} | ||
} | ||
onClick={() => onPickingFilter(NotCompleted)} | ||
/> | ||
</View> | ||
); | ||
|
||
let createElement = (~children, ~currentFilter, ~onPickingFilter, ()) => | ||
React.element(make(children, currentFilter, onPickingFilter)); | ||
}; | ||
|
||
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 | ||
currentFilter=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 />; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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,35 @@ 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 +139,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 | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 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 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.