forked from VoliJS/NestedLink
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuserslist.jsx
163 lines (133 loc) · 4.25 KB
/
userslist.jsx
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import './main.css'
import ReactDOM from 'react-dom'
import React, {PropTypes} from 'react'
import Link from 'valuelink'
import Modal from 'react-modal'
import {Input, isRequired, isEmail } from 'tags'
export const UsersList = React.createClass( {
getInitialState(){
return {
users : [],
dialog : null,
editing : null
}
},
render(){
const usersLink = Link.state( this, 'users' ),
{ dialog, editing } = this.state;
return (
<div>
<button onClick={ () => this.openDialog( 'addUser' ) }>
Add User
</button>
<Header/>
{ usersLink.map( ( userLink, i ) => (
<UserRow key={ i }
userLink={ userLink }
onEdit={ () => this.openDialog( 'editUser', i ) }
/>
) )}
<Modal isOpen={ dialog === 'addUser' }>
<EditUser userLink={ Link.value( {}, x => usersLink.push( x ) ) }
onClose={ this.closeDialog }/>
</Modal>
<Modal isOpen={ dialog === 'editUser' }>
<EditUser userLink={ usersLink.at( editing ) }
onClose={ this.closeDialog }/>
</Modal>
</div>
);
},
closeDialog(){
this.setState( { dialog : null } );
},
openDialog( name, editing = null ){
this.setState( { dialog : name, editing : editing } );
}
} );
const Header = () =>(
<div className="users-row">
<div>Name</div>
<div>Email</div>
<div>Is Active</div>
<div/>
</div>
);
const UserRow = ( { userLink, onEdit } ) =>{
const isActiveLink = userLink.at( 'isActive' ),
user = userLink.value;
return (
<div className="users-row" onDoubleClick={ onEdit }>
<div>{ user.name }</div>
<div>{ user.email }</div>
<div onClick={ isActiveLink.action( x => !x ) }>
{ user.isActive ? 'Yes' : 'No' }</div>
<div>
<button onClick={ onEdit }>Edit</button>
<button onClick={ () => userLink.remove() }>X</button>
</div>
</div>
)
};
const EditUser = React.createClass( {
propTypes : {
userLink : PropTypes.instanceOf( Link ).isRequired,
onClose : PropTypes.func.isRequired
},
getInitialState(){
return {
name : '',
email : '',
isActive : true
}
},
componentWillMount(){
this.setState( this.props.userLink.value );
},
onSubmit( e ){
e.preventDefault();
const { userLink, onClose } = this.props;
userLink.set( this.state );
onClose();
},
onCancel(){
this.props.onClose();
},
render(){
const linked = Link.all( this, 'name', 'email', 'isActive' );
linked.name
.check( isRequired )
.check( x => x.indexOf( ' ' ) < 0, 'Spaces are not allowed' );
linked.email
.check( isRequired )
.check( isEmail );
return (
<form onSubmit={ this.onSubmit }>
<label>
Name: <ValidatedInput type="text" valueLink={ linked.name }/>
</label>
<label>
Email: <ValidatedInput type="text" valueLink={ linked.email }/>
</label>
<label>
Is active: <Input type="checkbox" checkedLink={ linked.isActive }/>
</label>
<button type="submit" disabled={ linked.name.error || linked.email.error }>
Save
</button>
<button type="button" onClick={ this.onCancel }>
Cancel
</button>
</form>
);
}
} );
const ValidatedInput = ( props ) => (
<div>
<Input { ...props } />
<div className="validation-error">
{ props.valueLink.error || '' }
</div>
</div>
);
ReactDOM.render( <UsersList />, document.getElementById( 'app-mount-root' ) );