Skip to content

Instantly share code, notes, and snippets.

@markbrown4
Last active July 13, 2016 07:06
Show Gist options
  • Save markbrown4/a265f89b1801983c676cdaa40d70419b to your computer and use it in GitHub Desktop.
Save markbrown4/a265f89b1801983c676cdaa40d70419b to your computer and use it in GitHub Desktop.
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