-
Notifications
You must be signed in to change notification settings - Fork 0
/
react.html
132 lines (115 loc) · 3.03 KB
/
react.html
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
<!doctype html>
<title>React FP Test</title>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/react/0.12.2/react.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/react/0.12.2/JSXTransformer.js"></script>
<div id="content"></div>
<script type="text/jsx">
var users = [
{
fname: 'John',
lname: 'Doe',
birthdate: 951363573000,
},
{
fname: 'Jeff',
lname: 'Johnson',
birthdate: 701408373000,
},
{
fname: 'Jane',
lname: 'Smith',
birthdate: 572672373000,
},
{
fname: 'Julie',
lname: 'Rogers',
birthdate: 385789173000,
},
{
fname: 'Jackson',
lname: 'Nelson',
birthdate: 7097973000,
},
]
function UserViewModel(user) {
this.birthday = new Date()
this.birthday.setTime(user.birthdate)
this.name = user.fname + ' ' + user.lname
this.user = user
}
function filterUsers(filter) {
return users
.map(function(user) {
return new UserViewModel(user)
})
.filter(function(uvm) {
return !filter || uvm.name.toLowerCase().indexOf(filter) !== -1
})
}
var RxUserNode = React.createClass({
getInitialState: function() {
return { editing: false }
},
render: function() {
if (this.state.editing) {
return (
<div><input onChange={this.handleChange} defaultValue={this.props.user.name} ref="editValue" /> <a href='#' onClick={this.stopEditing}>done</a></div>
)
}
return (
<div>{this.props.user.name} – {this.props.user.birthday} <a href='#' onClick={this.startEditing}>edit</a></div>
)
},
stopEditing: function() {
this.setState({ editing: false })
},
startEditing: function() {
this.setState({ editing: true })
},
handleChange: function() {
//
// On the server:
//
var parts = this.refs.editValue.getDOMNode().value.split(' ')
var user = this.props.user.user
user.fname = parts[0] || ''
user.lname = parts[1] || ''
user.fname = user.fname.substr(0,1).toUpperCase() + user.fname.substr(1)
user.lname = user.lname.substr(0,1).toUpperCase() + user.lname.substr(1)
//
// End server
//
userUpdated()
},
})
var userUpdated
var RxUserList = React.createClass({
getInitialState: function() {
return { users: filterUsers() }
},
render: function() {
var me = this
userUpdated = function() {
me.setState({ users: filterUsers(me.refs.filter.getDOMNode().value) })
}
var userNodes = this.state.users.map(function(user) {
return (
<RxUserNode user={user} />
)
})
return (
<div>
<input onChange={this.handleChange} ref="filter" />
{userNodes}
</div>
)
},
handleChange: function() {
return userUpdated()
},
})
React.render(
<RxUserList />,
document.getElementById('content')
)
</script>