-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample2-react.js
112 lines (106 loc) · 2.58 KB
/
example2-react.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
var redux = require('../')
var react = require('react')
var toString = require('react-dom/server').renderToString;
var reactdom = require('react-dom');
var hyperx = require('hyperx')
var hx = hyperx(react.createElement)
var todo = function(state,action) {
switch (action.type) {
case 'ADD_TODO' :
return {
id: action.id,
text: action.text,
completed:false
}
break;
case 'TOGGLE_TODO' :
if (state.id !== action.id) {
return state
}
return {
type: state.type,
id : state.id,
text: state.text,
completed: !state.completed
}
break;
default:
return state
break;
}
}
var todos = function(state, action) {
if (state === undefined)
state = []
switch (action.type) {
case 'ADD_TODO':
return state.concat(todo(undefined,action))
break;
case 'TOGGLE_TODO' :
return state.map(function(t) {
return todo(t,action)
})
break;
default:
return state;
}
}
var visibilityFilter = function(state, action) {
if (state === undefined)
state = 'SHOW_ALL'
switch (action.type) {
case 'SET_VISIBILITY_FILTER':
return action.filter;
break;
default:
return state
}
}
var combineReducers = redux.combineReducers;
var todoApp = combineReducers({
todos:todos,
visibilityFilter:visibilityFilter
});
var createStore = redux.createStore;
var store = createStore(todoApp)
var Component = react.Component;
var nextTodoId = 0;
var myinput;
var TodoApp = function() {
Component.call(this);
this.render = function() {
return hx`<div><div>
<input ref=${function(node) {
myinput = node
}} />
<button onClick=${function() {
store.dispatch({
type:'ADD_TODO',
text:myinput.value,
id:nextTodoId++
});
myinput.value = '';
}}> Add Todo</button>
</div>
<ul>
${this.props.todos.map(function(todo) {
return hx`<li key=${todo.id} onClick=${function() {
store.dispatch({
type:'TOGGLE_TODO',
id:todo.id
})
}} style=${{textDecoration:todo.completed ? 'line-through':'none'}}>${todo.text}</li>`
})}
</ul>
</div>`
}
}
TodoApp.prototype = Object.create(Component.prototype);
TodoApp.prototype.constructor = Component;
var Factory = react.createFactory(TodoApp)
var render = function() {
var root = Factory({todos:store.getState().todos})
reactdom.render(root, document.querySelector('#content'))
}
store.subscribe(render)
render()