-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmain.jsx
158 lines (126 loc) · 4.29 KB
/
main.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
import './main.css'
import ReactDOM from 'react-dom'
import React, { Link } from 'react-mvx'
import { Record, define } from 'type-r'
import { localStorageIO } from 'type-r/endpoints/localStorage'
import Modal from 'react-modal'
const Email = String.has.check( x => !x || x.indexOf( '@' ) >= 0, 'Must be valid e-mail' );
@define class User extends Record {
static attributes = {
name : String.isRequired
.has.check( x => x.indexOf( ' ' ) < 0, 'Spaces are not allowed' ),
email : Email.isRequired,
isActive : true
}
remove(){ this.collection.remove( this ); }
}
@define class AppState extends Record {
static endpoint = localStorageIO( '/react-mvx/examples' );
static attributes = {
id : 'users-list',
users : User.Collection, // No comments required, isn't it?
editing : User.from( 'users' ), // User from user collection, which is being edited.
adding : User.shared.value( null ) // New user, which is being added.
}
}
@define
export class UsersList extends React.Component {
static State = AppState;
componentWillMount(){
this.state.fetch();
window.onunload = () => this.state.save();
}
render(){
const { state } = this;
return (
<div>
<button onClick={ () => state.adding = new User() }>
Add User
</button>
<Header/>
{ state.users.map( user => (
<UserRow key={ user.cid }
user={ user }
onEdit={ () => state.editing = user }
/>
) )}
<Modal isOpen={ Boolean( state.adding ) }>
<EditUser userLink={ state.linkAt( 'adding' ) }
onSave={ () => state.users.add( state.adding ) }/>
</Modal>
<Modal isOpen={ Boolean( state.editing ) }>
<EditUser userLink={ state.linkAt( 'editing' ) } />
</Modal>
</div>
);
}
}
const Header = () =>(
<div className="users-row">
<div>Name</div>
<div>Email</div>
<div>Is Active</div>
<div/>
</div>
);
const UserRow = ( { user, onEdit } ) =>(
<div className="users-row" onDoubleClick={ onEdit }>
<div>{ user.name }</div>
<div>{ user.email }</div>
<div onClick={ () => user.isActive = !user.isActive }>
{ user.isActive ? 'Yes' : 'No' }</div>
<div>
<button onClick={ onEdit }>Edit</button>
<button onClick={ () => user.remove() }>X</button>
</div>
</div>
);
@define class EditUser extends React.Component {
static props = {
userLink : Link.has.watcher( React.assignToState( 'user' ) ),
onSave : Function
};
static state = {
user : User
};
onSubmit = e => {
e.preventDefault();
const { userLink, onSave } = this.props;
userLink.value.assignFrom( this.state.user );
onSave && onSave( userLink.value );
this.onCancel()
}
onCancel = () => this.props.userLink.set( null );
render(){
const { user } = this.state,
linked = user.linkAll();
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" { ...linked.isActive.props }/>
</label>
<button type="submit" disabled={ !user.isValid() }>
Save
</button>
<button type="button" onClick={ this.onCancel }>
Cancel
</button>
</form>
);
}
}
const ValidatedInput = ({ valueLink, ...props }) => (
<div>
<input {...valueLink.props} { ...props } />
<div className="validation-error">
{ valueLink.error || '' }
</div>
</div>
);
ReactDOM.render( <UsersList />, document.getElementById( 'app-mount-root' ) );