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:
@@ -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)];
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user