Todo Example
Riot custom components are the building blocks for user interfaces. They make the “view” part of the application. Let’s start with an extended <todo>
example highlighting various features of Riot:
<todo>
<h3>{ props.title }</h3>
<ul>
<li each={ item in state.items }>
<label class={ item.done ? 'completed' : null }>
<input
type="checkbox"
checked={ item.done }
onclick={ () => toggle(item) } />
{ item.title }
</label>
</li>
</ul>
<form onsubmit={ add }>
<input onkeyup={ edit } value={ state.text } />
<button disabled={ !state.text }>
Add #{ state.items.length + 1 }
</button>
</form>
<script>
export default {
onBeforeMount(props, state) {
// initial state
this.state = {
items: props.items,
text: ''
}
},
edit(e) {
// update only the text state
this.update({
text: e.target.value
})
},
add(e) {
e.preventDefault()
if (this.state.text) {
this.update({
items: [
...this.state.items,
// add a new item
{title: this.state.text}
],
text: ''
})
}
},
toggle(item) {
item.done = !item.done
// trigger a component update
this.update()
}
}
</script>
</todo>
Custom components are compiled to javascript.