WIM/WIM4.1/tp/tp5/ex1/todo.riot

78 lines
1.7 KiB
Plaintext
Raw Normal View History

2022-06-07 13:34:26 +02:00
<todo>
<h3>{ props.title }</h3>
<ul>
2022-06-11 12:09:27 +02:00
<li each={ item in state.items.filter(e => e.done == state.search || state.search === undefined ) }>
2022-06-07 13:34:26 +02:00
<label class={ item.done ? 'completed' : null }>
<input
type="checkbox"
checked={ item.done }
onclick={ () => toggle(item) } />
{ item.title }
</label>
</li>
</ul>
<form onsubmit={ add }>
<input oninput={ edit } />
<button disabled={ !state.text }>
Add #{ state.items.length + 1 }
</button>
2022-06-11 12:09:27 +02:00
<button disabled={this.state.items.filter(e => e["done"]).length == 0} onclick={ clear }>Clear done</button>
2022-06-07 13:34:26 +02:00
<ul class="filters">
2022-06-11 12:09:27 +02:00
<li> <a onclick={() =>filtre(undefined)} href="#">All</a></li>
<li> <a onclick={() => filtre(false)} href="#">Active</a></li>
<li> <a onclick={() => filtre(true)} href="#">Done</a></li>
2022-06-07 13:34:26 +02:00
</ul>
</form>
<script>
export default {
onBeforeMount(props, state) {
// initial state
this.state = {
items: props.items,
2022-06-11 12:09:27 +02:00
text: '',
search: undefined
2022-06-07 13:34:26 +02:00
}
},
edit(e) {
// update only the text state
2022-06-11 12:09:27 +02:00
e.preventDefault()
2022-06-07 13:34:26 +02:00
this.update({
text: e.target.value
})
},
clear(e) {
2022-06-11 12:09:27 +02:00
e.preventDefault()
this.state.items = this.state.items.filter(e => e.done == false)
this.update()
2022-06-07 13:34:26 +02:00
// COMPLETEZ
},
add(e) {
// COMPLETEZ
2022-06-11 12:09:27 +02:00
e.preventDefault()
let tab = new Array()
tab["title"] = this.state.text
tab["done"] = false
this.state.items.push(tab)
this.$("form input").value = ""
this.state.text = ""
this.update()
2022-06-07 13:34:26 +02:00
},
toggle(item) {
item.done = !item.done
// trigger a component update
this.update()
2022-06-11 12:09:27 +02:00
},
filtre(type) {
console.log(type + " and " + this.state.search)
this.update({
search: type
})
2022-06-07 13:34:26 +02:00
}
}
</script>
</todo>