A working version but using non-legacy decorators should be reconsidered, and probably quite a few bugs left in the render loop

This commit is contained in:
2019-11-03 15:35:19 +01:00
parent fc527cb156
commit 3a135a30d1
18 changed files with 599 additions and 257 deletions

View File

@@ -7,11 +7,20 @@ import {TodoItem} from './todo-item';
@defineElement('my-todo')
export class MyTodo extends CustomElement{
uid = 1;
// @State Won't work;
@State() todos = [
{id: this.uid++, text: "my initial todo", checked: false },
{id: this.uid++, text: "Learn about Web Components", checked: false },
];
@State() todos;
// = [
// {id: this.uid++, text: "my initial todo", checked: false },
// {id: this.uid++, text: "Learn about Web Components", checked: false },
// ];
constructor(){
super();
this.uid = 1;
this.todos = [
{id: this.uid++, text: "my initial todo", checked: false },
{id: this.uid++, text: "Learn about Web Components", checked: false },
]
}
render(){
return (
@@ -27,7 +36,8 @@ export class MyTodo extends CustomElement{
{this.todos.map(item =>
<todo-item
model={ item.id }
checked={( item.checked )}
text={item.text}
checked={ item.checked }
>
{ item.text }
</todo-item>
@@ -44,12 +54,14 @@ export class MyTodo extends CustomElement{
this.todos = [...this.todos, { id: this.uid++, text, checked: false }];
}
};
handleCheck = ({detail: checked}, id) => {
handleCheck = ({detail: {checked,id}}) => {
let indexOf = this.todos.findIndex(t=>t.id===id);
let updated = {...this.todos[indexOf], checked};
this.todos = [...this.todos.slice(0,indexOf), updated, ...this.todos.slice(indexOf+1)];
if(indexOf>=0) {
let updated = { ...this.todos[ indexOf ], checked };
this.todos = [...this.todos.slice(0, indexOf), updated, ...this.todos.slice(indexOf + 1)];
}
};
handleRemove = (e,id)=>{
handleRemove = ({detail: {id}})=>{
let indexOf = this.todos.findIndex(t=>t.id===id);
this.todos = [...this.todos.slice(0,indexOf), ...this.todos.slice(indexOf+1)];
}