Last active
July 13, 2016 07:06
-
-
Save markbrown4/a265f89b1801983c676cdaa40d70419b to your computer and use it in GitHub Desktop.
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
const view = (state, prev, send) => { | |
return html` | |
<div onload=${()=> send('getTodos')}> | |
<form onsubmit=${onSubmit}> | |
<input type="text" placeholder="New item" id="title"> | |
</form> | |
${ TodoList({ todos: state.todos }, send) } | |
</div>` | |
function onSubmit (e) { | |
const input = e.target.children[0] | |
send('addTodo', { title: input.value }) | |
input.value = '' | |
e.preventDefault() | |
} | |
} | |
const TodoList = (props, send)=> { | |
const todos = props.todos | |
return html` | |
<ul> | |
${todos.map((todo, index)=> { | |
return Todo({ todo: todo, index: index }, send) | |
})} | |
</ul> | |
` | |
} | |
const Todo = (props, send)=> { | |
const todo = props.todo | |
const index = props.index | |
return html` | |
<li> | |
<input | |
type="checkbox" | |
${todo.completed ? 'checked' : ''} | |
onchange=${(e)=> onChange(e, index)} | |
/> | |
${todo.title} | |
</li> | |
` | |
function onChange(e, index) { | |
const updates = { completed: e.target.checked } | |
send('updateTodo', { index: index, updates: updates }) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment