Skip to content

Instantly share code, notes, and snippets.

@developit
Last active March 10, 2016 23:00
Show Gist options
  • Select an option

  • Save developit/9c992087c4133003a716 to your computer and use it in GitHub Desktop.

Select an option

Save developit/9c992087c4133003a716 to your computer and use it in GitHub Desktop.
esnextbin sketch
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>ESNextbin Sketch</title>
<style>
body > div { padding:20px; margin:auto; }
p { display: inline-block; padding:5px; margin:0; }
a { opacity: 0.5; }
li.done p { text-decoration: line-through; }
</style>
</head>
<body>
<!-- put markup and other contents here -->
</body>
</html>
import { h, render } from 'preact-cycle';
/** @jsx h */
const ADD = ({ text, todos, ...state }) => ({
...state,
text: '',
todos: todos.concat({ text })
});
const TOGGLE = (state, todo) => {
todo.done = !todo.done;
return state;
};
const REMOVE = ({ todos, ...state }, todo) => ({
...state,
todos: todos.filter( t => t!==todo )
});
const TodoList = ({ text, todos, mutate, mutation }) => (
<div>
<form onSubmit={ e => { e.preventDefault(), mutate(ADD) }}>
<input value={text} onInput={e => mutate('text', e.target.value)} />
<button action="submit">Add</button>
</form>
<ul>
{ todos.map( todo => (
<li onClick={mutation(TOGGLE, todo)} class={{ done: todo.done }}>
<input type="checkbox" checked={todo.done} readonly />
<p>{ todo.text }</p>
<a onClick={mutation(REMOVE, todo)}>✕</a>
</li>
))}
</ul>
</div>
);
render(TodoList, { todos: [] }, document.body);
{
"name": "esnextbin-sketch",
"version": "0.0.0",
"dependencies": {
"preact-cycle": "0.1.1",
"babel-runtime": "6.6.1"
}
}
'use strict';
var _extends2 = require('babel-runtime/helpers/extends');
var _extends3 = _interopRequireDefault(_extends2);
var _objectWithoutProperties2 = require('babel-runtime/helpers/objectWithoutProperties');
var _objectWithoutProperties3 = _interopRequireDefault(_objectWithoutProperties2);
var _preactCycle = require('preact-cycle');
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
/** @jsx h */
var ADD = function ADD(_ref) {
var text = _ref.text;
var todos = _ref.todos;
var state = (0, _objectWithoutProperties3.default)(_ref, ['text', 'todos']);
return (0, _extends3.default)({}, state, {
text: '',
todos: todos.concat({ text: text })
});
};
var TOGGLE = function TOGGLE(state, todo) {
todo.done = !todo.done;
return state;
};
var REMOVE = function REMOVE(_ref2, todo) {
var todos = _ref2.todos;
var state = (0, _objectWithoutProperties3.default)(_ref2, ['todos']);
return (0, _extends3.default)({}, state, {
todos: todos.filter(function (t) {
return t !== todo;
})
});
};
var TodoList = function TodoList(_ref3) {
var text = _ref3.text;
var todos = _ref3.todos;
var mutate = _ref3.mutate;
var mutation = _ref3.mutation;
return (0, _preactCycle.h)(
'div',
null,
(0, _preactCycle.h)(
'form',
{ onSubmit: function onSubmit(e) {
e.preventDefault(), mutate(ADD);
} },
(0, _preactCycle.h)('input', { value: text, onInput: function onInput(e) {
return mutate('text', e.target.value);
} }),
(0, _preactCycle.h)(
'button',
{ action: 'submit' },
'Add'
)
),
(0, _preactCycle.h)(
'ul',
null,
todos.map(function (todo) {
return (0, _preactCycle.h)(
'li',
{ onClick: mutation(TOGGLE, todo), 'class': { done: todo.done } },
(0, _preactCycle.h)('input', { type: 'checkbox', checked: todo.done, readonly: true }),
(0, _preactCycle.h)(
'p',
null,
todo.text
),
(0, _preactCycle.h)(
'a',
{ onClick: mutation(REMOVE, todo) },
'✕'
)
);
})
)
);
};
(0, _preactCycle.render)(TodoList, { todos: [] }, document.body);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment