-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest.js
130 lines (118 loc) · 3.65 KB
/
test.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
'use strict';
var React = require('react');
var $ = React.DOM;
var ou = require('plexus-objective');
var fields = require('./fields');
var normalise = require('./normalise');
module.exports = React.createClass({
displayName: 'Form',
getInitialState: function() {
var values = this.props.values;
var errors = this.validate(this.props.schema, values, context(this.props));
return { values: values,
output: values,
errors: errors };
},
componentWillReceiveProps: function(props) {
var values = props.values || this.state.values;
var output = props.values || this.state.output;
this.setState({
values: values,
output: output,
errors: this.validate(props.schema, output, context(props))
});
},
setValue: function(path, raw, parsed) {
var schema = this.props.schema;
var ctx = context(this.props);
var values = normalise(ou.setIn(this.state.values, path, raw),
schema, ctx);
var output = normalise(ou.setIn(this.state.output, path, parsed),
schema, ctx);
var errors = this.validate(schema, output, ctx);
if (this.props.submitOnChange) {
this.props.onSubmit(output, null, errors);
}
else {
this.setState({
values: values,
output: output,
errors: errors
});
}
},
getValue: function(path) {
return ou.getIn(this.state.values, path);
},
getErrors: function(path) {
return this.state.errors[makeKey(path)];
},
validate: function(schema, values, context) {
return hashedErrors(this.props.validate(schema, values, context));
},
preventSubmit: function(event) {
event.preventDefault();
},
handleSubmit: function(event) {
this.props.onSubmit(this.state.output,
event.target.value,
this.state.errors);
},
handleKeyPress: function(event) {
if (event.keyCode === 13 && this.props.enterKeySubmits) {
this.props.onSubmit(this.state.output, this.props.enterKeySubmits);
}
},
renderButtons: function() {
var submit = this.handleSubmit;
if (typeof this.props.buttons === 'function') {
return this.props.buttons(submit);
}
else {
var buttons = (this.props.buttons || ['Cancel', 'Submit'])
.map(function(value) {
return $.input({ type : 'submit',
key : value,
value : value,
onClick: submit });
});
return $.p(null, buttons);
}
},
render: function() {
var renderedFields = fields.make(fields, {
schema : this.props.schema,
context : context(this.props),
fieldWrapper : this.props.fieldWrapper,
sectionWrapper: this.props.sectionWrapper,
handlers : this.props.handlers,
hints : this.props.hints,
path : [],
update : this.setValue,
getValue : this.getValue,
getErrors : this.getErrors
});
return $.form({ onSubmit : this.preventSubmit,
onKeyPress: this.handleKeyPress,
className : this.props.className
},
this.props.extraButtons ? this.renderButtons() : $.span(),
renderedFields,
this.renderButtons());
}
});
function hashedErrors(errors) {
var result = {};
var i, entry;
for (i = 0; i < errors.length; ++i) {
entry = errors[i];
result[makeKey(entry.path)] = entry.errors;
}
return result;
}
function makeKey(path) {
return path.join('_');
}
function context(props) {
return props.context || props.schema;
}