-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCreateForm.js
More file actions
149 lines (121 loc) · 3.69 KB
/
CreateForm.js
File metadata and controls
149 lines (121 loc) · 3.69 KB
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
var _ = require('underscore'),
React = require('react'),
Fields = require('FieldTypes'),
InvalidFieldType = require('./InvalidFieldType');
var Form = React.createClass({
displayName: 'CreateForm',
getDefaultProps: function() {
return {
err: null,
values: {},
animate: false
};
},
getInitialState: function() {
var values = this.props.values;
_.each(this.props.list.fields, function(field) {
if (!values[field.path]) {
values[field.path] = field.defaultValue;
}
});
return {
values: values
};
},
handleChange: function(event) {
var values = this.state.values;
values[event.path] = event.value;
this.setState({
values: values
});
},
componentWillMount: function() {
this._bodyStyleOverflow = document.body.style.overflow;
document.body.style.overflow = 'hidden';
},
componentDidMount: function() {
if (this.refs.focusTarget) {
this.refs.focusTarget.focus();
}
},
componentWillUnmount: function() {
document.body.style.overflow = this._bodyStyleOverflow;
},
getFieldProps: function(field) {
var props = _.clone(field);
props.value = this.state.values[field.path];
props.values = this.state.values;
props.onChange = this.handleChange;
props.mode = 'create';
return props;
},
render: function() {
var errors = null,
form = {},
list = this.props.list,
formAction = '/keystone/' + list.path,
nameField = this.props.list.nameField,
focusRef;
var modalClass = 'modal modal-md' + (this.props.animate ? ' animate' : '');
if (this.props.err && this.props.err.errors) {
var msgs = {};
_.each(this.props.err.errors, function(err, path) {
msgs[path] = <li>{err.message}</li>;
});
errors = (
<div className="alert alert-danger">
<h4>There was an error creating the new {list.singular}:</h4>
<ul>{msgs}</ul>
</div>
);
}
if (list.nameIsInitial) {
var nameFieldProps = this.getFieldProps(nameField);
nameFieldProps.ref = focusRef = 'focusTarget';
if (nameField.type === 'text') {
nameFieldProps.className = 'item-name-field';
nameFieldProps.placeholder = nameField.label;
nameFieldProps.label = false;
}
form[nameField.path] = React.createElement(Fields[nameField.type], nameFieldProps);
}
_.each(list.initialFields, function(path) {
var field = list.fields[path];
if ('function' !== typeof Fields[field.type]) {
form[field.path] = React.createElement(InvalidFieldType, { type: field.type, path: field.path });
return;
}
var fieldProps = this.getFieldProps(field);
if (!focusRef) {
fieldProps.ref = focusRef = 'focusTarget';
}
form[field.path] = React.createElement(Fields[field.type], fieldProps);
}, this);
return (
<div>
<div className={modalClass}>
<div className="modal-dialog">
<form className="modal-content" encType="multipart/form-data" method="post" action={formAction}>
<input type="hidden" name="action" value="create" />
<input type="hidden" name={Keystone.csrf.key} value={Keystone.csrf.value} />
<div className="modal-header">
<button type="button" className="modal-close" onClick={this.props.onCancel}></button>
<div className="modal-title">Create a new {list.singular}</div>
</div>
<div className="modal-body">
{errors}
{form}
</div>
<div className="modal-footer">
<button type="submit" className="btn btn-create">Create</button>
<button type="button" className="btn btn-link btn-cancel" onClick={this.props.onCancel}>cancel</button>
</div>
</form>
</div>
</div>
<div className="modal-backdrop"></div>
</div>
);
}
});
module.exports = Form;