I have a simple check list with a delete button for each item. When I check the first item and then delete it, the list updates, deleting the item, but the check box of the next item is checked. The properties of the next item are correct.
Here's my code:
import { LitElement, html } from 'lit-element';
class CheckList extends LitElement {
static get properties() {
return {
items: { type: Array },
};
}
constructor() {
super();
this.items = [
{
id: 1,
text: 'Item 1',
isDone: false,
},
{
id: 2,
text: 'Item 2',
isDone: false,
},
];
this.toggleCheck = this.toggleCheck.bind(this);
this.deleteItem = this.deleteItem.bind(this);
}
render() {
return html`
<ul>
${this.items.map(item => html`
<li>
<input
type="checkbox"
value=${item.id}
?checked=${item.isDone}
@click=${this.toggleCheck}
>
${item.text}
<button @click=${this.deleteItem}>X</button>
</li>
`)}
</ul>
`;
}
toggleCheck(e) {
const id = Number(e.target.value);
this.items = this.items.map(item => {
if (item.id === id) {
item.isDone = !item.isDone;
}
return item;
});
}
deleteItem(e) {
const id = Number(e.target.parentNode.querySelector('input').value);
this.items = this.items.filter(item => item.id !== id);
}
}
customElements.define('check-list', CheckList);