1 | "use strict";
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 |
|
7 | var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
|
8 |
|
9 | var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
|
10 |
|
11 | var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
|
12 |
|
13 | var _react = require("react");
|
14 |
|
15 | var _react2 = _interopRequireDefault(_react);
|
16 |
|
17 | var _propTypes = require("prop-types");
|
18 |
|
19 | var _propTypes2 = _interopRequireDefault(_propTypes);
|
20 |
|
21 | var _ErrorList = require("./ErrorList");
|
22 |
|
23 | var _ErrorList2 = _interopRequireDefault(_ErrorList);
|
24 |
|
25 | var _utils = require("../utils");
|
26 |
|
27 | var _validate = require("../validate");
|
28 |
|
29 | var _validate2 = _interopRequireDefault(_validate);
|
30 |
|
31 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
32 |
|
33 | function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
34 |
|
35 | function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
|
36 |
|
37 | function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
|
38 |
|
39 | var Form = function (_Component) {
|
40 | _inherits(Form, _Component);
|
41 |
|
42 | function Form(props) {
|
43 | _classCallCheck(this, Form);
|
44 |
|
45 | var _this = _possibleConstructorReturn(this, (Form.__proto__ || Object.getPrototypeOf(Form)).call(this, props));
|
46 |
|
47 | _this.onChange = function (formData, newErrorSchema) {
|
48 | var mustValidate = !_this.props.noValidate && _this.props.liveValidate;
|
49 | var state = { formData: formData };
|
50 | if (mustValidate) {
|
51 | var _this$validate = _this.validate(formData),
|
52 | errors = _this$validate.errors,
|
53 | errorSchema = _this$validate.errorSchema;
|
54 |
|
55 | state = _extends({}, state, { errors: errors, errorSchema: errorSchema });
|
56 | } else if (!_this.props.noValidate && newErrorSchema) {
|
57 | state = _extends({}, state, {
|
58 | errorSchema: newErrorSchema,
|
59 | errors: (0, _validate.toErrorList)(newErrorSchema)
|
60 | });
|
61 | }
|
62 | (0, _utils.setState)(_this, state, function () {
|
63 | if (_this.props.onChange) {
|
64 | _this.props.onChange(_this.state);
|
65 | }
|
66 | });
|
67 | };
|
68 |
|
69 | _this.onBlur = function () {
|
70 | if (_this.props.onBlur) {
|
71 | var _this$props;
|
72 |
|
73 | (_this$props = _this.props).onBlur.apply(_this$props, arguments);
|
74 | }
|
75 | };
|
76 |
|
77 | _this.onFocus = function () {
|
78 | if (_this.props.onFocus) {
|
79 | var _this$props2;
|
80 |
|
81 | (_this$props2 = _this.props).onFocus.apply(_this$props2, arguments);
|
82 | }
|
83 | };
|
84 |
|
85 | _this.onSubmit = function (event) {
|
86 | event.preventDefault();
|
87 |
|
88 | if (!_this.props.noValidate) {
|
89 | var _ret = function () {
|
90 | var _this$validate2 = _this.validate(_this.state.formData),
|
91 | errors = _this$validate2.errors,
|
92 | errorSchema = _this$validate2.errorSchema;
|
93 |
|
94 | if (Object.keys(errors).length > 0) {
|
95 | (0, _utils.setState)(_this, { errors: errors, errorSchema: errorSchema }, function () {
|
96 | if (_this.props.onError) {
|
97 | _this.props.onError(errors);
|
98 | } else {
|
99 | console.error("Form validation failed", errors);
|
100 | }
|
101 | });
|
102 | return {
|
103 | v: void 0
|
104 | };
|
105 | }
|
106 | }();
|
107 |
|
108 | if ((typeof _ret === "undefined" ? "undefined" : _typeof(_ret)) === "object") return _ret.v;
|
109 | }
|
110 |
|
111 | if (_this.props.onSubmit) {
|
112 | _this.props.onSubmit(_extends({}, _this.state, { status: "submitted" }));
|
113 | }
|
114 | _this.setState({ errors: [], errorSchema: {} });
|
115 | };
|
116 |
|
117 | _this.state = _this.getStateFromProps(props);
|
118 | return _this;
|
119 | }
|
120 |
|
121 | _createClass(Form, [{
|
122 | key: "componentWillReceiveProps",
|
123 | value: function componentWillReceiveProps(nextProps) {
|
124 | this.setState(this.getStateFromProps(nextProps));
|
125 | }
|
126 | }, {
|
127 | key: "getStateFromProps",
|
128 | value: function getStateFromProps(props) {
|
129 | var state = this.state || {};
|
130 | var schema = "schema" in props ? props.schema : this.props.schema;
|
131 | var uiSchema = "uiSchema" in props ? props.uiSchema : this.props.uiSchema;
|
132 | var edit = typeof props.formData !== "undefined";
|
133 | var liveValidate = props.liveValidate || this.props.liveValidate;
|
134 | var mustValidate = edit && !props.noValidate && liveValidate;
|
135 | var definitions = schema.definitions;
|
136 |
|
137 | var formData = (0, _utils.getDefaultFormState)(schema, props.formData, definitions);
|
138 | var retrievedSchema = (0, _utils.retrieveSchema)(schema, definitions, formData);
|
139 |
|
140 | var _ref = mustValidate ? this.validate(formData, schema) : {
|
141 | errors: state.errors || [],
|
142 | errorSchema: state.errorSchema || {}
|
143 | },
|
144 | errors = _ref.errors,
|
145 | errorSchema = _ref.errorSchema;
|
146 |
|
147 | var idSchema = (0, _utils.toIdSchema)(retrievedSchema, uiSchema["ui:rootFieldId"], definitions, formData, props.idPrefix);
|
148 | return {
|
149 | schema: schema,
|
150 | uiSchema: uiSchema,
|
151 | idSchema: idSchema,
|
152 | formData: formData,
|
153 | edit: edit,
|
154 | errors: errors,
|
155 | errorSchema: errorSchema
|
156 | };
|
157 | }
|
158 | }, {
|
159 | key: "shouldComponentUpdate",
|
160 | value: function shouldComponentUpdate(nextProps, nextState) {
|
161 | return (0, _utils.shouldRender)(this, nextProps, nextState);
|
162 | }
|
163 | }, {
|
164 | key: "validate",
|
165 | value: function validate(formData, schema) {
|
166 | var _props = this.props,
|
167 | validate = _props.validate,
|
168 | transformErrors = _props.transformErrors;
|
169 |
|
170 | return (0, _validate2.default)(formData, schema || this.props.schema, validate, transformErrors);
|
171 | }
|
172 | }, {
|
173 | key: "renderErrors",
|
174 | value: function renderErrors() {
|
175 | var _state = this.state,
|
176 | errors = _state.errors,
|
177 | errorSchema = _state.errorSchema,
|
178 | schema = _state.schema,
|
179 | uiSchema = _state.uiSchema;
|
180 | var _props2 = this.props,
|
181 | ErrorList = _props2.ErrorList,
|
182 | showErrorList = _props2.showErrorList,
|
183 | formContext = _props2.formContext;
|
184 |
|
185 |
|
186 | if (errors.length && showErrorList != false) {
|
187 | return _react2.default.createElement(ErrorList, {
|
188 | errors: errors,
|
189 | errorSchema: errorSchema,
|
190 | schema: schema,
|
191 | uiSchema: uiSchema,
|
192 | formContext: formContext
|
193 | });
|
194 | }
|
195 | return null;
|
196 | }
|
197 | }, {
|
198 | key: "getRegistry",
|
199 | value: function getRegistry() {
|
200 |
|
201 |
|
202 | var _getDefaultRegistry = (0, _utils.getDefaultRegistry)(),
|
203 | fields = _getDefaultRegistry.fields,
|
204 | widgets = _getDefaultRegistry.widgets;
|
205 |
|
206 | return {
|
207 | fields: _extends({}, fields, this.props.fields),
|
208 | widgets: _extends({}, widgets, this.props.widgets),
|
209 | ArrayFieldTemplate: this.props.ArrayFieldTemplate,
|
210 | ObjectFieldTemplate: this.props.ObjectFieldTemplate,
|
211 | FieldTemplate: this.props.FieldTemplate,
|
212 | definitions: this.props.schema.definitions || {},
|
213 | formContext: this.props.formContext || {}
|
214 | };
|
215 | }
|
216 | }, {
|
217 | key: "render",
|
218 | value: function render() {
|
219 | var _props3 = this.props,
|
220 | children = _props3.children,
|
221 | safeRenderCompletion = _props3.safeRenderCompletion,
|
222 | id = _props3.id,
|
223 | className = _props3.className,
|
224 | name = _props3.name,
|
225 | method = _props3.method,
|
226 | target = _props3.target,
|
227 | action = _props3.action,
|
228 | autocomplete = _props3.autocomplete,
|
229 | enctype = _props3.enctype,
|
230 | acceptcharset = _props3.acceptcharset,
|
231 | noHtml5Validate = _props3.noHtml5Validate;
|
232 | var _state2 = this.state,
|
233 | schema = _state2.schema,
|
234 | uiSchema = _state2.uiSchema,
|
235 | formData = _state2.formData,
|
236 | errorSchema = _state2.errorSchema,
|
237 | idSchema = _state2.idSchema;
|
238 |
|
239 | var registry = this.getRegistry();
|
240 | var _SchemaField = registry.fields.SchemaField;
|
241 |
|
242 | return _react2.default.createElement(
|
243 | "form",
|
244 | {
|
245 | className: className ? className : "rjsf",
|
246 | id: id,
|
247 | name: name,
|
248 | method: method,
|
249 | target: target,
|
250 | action: action,
|
251 | autoComplete: autocomplete,
|
252 | encType: enctype,
|
253 | acceptCharset: acceptcharset,
|
254 | noValidate: noHtml5Validate,
|
255 | onSubmit: this.onSubmit },
|
256 | this.renderErrors(),
|
257 | _react2.default.createElement(_SchemaField, {
|
258 | schema: schema,
|
259 | uiSchema: uiSchema,
|
260 | errorSchema: errorSchema,
|
261 | idSchema: idSchema,
|
262 | formData: formData,
|
263 | onChange: this.onChange,
|
264 | onBlur: this.onBlur,
|
265 | onFocus: this.onFocus,
|
266 | registry: registry,
|
267 | safeRenderCompletion: safeRenderCompletion
|
268 | }),
|
269 | children ? children : _react2.default.createElement(
|
270 | "p",
|
271 | null,
|
272 | _react2.default.createElement(
|
273 | "button",
|
274 | { type: "submit", className: "btn btn-info" },
|
275 | "Submit"
|
276 | )
|
277 | )
|
278 | );
|
279 | }
|
280 | }]);
|
281 |
|
282 | return Form;
|
283 | }(_react.Component);
|
284 |
|
285 | Form.defaultProps = {
|
286 | uiSchema: {},
|
287 | noValidate: false,
|
288 | liveValidate: false,
|
289 | safeRenderCompletion: false,
|
290 | noHtml5Validate: false,
|
291 | ErrorList: _ErrorList2.default
|
292 | };
|
293 | exports.default = Form;
|
294 |
|
295 |
|
296 | if (process.env.NODE_ENV !== "production") {
|
297 | Form.propTypes = {
|
298 | schema: _propTypes2.default.object.isRequired,
|
299 | uiSchema: _propTypes2.default.object,
|
300 | formData: _propTypes2.default.any,
|
301 | widgets: _propTypes2.default.objectOf(_propTypes2.default.oneOfType([_propTypes2.default.func, _propTypes2.default.object])),
|
302 | fields: _propTypes2.default.objectOf(_propTypes2.default.func),
|
303 | ArrayFieldTemplate: _propTypes2.default.func,
|
304 | ObjectFieldTemplate: _propTypes2.default.func,
|
305 | FieldTemplate: _propTypes2.default.func,
|
306 | ErrorList: _propTypes2.default.func,
|
307 | onChange: _propTypes2.default.func,
|
308 | onError: _propTypes2.default.func,
|
309 | showErrorList: _propTypes2.default.bool,
|
310 | onSubmit: _propTypes2.default.func,
|
311 | id: _propTypes2.default.string,
|
312 | className: _propTypes2.default.string,
|
313 | name: _propTypes2.default.string,
|
314 | method: _propTypes2.default.string,
|
315 | target: _propTypes2.default.string,
|
316 | action: _propTypes2.default.string,
|
317 | autocomplete: _propTypes2.default.string,
|
318 | enctype: _propTypes2.default.string,
|
319 | acceptcharset: _propTypes2.default.string,
|
320 | noValidate: _propTypes2.default.bool,
|
321 | noHtml5Validate: _propTypes2.default.bool,
|
322 | liveValidate: _propTypes2.default.bool,
|
323 | validate: _propTypes2.default.func,
|
324 | transformErrors: _propTypes2.default.func,
|
325 | safeRenderCompletion: _propTypes2.default.bool,
|
326 | formContext: _propTypes2.default.object
|
327 | };
|
328 | } |
\ | No newline at end of file |