Created
January 6, 2017 11:23
-
-
Save bendc/fd121fd3daa60a5d7dd0eaecb50fa754 to your computer and use it in GitHub Desktop.
Web Components: todo-list-events
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const store = (() => { | |
let state; | |
return todos => { | |
if (todos) { | |
state = todos; | |
render("todo-list"); | |
} | |
return state; | |
}; | |
})(); | |
const render = (component, parent = document.body) => | |
parent.innerHTML = `<${component}></${component}>`; | |
customElements.define("todo-item", class extends HTMLElement { | |
constructor() { | |
super(); | |
const root = this.attachShadow({mode: "open"}); | |
root.innerHTML = ` | |
<label> | |
<input type=checkbox> | |
<slot></slot> | |
</label> | |
`; | |
root.querySelector("input").addEventListener("click", () => | |
store(store().filter((todo, index) => index != this.index))); | |
} | |
static get observedAttributes() { | |
return ["index"]; | |
} | |
attributeChangedCallback(name, old, change) { | |
this.index = change; | |
} | |
}); | |
customElements.define("todo-list", class extends HTMLElement { | |
constructor() { | |
super(); | |
const root = this.attachShadow({mode: "open"}); | |
root.innerHTML = ` | |
<ul> | |
${store().map((todo, index) => ` | |
<li> | |
<todo-item index=${index}> | |
${todo} | |
</todo-item> | |
</li> | |
`).join("")} | |
</ul> | |
`; | |
} | |
}); | |
store(["Buy milk", "Call Sarah", "Pay bills"]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment