JSFiddle - React, Tailwind, and code Playground
by toddmotto
HTML
<div>
<todos></todos>
</div>
CSS
</style>
<script src="https://code.angularjs.org/1.5.8/angular.min.js"></script>
<style>
.complete {
text-decoration: line-through;
}
Babel + JSX
const todos = {
template: `
<div>
<todo-form
new-todo="$ctrl.newTodo"
on-add="$ctrl.addTodo($event);">
</todo-form>
<todo-list
todos="$ctrl.todos"
on-complete="$ctrl.completeTodo($event);"
on-delete="$ctrl.removeTodo($event);">
</todo-list>
</div>
`,
controller: class TodoController {
constructor(TodoService) {
this.todoService = TodoService;
}
$onInit() {
this.todos = this.todoService.getTodos();
}
addTodo({ label }) {
this.todos = [{ label, id: this.todos.length + 1 }, ...this.todos];
}
completeTodo({ todo }) {
this.todos = this.todos.map(
item => item.id === todo.id ? Object.assign({}, item, { complete: true }) : item
);
}
removeTodo({ todo }) {
this.todos = this.todos.filter(({ id }) => id !== todo.id);
}
}
};
const todoForm = {
bindings: {
onAdd: '&'
},
template: `
<form ng-submit="$ctrl.submit();">
<input ng-model="$ctrl.label">
<button type="submit">Add todo</button>
</form>
`,
controller: class TodoFormController {
constructor() {}
submit(label) {
if (!this.label) return;
this.onAdd({
$event: { label: this.label }
});
this.label = '';
};
}
};
const todoList = {
bindings: {
todos: '<',
onComplete: '&',
onDelete: '&'
},
template: `
<ul>
<li ng-repeat="todo in $ctrl.todos">
<todo
item="todo"
on-change="$ctrl.onComplete($locals);"
on-remove="$ctrl.onDelete($locals);">
</todo>
</li>
</ul>
`
};
const todo = {
bindings: {
item: '<',
onChange: '&',
onRemove: '&'
},
template: `
<div>
<span ng-class="{ complete: $ctrl.item.complete }">{{ $ctrl.item.label }}</span>
<button
type="button"
ng-click="$ctrl.onChange({ $event: { todo: $ctrl.item } });">Done</button>
<button
type="button"
...