Skip to content

Instantly share code, notes, and snippets.

@jwx
Last active June 10, 2022 22:07
Show Gist options
  • Save jwx/9b4fb4b8e818296633e46a286cf8d465 to your computer and use it in GitHub Desktop.
Save jwx/9b4fb4b8e818296633e46a286cf8d465 to your computer and use it in GitHub Desktop.
peasy-ui-demo-todo
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Dumber Gist</title>
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1.0, user-scalable=no">
</head>
<!--
Dumber Gist uses dumber bundler, the default bundle file
is /dist/entry-bundle.js.
The starting module is pointed to "main" (data-main attribute on script)
which is your src/main.ts.
-->
<body>
<script src="/dist/entry-bundle.js" data-main="main"></script>
</body>
</html>
{
"dependencies": {
"peasy-ui": "latest"
}
}
import { UI } from 'peasy-ui';
import 'styles.css';
window.addEventListener('DOMContentLoaded', (event) => {
main();
});
function main(): void {
const model = {
todos: [],
get remainingTodos() { return model.todos.filter(todo => !todo.done) },
get doneTodos() { return model.todos.filter(todo => todo.done) },
get hasRemaining() { return model.remainingTodos.length > 0; },
get hasDone() { return model.doneTodos.length > 0; },
addTodo: (_event, model) => {
model.todos.push({ text: model.todo, done: false });
model.todo = '';
model.inputElement.focus();
},
removeTodo: (_event, model, _element, _at, context) => {
context.$parent.$model.todos = context.$parent.$model.todos.filter(todo => todo !== model.todo);
},
};
const templateTodo = `
<div class="todos">
<h2>Todo list</h2>
<div class="input"><form onsubmit="return false;"><input \${value <=> todo} \${==> inputElement}> <button type="submit" \${click @=> addTodo}>Add todo</button></form></div>
<div class="header" \${ === hasRemaining}>Remaining</div>
<div class="todo remaining-todo" \${todo <=* remainingTodos}"><label><input type="checkbox" \${checked <=> todo.done}> \${todo.text}</label> <button \${click @=> removeTodo}>Remove todo</button></div>
<div class="header" \${ === hasDone}>Done</div>
<div class="todo done-todo" \${todo <=* doneTodos}><label><input type="checkbox" \${checked <=> todo.done}> \${todo.text}</label> <button \${click @=> removeTodo}>Remove todo</button></div>
</div>
`;
UI.create(document.body, templateTodo, model);
// Event loop (very simplified)
setInterval(() => UI.update(), 1000 / 30);
}
input {
padding: 3px;
border-radius: 3px;
}
.main {
margin: 10px;
padding: 10px;
}
.todos {
margin: 10px;
}
.todos .input {
margin-bottom: 10px;
}
.todo {
padding: 5px 0px;
}
.remaining-todo {
background-color: lightgreen;
}
.done-todo {
background-color: darkgreen;
text-decoration: line-through;
}
.todo button {
float: right;
margin-right: 1vw;
}
.todos .header {
margin-top: 10px;
margin-left: 3px;
font-weight: bold;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment