-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathform.jsx
63 lines (51 loc) · 1.63 KB
/
form.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
import './styles.css'
import React, { define } from 'react-mvx'
import ReactDOM from 'react-dom'
import { Record } from 'type-r'
import { localStorageIO } from 'type-r/endpoints/localStorage'
@define class AppState extends Record {
// Persist this class to the local storage.
static endpoint = localStorageIO( '/react-mvx/example' );
// Define state structure
static attributes = {
id : 'form',
name : '',
email : '',
isActive : true
}
}
@define class Application extends React.Component {
static State = AppState;
componentWillMount(){
// Load from the local storage.
this.state.fetch();
}
// Save to the local storage
onSubmit = e => {
e.preventDefault();
this.state.save();
}
onCancel = () => this.state.set( this.state.defaults() );
render(){
// Link the state...
const { name, email, isActive } = this.linkAll();
return (
<form onSubmit={ this.onSubmit }>
<label>
Name: <input type="text" { ...name.props }/>
</label>
<label>
Email: <input type="text" { ...email.props }/>
</label>
<label>
Is active: <input type="checkbox" { ...isActive.props }/>
</label>
<button type="submit">Save</button>
<button type="button" onClick={ this.onCancel }>
Clear
</button>
</form>
);
}
}
ReactDOM.render( <Application/>, document.getElementById( 'react-application' ) );