1
0
mirror of https://github.com/morris/vanilla-todo.git synced 2025-08-22 13:43:06 +02:00

refactor state

This commit is contained in:
Morris Brodersen
2023-11-26 11:54:04 +01:00
parent 9343da1693
commit 6a640515b2
12 changed files with 209 additions and 203 deletions

View File

@@ -6,9 +6,7 @@ import { TodoItemInput } from './TodoItemInput.js';
* @param {HTMLElement} el
*/
export function TodoList(el) {
const state = {
items: [],
};
let items = [];
el.innerHTML = `
<div class="items"></div>
@@ -30,18 +28,19 @@ export function TodoList(el) {
),
);
el.addEventListener('todoItems', (e) => update({ items: e.detail }));
function update(next) {
Object.assign(state, next);
el.addEventListener('todoItems', (e) => {
items = e.detail;
update();
});
function update() {
const container = el.querySelector('.items');
const obsolete = new Set(container.children);
const childrenByKey = new Map();
obsolete.forEach((child) => childrenByKey.set(child.dataset.key, child));
const children = state.items.map((item) => {
const children = items.map((item) => {
let child = childrenByKey.get(item.id);
if (child) {