-
Notifications
You must be signed in to change notification settings - Fork 62
/
TodoItem.js
94 lines (88 loc) · 2.92 KB
/
TodoItem.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import {Observable} from 'rx';
import {button, div, input, label, li} from '@cycle/dom';
import {propHook, ENTER_KEY, ESC_KEY} from '../utils';
// THE TODO ITEM INTENT
// This intent function returns a stream of all the different,
// actions that can be taken on a todo.
function intent(DOM) {
// THE INTENT MERGE
// Merge all actions into one stream.
return Observable.merge(
// THE DESTROY ACTION STREAM
DOM.select('.destroy').events('click').map(() => ({type: 'destroy'})),
// THE TOGGLE ACTION STREAM
DOM.select('.toggle').events('change').map(() => ({type: 'toggle'})),
// THE START EDIT ACTION STREAM
DOM.select('label').events('dblclick').map(() => ({type: 'startEdit'})),
// THE ESC KEY ACTION STREAM
DOM.select('.edit').events('keyup')
.filter(ev => ev.keyCode === ESC_KEY)
.map(() => ({type: 'cancelEdit'})),
// THE ENTER KEY ACTION STREAM
DOM.select('.edit').events('keyup')
.filter(ev => ev.keyCode === ENTER_KEY)
.merge(DOM.select('.edit').events('blur', true))
.map(ev => ({title: ev.target.value, type: 'doneEdit'}))
)
// MAKE THIS OBSERVABLE HOT
// By sharing it will act as a hot Observable.
.share();
}
function model(props$, action$) {
// THE SANITIZED PROPERTIES
// If the list item has no data set it as empty and not completed.
const sanitizedProps$ = props$.startWith({title: '', completed: false});
// THE EDITING STREAM
// Create a stream that emits booleans that represent the
// "is editing" state.
const editing$ = Observable
.merge(
action$.filter(a => a.type === 'startEdit').map(() => true),
action$.filter(a => a.type === 'doneEdit').map(() => false),
action$.filter(a => a.type === 'cancelEdit').map(() => false)
)
.startWith(false);
return Observable.combineLatest(
sanitizedProps$, editing$,
({title, completed}, editing) => ({title, completed, editing})
);
}
function view(state$) {
return state$.map(({title, completed, editing}) => {
const completedClass = (completed ? '.completed' : '');
const editingClass = (editing ? '.editing' : '');
return li(`.todoRoot${completedClass}${editingClass}`, [
div('.view', [
input('.toggle', {
type: 'checkbox',
checked: propHook(elem => elem.checked = completed)
}),
label(title),
button('.destroy')
]),
input('.edit', {
type: 'text',
value: propHook(element => {
element.value = title;
if (editing) {
element.focus();
element.selectionStart = element.value.length;
}
})
})
]);
});
}
// THE TODO ITEM FUNCTION
// This is a simple todo item component,
// structured with the MVI-pattern.
function TodoItem({DOM, props$}) {
const action$ = intent(DOM);
const state$ = model(props$, action$);
const vtree$ = view(state$);
return {
DOM: vtree$,
action$,
};
}
export default TodoItem;