UNPKG

41.8 kBJavaScriptView Raw
1"use strict";
2
3var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");
4
5var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
6
7exports.__esModule = true;
8exports["default"] = void 0;
9
10var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
11
12var _inheritsLoose2 = _interopRequireDefault(require("@babel/runtime/helpers/inheritsLoose"));
13
14var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
15
16var _objectWithoutPropertiesLoose2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutPropertiesLoose"));
17
18var _merge4 = _interopRequireDefault(require("lodash/merge"));
19
20var _mapValues2 = _interopRequireDefault(require("lodash/mapValues"));
21
22var _hoistNonReactStatics = _interopRequireDefault(require("hoist-non-react-statics"));
23
24var _invariant = _interopRequireDefault(require("invariant"));
25
26var _isPromise = _interopRequireDefault(require("is-promise"));
27
28var _propTypes = _interopRequireDefault(require("prop-types"));
29
30var _react = _interopRequireWildcard(require("react"));
31
32var _reactRedux = require("react-redux");
33
34var _redux = require("redux");
35
36var _actions = _interopRequireDefault(require("./actions"));
37
38var _asyncValidation = _interopRequireDefault(require("./asyncValidation"));
39
40var _defaultShouldAsyncValidate = _interopRequireDefault(require("./defaultShouldAsyncValidate"));
41
42var _defaultShouldValidate = _interopRequireDefault(require("./defaultShouldValidate"));
43
44var _defaultShouldError = _interopRequireDefault(require("./defaultShouldError"));
45
46var _defaultShouldWarn = _interopRequireDefault(require("./defaultShouldWarn"));
47
48var _silenceEvent = _interopRequireDefault(require("./events/silenceEvent"));
49
50var _silenceEvents = _interopRequireDefault(require("./events/silenceEvents"));
51
52var _generateValidator = _interopRequireDefault(require("./generateValidator"));
53
54var _handleSubmit = _interopRequireDefault(require("./handleSubmit"));
55
56var _isValid = _interopRequireDefault(require("./selectors/isValid"));
57
58var _plain = _interopRequireDefault(require("./structure/plain"));
59
60var _getDisplayName = _interopRequireDefault(require("./util/getDisplayName"));
61
62var _isHotReloading = _interopRequireDefault(require("./util/isHotReloading"));
63
64var _ReduxFormContext = require("./ReduxFormContext");
65
66var isClassComponent = function isClassComponent(Component) {
67 return Boolean(Component && Component.prototype && typeof Component.prototype.isReactComponent === 'object');
68}; // extract field-specific actions
69
70
71var arrayInsert = _actions["default"].arrayInsert,
72 arrayMove = _actions["default"].arrayMove,
73 arrayPop = _actions["default"].arrayPop,
74 arrayPush = _actions["default"].arrayPush,
75 arrayRemove = _actions["default"].arrayRemove,
76 arrayRemoveAll = _actions["default"].arrayRemoveAll,
77 arrayShift = _actions["default"].arrayShift,
78 arraySplice = _actions["default"].arraySplice,
79 arraySwap = _actions["default"].arraySwap,
80 arrayUnshift = _actions["default"].arrayUnshift,
81 blur = _actions["default"].blur,
82 change = _actions["default"].change,
83 focus = _actions["default"].focus,
84 formActions = (0, _objectWithoutPropertiesLoose2["default"])(_actions["default"], ["arrayInsert", "arrayMove", "arrayPop", "arrayPush", "arrayRemove", "arrayRemoveAll", "arrayShift", "arraySplice", "arraySwap", "arrayUnshift", "blur", "change", "focus"]);
85var arrayActions = {
86 arrayInsert: arrayInsert,
87 arrayMove: arrayMove,
88 arrayPop: arrayPop,
89 arrayPush: arrayPush,
90 arrayRemove: arrayRemove,
91 arrayRemoveAll: arrayRemoveAll,
92 arrayShift: arrayShift,
93 arraySplice: arraySplice,
94 arraySwap: arraySwap,
95 arrayUnshift: arrayUnshift
96};
97var propsToNotUpdateFor = [].concat(Object.keys(_actions["default"]), ['array', 'asyncErrors', 'initialValues', 'syncErrors', 'syncWarnings', 'values', 'registeredFields']);
98
99var checkSubmit = function checkSubmit(submit) {
100 if (!submit || typeof submit !== 'function') {
101 throw new Error('You must either pass handleSubmit() an onSubmit function or pass onSubmit as a prop');
102 }
103
104 return submit;
105};
106
107/**
108 * The decorator that is the main API to redux-form
109 */
110var createReduxForm = function createReduxForm(structure) {
111 var deepEqual = structure.deepEqual,
112 empty = structure.empty,
113 getIn = structure.getIn,
114 setIn = structure.setIn,
115 keys = structure.keys,
116 fromJS = structure.fromJS,
117 toJS = structure.toJS;
118 var isValid = (0, _isValid["default"])(structure);
119 return function (initialConfig) {
120 var config = (0, _extends2["default"])({
121 touchOnBlur: true,
122 touchOnChange: false,
123 persistentSubmitErrors: false,
124 destroyOnUnmount: true,
125 shouldAsyncValidate: _defaultShouldAsyncValidate["default"],
126 shouldValidate: _defaultShouldValidate["default"],
127 shouldError: _defaultShouldError["default"],
128 shouldWarn: _defaultShouldWarn["default"],
129 enableReinitialize: false,
130 keepDirtyOnReinitialize: false,
131 updateUnregisteredFields: false,
132 getFormState: function getFormState(state) {
133 return getIn(state, 'form');
134 },
135 pure: true,
136 forceUnregisterOnUnmount: false,
137 submitAsSideEffect: false
138 }, initialConfig);
139 return function (WrappedComponent) {
140 var Form =
141 /*#__PURE__*/
142 function (_React$Component) {
143 (0, _inheritsLoose2["default"])(Form, _React$Component);
144
145 function Form() {
146 var _this;
147
148 for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
149 args[_key] = arguments[_key];
150 }
151
152 _this = _React$Component.call.apply(_React$Component, [this].concat(args)) || this;
153 _this.wrapped = _react["default"].createRef();
154 _this.destroyed = false;
155 _this.fieldCounts = {};
156 _this.fieldValidators = {};
157 _this.lastFieldValidatorKeys = [];
158 _this.fieldWarners = {};
159 _this.lastFieldWarnerKeys = [];
160 _this.innerOnSubmit = undefined;
161 _this.submitPromise = undefined;
162
163 _this.getValues = function () {
164 return _this.props.values;
165 };
166
167 _this.isValid = function () {
168 return _this.props.valid;
169 };
170
171 _this.isPristine = function () {
172 return _this.props.pristine;
173 };
174
175 _this.register = function (name, type, getValidator, getWarner) {
176 var lastCount = _this.fieldCounts[name];
177 var nextCount = (lastCount || 0) + 1;
178 _this.fieldCounts[name] = nextCount;
179
180 _this.props.registerField(name, type);
181
182 if (getValidator) {
183 _this.fieldValidators[name] = getValidator;
184 }
185
186 if (getWarner) {
187 _this.fieldWarners[name] = getWarner;
188 }
189 };
190
191 _this.unregister = function (name) {
192 var lastCount = _this.fieldCounts[name];
193 if (lastCount === 1) delete _this.fieldCounts[name];else if (lastCount != null) _this.fieldCounts[name] = lastCount - 1;
194
195 if (!_this.destroyed) {
196 var _this$props = _this.props,
197 _destroyOnUnmount = _this$props.destroyOnUnmount,
198 forceUnregisterOnUnmount = _this$props.forceUnregisterOnUnmount,
199 unregisterField = _this$props.unregisterField;
200
201 if (_destroyOnUnmount || forceUnregisterOnUnmount) {
202 unregisterField(name, _destroyOnUnmount);
203
204 if (!_this.fieldCounts[name]) {
205 delete _this.fieldValidators[name];
206 delete _this.fieldWarners[name];
207 _this.lastFieldValidatorKeys = _this.lastFieldValidatorKeys.filter(function (key) {
208 return key !== name;
209 });
210 }
211 } else {
212 unregisterField(name, false);
213 }
214 }
215 };
216
217 _this.getFieldList = function (options) {
218 var registeredFields = _this.props.registeredFields;
219
220 if (!registeredFields) {
221 return [];
222 }
223
224 var keySeq = keys(registeredFields);
225
226 if (options) {
227 if (options.excludeFieldArray) {
228 keySeq = keySeq.filter(function (name) {
229 return getIn(registeredFields, "['" + name + "'].type") !== 'FieldArray';
230 });
231 }
232
233 if (options.excludeUnregistered) {
234 keySeq = keySeq.filter(function (name) {
235 return getIn(registeredFields, "['" + name + "'].count") !== 0;
236 });
237 }
238 }
239
240 return toJS(keySeq);
241 };
242
243 _this.getValidators = function () {
244 var validators = {};
245 Object.keys(_this.fieldValidators).forEach(function (name) {
246 var validator = _this.fieldValidators[name]();
247
248 if (validator) {
249 validators[name] = validator;
250 }
251 });
252 return validators;
253 };
254
255 _this.generateValidator = function () {
256 var validators = _this.getValidators();
257
258 return Object.keys(validators).length ? (0, _generateValidator["default"])(validators, structure) : undefined;
259 };
260
261 _this.getWarners = function () {
262 var warners = {};
263 Object.keys(_this.fieldWarners).forEach(function (name) {
264 var warner = _this.fieldWarners[name]();
265
266 if (warner) {
267 warners[name] = warner;
268 }
269 });
270 return warners;
271 };
272
273 _this.generateWarner = function () {
274 var warners = _this.getWarners();
275
276 return Object.keys(warners).length ? (0, _generateValidator["default"])(warners, structure) : undefined;
277 };
278
279 _this.asyncValidate = function (name, value, trigger) {
280 var _this$props2 = _this.props,
281 asyncBlurFields = _this$props2.asyncBlurFields,
282 asyncChangeFields = _this$props2.asyncChangeFields,
283 asyncErrors = _this$props2.asyncErrors,
284 asyncValidate = _this$props2.asyncValidate,
285 dispatch = _this$props2.dispatch,
286 initialized = _this$props2.initialized,
287 pristine = _this$props2.pristine,
288 shouldAsyncValidate = _this$props2.shouldAsyncValidate,
289 startAsyncValidation = _this$props2.startAsyncValidation,
290 stopAsyncValidation = _this$props2.stopAsyncValidation,
291 syncErrors = _this$props2.syncErrors,
292 values = _this$props2.values;
293 var submitting = !name;
294
295 var fieldNeedsValidation = function fieldNeedsValidation() {
296 var fieldNeedsValidationForBlur = asyncBlurFields && name && ~asyncBlurFields.indexOf(name.replace(/\[[0-9]+\]/g, '[]'));
297 var fieldNeedsValidationForChange = asyncChangeFields && name && ~asyncChangeFields.indexOf(name.replace(/\[[0-9]+\]/g, '[]'));
298 var asyncValidateByDefault = !(asyncBlurFields || asyncChangeFields);
299 return submitting || asyncValidateByDefault || (trigger === 'blur' ? fieldNeedsValidationForBlur : fieldNeedsValidationForChange);
300 };
301
302 if (asyncValidate) {
303 var valuesToValidate = submitting ? values : setIn(values, name, value);
304 var syncValidationPasses = submitting || !getIn(syncErrors, name);
305
306 if (fieldNeedsValidation() && shouldAsyncValidate({
307 asyncErrors: asyncErrors,
308 initialized: initialized,
309 trigger: submitting ? 'submit' : trigger,
310 blurredField: name,
311 pristine: pristine,
312 syncValidationPasses: syncValidationPasses
313 })) {
314 return (0, _asyncValidation["default"])(function () {
315 return asyncValidate(valuesToValidate, dispatch, _this.props, name);
316 }, startAsyncValidation, stopAsyncValidation, name);
317 }
318 }
319 };
320
321 _this.submitCompleted = function (result) {
322 delete _this.submitPromise;
323 return result;
324 };
325
326 _this.submitFailed = function (error) {
327 delete _this.submitPromise;
328 throw error;
329 };
330
331 _this.listenToSubmit = function (promise) {
332 if (!(0, _isPromise["default"])(promise)) {
333 return promise;
334 }
335
336 _this.submitPromise = promise;
337 return promise.then(_this.submitCompleted, _this.submitFailed);
338 };
339
340 _this.submit = function (submitOrEvent) {
341 var _this$props3 = _this.props,
342 onSubmit = _this$props3.onSubmit,
343 blur = _this$props3.blur,
344 change = _this$props3.change,
345 dispatch = _this$props3.dispatch;
346
347 if (!submitOrEvent || (0, _silenceEvent["default"])(submitOrEvent)) {
348 // submitOrEvent is an event: fire submit if not already submitting
349 if (!_this.submitPromise) {
350 // avoid recursive stack trace if use Form with onSubmit as handleSubmit
351 if (_this.innerOnSubmit && _this.innerOnSubmit !== _this.submit) {
352 // will call "submitOrEvent is the submit function" block below
353 return _this.innerOnSubmit();
354 } else {
355 return _this.listenToSubmit((0, _handleSubmit["default"])(checkSubmit(onSubmit), (0, _extends2["default"])({}, _this.props, {}, (0, _redux.bindActionCreators)({
356 blur: blur,
357 change: change
358 }, dispatch)), _this.props.validExceptSubmit, _this.asyncValidate, _this.getFieldList({
359 excludeFieldArray: true,
360 excludeUnregistered: true
361 })));
362 }
363 }
364 } else {
365 // submitOrEvent is the submit function: return deferred submit thunk
366 return (0, _silenceEvents["default"])(function () {
367 return !_this.submitPromise && _this.listenToSubmit((0, _handleSubmit["default"])(checkSubmit(submitOrEvent), (0, _extends2["default"])({}, _this.props, {}, (0, _redux.bindActionCreators)({
368 blur: blur,
369 change: change
370 }, dispatch)), _this.props.validExceptSubmit, _this.asyncValidate, _this.getFieldList({
371 excludeFieldArray: true,
372 excludeUnregistered: true
373 })));
374 });
375 }
376 };
377
378 _this.reset = function () {
379 return _this.props.reset();
380 };
381
382 return _this;
383 }
384
385 var _proto = Form.prototype;
386
387 _proto.initIfNeeded = function initIfNeeded(nextProps) {
388 var enableReinitialize = this.props.enableReinitialize;
389
390 if (nextProps) {
391 if ((enableReinitialize || !nextProps.initialized) && !deepEqual(this.props.initialValues, nextProps.initialValues)) {
392 var _keepDirty = nextProps.initialized && this.props.keepDirtyOnReinitialize;
393
394 this.props.initialize(nextProps.initialValues, _keepDirty, {
395 keepValues: nextProps.keepValues,
396 lastInitialValues: this.props.initialValues,
397 updateUnregisteredFields: nextProps.updateUnregisteredFields
398 });
399 }
400 } else if (this.props.initialValues && (!this.props.initialized || enableReinitialize)) {
401 this.props.initialize(this.props.initialValues, this.props.keepDirtyOnReinitialize, {
402 keepValues: this.props.keepValues,
403 updateUnregisteredFields: this.props.updateUnregisteredFields
404 });
405 }
406 };
407
408 _proto.updateSyncErrorsIfNeeded = function updateSyncErrorsIfNeeded(nextSyncErrors, nextError, lastSyncErrors) {
409 var _this$props4 = this.props,
410 error = _this$props4.error,
411 updateSyncErrors = _this$props4.updateSyncErrors;
412 var noErrors = (!lastSyncErrors || !Object.keys(lastSyncErrors).length) && !error;
413 var nextNoErrors = (!nextSyncErrors || !Object.keys(nextSyncErrors).length) && !nextError;
414
415 if (!(noErrors && nextNoErrors) && (!_plain["default"].deepEqual(lastSyncErrors, nextSyncErrors) || !_plain["default"].deepEqual(error, nextError))) {
416 updateSyncErrors(nextSyncErrors, nextError);
417 }
418 };
419
420 _proto.clearSubmitPromiseIfNeeded = function clearSubmitPromiseIfNeeded(nextProps) {
421 var submitting = this.props.submitting;
422
423 if (this.submitPromise && submitting && !nextProps.submitting) {
424 delete this.submitPromise;
425 }
426 };
427
428 _proto.submitIfNeeded = function submitIfNeeded(nextProps) {
429 var _this$props5 = this.props,
430 clearSubmit = _this$props5.clearSubmit,
431 triggerSubmit = _this$props5.triggerSubmit;
432
433 if (!triggerSubmit && nextProps.triggerSubmit) {
434 clearSubmit();
435 this.submit();
436 }
437 };
438
439 _proto.shouldErrorFunction = function shouldErrorFunction() {
440 var _this$props6 = this.props,
441 shouldValidate = _this$props6.shouldValidate,
442 shouldError = _this$props6.shouldError;
443 var shouldValidateOverridden = shouldValidate !== _defaultShouldValidate["default"];
444 var shouldErrorOverridden = shouldError !== _defaultShouldError["default"];
445 return shouldValidateOverridden && !shouldErrorOverridden ? shouldValidate : shouldError;
446 };
447
448 _proto.validateIfNeeded = function validateIfNeeded(nextProps) {
449 var _this$props7 = this.props,
450 validate = _this$props7.validate,
451 values = _this$props7.values;
452 var shouldError = this.shouldErrorFunction();
453 var fieldLevelValidate = this.generateValidator();
454
455 if (validate || fieldLevelValidate) {
456 var initialRender = nextProps === undefined;
457 var fieldValidatorKeys = Object.keys(this.getValidators());
458 var validateParams = {
459 values: values,
460 nextProps: nextProps,
461 props: this.props,
462 initialRender: initialRender,
463 lastFieldValidatorKeys: this.lastFieldValidatorKeys,
464 fieldValidatorKeys: fieldValidatorKeys,
465 structure: structure
466 };
467
468 if (shouldError(validateParams)) {
469 var propsToValidate = initialRender || !nextProps ? this.props : nextProps;
470
471 var _merge2 = (0, _merge4["default"])(validate ? validate(propsToValidate.values, propsToValidate) || {} : {}, fieldLevelValidate ? fieldLevelValidate(propsToValidate.values, propsToValidate) || {} : {}),
472 _error = _merge2._error,
473 nextSyncErrors = (0, _objectWithoutPropertiesLoose2["default"])(_merge2, ["_error"]);
474
475 this.lastFieldValidatorKeys = fieldValidatorKeys;
476 this.updateSyncErrorsIfNeeded(nextSyncErrors, _error, propsToValidate.syncErrors);
477 }
478 } else {
479 this.lastFieldValidatorKeys = [];
480 }
481 };
482
483 _proto.updateSyncWarningsIfNeeded = function updateSyncWarningsIfNeeded(nextSyncWarnings, nextWarning, lastSyncWarnings) {
484 var _this$props8 = this.props,
485 warning = _this$props8.warning,
486 updateSyncWarnings = _this$props8.updateSyncWarnings;
487 var noWarnings = (!lastSyncWarnings || !Object.keys(lastSyncWarnings).length) && !warning;
488 var nextNoWarnings = (!nextSyncWarnings || !Object.keys(nextSyncWarnings).length) && !nextWarning;
489
490 if (!(noWarnings && nextNoWarnings) && (!_plain["default"].deepEqual(lastSyncWarnings, nextSyncWarnings) || !_plain["default"].deepEqual(warning, nextWarning))) {
491 updateSyncWarnings(nextSyncWarnings, nextWarning);
492 }
493 };
494
495 _proto.shouldWarnFunction = function shouldWarnFunction() {
496 var _this$props9 = this.props,
497 shouldValidate = _this$props9.shouldValidate,
498 shouldWarn = _this$props9.shouldWarn;
499 var shouldValidateOverridden = shouldValidate !== _defaultShouldValidate["default"];
500 var shouldWarnOverridden = shouldWarn !== _defaultShouldWarn["default"];
501 return shouldValidateOverridden && !shouldWarnOverridden ? shouldValidate : shouldWarn;
502 };
503
504 _proto.warnIfNeeded = function warnIfNeeded(nextProps) {
505 var _this$props10 = this.props,
506 warn = _this$props10.warn,
507 values = _this$props10.values;
508 var shouldWarn = this.shouldWarnFunction();
509 var fieldLevelWarn = this.generateWarner();
510
511 if (warn || fieldLevelWarn) {
512 var initialRender = nextProps === undefined;
513 var fieldWarnerKeys = Object.keys(this.getWarners());
514 var validateParams = {
515 values: values,
516 nextProps: nextProps,
517 props: this.props,
518 initialRender: initialRender,
519 lastFieldValidatorKeys: this.lastFieldWarnerKeys,
520 fieldValidatorKeys: fieldWarnerKeys,
521 structure: structure
522 };
523
524 if (shouldWarn(validateParams)) {
525 var propsToWarn = initialRender || !nextProps ? this.props : nextProps;
526
527 var _merge3 = (0, _merge4["default"])(warn ? warn(propsToWarn.values, propsToWarn) : {}, fieldLevelWarn ? fieldLevelWarn(propsToWarn.values, propsToWarn) : {}),
528 _warning = _merge3._warning,
529 nextSyncWarnings = (0, _objectWithoutPropertiesLoose2["default"])(_merge3, ["_warning"]);
530
531 this.lastFieldWarnerKeys = fieldWarnerKeys;
532 this.updateSyncWarningsIfNeeded(nextSyncWarnings, _warning, propsToWarn.syncWarnings);
533 }
534 }
535 };
536
537 _proto.UNSAFE_componentWillMount = function UNSAFE_componentWillMount() {
538 if (!(0, _isHotReloading["default"])()) {
539 this.initIfNeeded();
540 this.validateIfNeeded();
541 this.warnIfNeeded();
542 }
543
544 (0, _invariant["default"])(this.props.shouldValidate, 'shouldValidate() is deprecated and will be removed in v9.0.0. Use shouldWarn() or shouldError() instead.');
545 };
546
547 _proto.UNSAFE_componentWillReceiveProps = function UNSAFE_componentWillReceiveProps(nextProps) {
548 this.initIfNeeded(nextProps);
549 this.validateIfNeeded(nextProps);
550 this.warnIfNeeded(nextProps);
551 this.clearSubmitPromiseIfNeeded(nextProps);
552 this.submitIfNeeded(nextProps);
553 var onChange = nextProps.onChange,
554 values = nextProps.values,
555 dispatch = nextProps.dispatch;
556
557 if (onChange && !deepEqual(values, this.props.values)) {
558 onChange(values, dispatch, nextProps, this.props.values);
559 }
560 };
561
562 _proto.shouldComponentUpdate = function shouldComponentUpdate(nextProps) {
563 var _this2 = this;
564
565 if (!this.props.pure) return true;
566 var _config$immutableProp = config.immutableProps,
567 immutableProps = _config$immutableProp === void 0 ? [] : _config$immutableProp; // if we have children, we MUST update in React 16
568 // https://twitter.com/erikras/status/915866544558788608
569
570 return !!(this.props.children || nextProps.children || Object.keys(nextProps).some(function (prop) {
571 // useful to debug rerenders
572 // if (!plain.deepEqual(this.props[ prop ], nextProps[ prop ])) {
573 // console.info(prop, 'changed', this.props[ prop ], '==>', nextProps[ prop ])
574 // }
575 if (~immutableProps.indexOf(prop)) {
576 return _this2.props[prop] !== nextProps[prop];
577 }
578
579 return !~propsToNotUpdateFor.indexOf(prop) && !deepEqual(_this2.props[prop], nextProps[prop]);
580 }));
581 };
582
583 _proto.componentDidMount = function componentDidMount() {
584 if (!(0, _isHotReloading["default"])()) {
585 this.initIfNeeded(this.props);
586 this.validateIfNeeded();
587 this.warnIfNeeded();
588 }
589
590 (0, _invariant["default"])(this.props.shouldValidate, 'shouldValidate() is deprecated and will be removed in v9.0.0. Use shouldWarn() or shouldError() instead.');
591 };
592
593 _proto.componentWillUnmount = function componentWillUnmount() {
594 var _this$props11 = this.props,
595 destroyOnUnmount = _this$props11.destroyOnUnmount,
596 destroy = _this$props11.destroy;
597
598 if (destroyOnUnmount && !(0, _isHotReloading["default"])()) {
599 this.destroyed = true;
600 destroy();
601 }
602 };
603
604 _proto.render = function render() {
605 var _ref,
606 _this3 = this;
607
608 // remove some redux-form config-only props
609
610 /* eslint-disable no-unused-vars */
611 var _this$props12 = this.props,
612 anyTouched = _this$props12.anyTouched,
613 array = _this$props12.array,
614 arrayInsert = _this$props12.arrayInsert,
615 arrayMove = _this$props12.arrayMove,
616 arrayPop = _this$props12.arrayPop,
617 arrayPush = _this$props12.arrayPush,
618 arrayRemove = _this$props12.arrayRemove,
619 arrayRemoveAll = _this$props12.arrayRemoveAll,
620 arrayShift = _this$props12.arrayShift,
621 arraySplice = _this$props12.arraySplice,
622 arraySwap = _this$props12.arraySwap,
623 arrayUnshift = _this$props12.arrayUnshift,
624 asyncErrors = _this$props12.asyncErrors,
625 asyncValidate = _this$props12.asyncValidate,
626 asyncValidating = _this$props12.asyncValidating,
627 blur = _this$props12.blur,
628 change = _this$props12.change,
629 clearSubmit = _this$props12.clearSubmit,
630 destroy = _this$props12.destroy,
631 destroyOnUnmount = _this$props12.destroyOnUnmount,
632 forceUnregisterOnUnmount = _this$props12.forceUnregisterOnUnmount,
633 dirty = _this$props12.dirty,
634 dispatch = _this$props12.dispatch,
635 enableReinitialize = _this$props12.enableReinitialize,
636 error = _this$props12.error,
637 focus = _this$props12.focus,
638 form = _this$props12.form,
639 getFormState = _this$props12.getFormState,
640 immutableProps = _this$props12.immutableProps,
641 initialize = _this$props12.initialize,
642 initialized = _this$props12.initialized,
643 initialValues = _this$props12.initialValues,
644 invalid = _this$props12.invalid,
645 keepDirtyOnReinitialize = _this$props12.keepDirtyOnReinitialize,
646 keepValues = _this$props12.keepValues,
647 updateUnregisteredFields = _this$props12.updateUnregisteredFields,
648 pristine = _this$props12.pristine,
649 propNamespace = _this$props12.propNamespace,
650 registeredFields = _this$props12.registeredFields,
651 registerField = _this$props12.registerField,
652 reset = _this$props12.reset,
653 resetSection = _this$props12.resetSection,
654 setSubmitFailed = _this$props12.setSubmitFailed,
655 setSubmitSucceeded = _this$props12.setSubmitSucceeded,
656 shouldAsyncValidate = _this$props12.shouldAsyncValidate,
657 shouldValidate = _this$props12.shouldValidate,
658 shouldError = _this$props12.shouldError,
659 shouldWarn = _this$props12.shouldWarn,
660 startAsyncValidation = _this$props12.startAsyncValidation,
661 startSubmit = _this$props12.startSubmit,
662 stopAsyncValidation = _this$props12.stopAsyncValidation,
663 stopSubmit = _this$props12.stopSubmit,
664 submitAsSideEffect = _this$props12.submitAsSideEffect,
665 submitting = _this$props12.submitting,
666 submitFailed = _this$props12.submitFailed,
667 submitSucceeded = _this$props12.submitSucceeded,
668 touch = _this$props12.touch,
669 touchOnBlur = _this$props12.touchOnBlur,
670 touchOnChange = _this$props12.touchOnChange,
671 persistentSubmitErrors = _this$props12.persistentSubmitErrors,
672 syncErrors = _this$props12.syncErrors,
673 syncWarnings = _this$props12.syncWarnings,
674 unregisterField = _this$props12.unregisterField,
675 untouch = _this$props12.untouch,
676 updateSyncErrors = _this$props12.updateSyncErrors,
677 updateSyncWarnings = _this$props12.updateSyncWarnings,
678 valid = _this$props12.valid,
679 validExceptSubmit = _this$props12.validExceptSubmit,
680 values = _this$props12.values,
681 warning = _this$props12.warning,
682 rest = (0, _objectWithoutPropertiesLoose2["default"])(_this$props12, ["anyTouched", "array", "arrayInsert", "arrayMove", "arrayPop", "arrayPush", "arrayRemove", "arrayRemoveAll", "arrayShift", "arraySplice", "arraySwap", "arrayUnshift", "asyncErrors", "asyncValidate", "asyncValidating", "blur", "change", "clearSubmit", "destroy", "destroyOnUnmount", "forceUnregisterOnUnmount", "dirty", "dispatch", "enableReinitialize", "error", "focus", "form", "getFormState", "immutableProps", "initialize", "initialized", "initialValues", "invalid", "keepDirtyOnReinitialize", "keepValues", "updateUnregisteredFields", "pristine", "propNamespace", "registeredFields", "registerField", "reset", "resetSection", "setSubmitFailed", "setSubmitSucceeded", "shouldAsyncValidate", "shouldValidate", "shouldError", "shouldWarn", "startAsyncValidation", "startSubmit", "stopAsyncValidation", "stopSubmit", "submitAsSideEffect", "submitting", "submitFailed", "submitSucceeded", "touch", "touchOnBlur", "touchOnChange", "persistentSubmitErrors", "syncErrors", "syncWarnings", "unregisterField", "untouch", "updateSyncErrors", "updateSyncWarnings", "valid", "validExceptSubmit", "values", "warning"]);
683 /* eslint-enable no-unused-vars */
684
685 var reduxFormProps = (0, _extends2["default"])({
686 array: array,
687 anyTouched: anyTouched,
688 asyncValidate: this.asyncValidate,
689 asyncValidating: asyncValidating
690 }, (0, _redux.bindActionCreators)({
691 blur: blur,
692 change: change
693 }, dispatch), {
694 clearSubmit: clearSubmit,
695 destroy: destroy,
696 dirty: dirty,
697 dispatch: dispatch,
698 error: error,
699 form: form,
700 handleSubmit: this.submit,
701 initialize: initialize,
702 initialized: initialized,
703 initialValues: initialValues,
704 invalid: invalid,
705 pristine: pristine,
706 reset: reset,
707 resetSection: resetSection,
708 submitting: submitting,
709 submitAsSideEffect: submitAsSideEffect,
710 submitFailed: submitFailed,
711 submitSucceeded: submitSucceeded,
712 touch: touch,
713 untouch: untouch,
714 valid: valid,
715 warning: warning
716 });
717 var propsToPass = (0, _extends2["default"])({}, propNamespace ? (_ref = {}, _ref[propNamespace] = reduxFormProps, _ref) : reduxFormProps, {}, rest);
718
719 if (isClassComponent(WrappedComponent)) {
720 ;
721 propsToPass.ref = this.wrapped;
722 }
723
724 var _reduxForm = (0, _extends2["default"])({}, this.props, {
725 getFormState: function getFormState(state) {
726 return getIn(_this3.props.getFormState(state), _this3.props.form);
727 },
728 asyncValidate: this.asyncValidate,
729 getValues: this.getValues,
730 sectionPrefix: undefined,
731 register: this.register,
732 unregister: this.unregister,
733 registerInnerOnSubmit: function registerInnerOnSubmit(innerOnSubmit) {
734 return _this3.innerOnSubmit = innerOnSubmit;
735 }
736 });
737
738 return (0, _react.createElement)(_ReduxFormContext.ReduxFormContext.Provider, {
739 value: _reduxForm,
740 children: (0, _react.createElement)(WrappedComponent, propsToPass)
741 });
742 };
743
744 return Form;
745 }(_react["default"].Component);
746
747 Form.displayName = "Form(" + (0, _getDisplayName["default"])(WrappedComponent) + ")";
748 Form.WrappedComponent = WrappedComponent;
749 Form.propTypes = {
750 destroyOnUnmount: _propTypes["default"].bool,
751 forceUnregisterOnUnmount: _propTypes["default"].bool,
752 form: _propTypes["default"].string.isRequired,
753 immutableProps: _propTypes["default"].arrayOf(_propTypes["default"].string),
754 initialValues: _propTypes["default"].oneOfType([_propTypes["default"].array, _propTypes["default"].object]),
755 getFormState: _propTypes["default"].func,
756 onSubmitFail: _propTypes["default"].func,
757 onSubmitSuccess: _propTypes["default"].func,
758 propNamespace: _propTypes["default"].string,
759 validate: _propTypes["default"].func,
760 warn: _propTypes["default"].func,
761 touchOnBlur: _propTypes["default"].bool,
762 touchOnChange: _propTypes["default"].bool,
763 triggerSubmit: _propTypes["default"].bool,
764 persistentSubmitErrors: _propTypes["default"].bool,
765 registeredFields: _propTypes["default"].any
766 };
767 var connector = (0, _reactRedux.connect)(function (state, props) {
768 var form = props.form,
769 getFormState = props.getFormState,
770 initialValues = props.initialValues,
771 enableReinitialize = props.enableReinitialize,
772 keepDirtyOnReinitialize = props.keepDirtyOnReinitialize;
773 var formState = getIn(getFormState(state) || empty, form) || empty;
774 var stateInitial = getIn(formState, 'initial');
775 var initialized = !!stateInitial;
776 var shouldUpdateInitialValues = enableReinitialize && initialized && !deepEqual(initialValues, stateInitial);
777 var shouldResetValues = shouldUpdateInitialValues && !keepDirtyOnReinitialize;
778 var initial = initialValues || stateInitial || empty;
779
780 if (!shouldUpdateInitialValues) {
781 initial = stateInitial || empty;
782 }
783
784 var values = getIn(formState, 'values') || initial;
785
786 if (shouldResetValues) {
787 values = initial;
788 }
789
790 var pristine = shouldResetValues || deepEqual(initial, values);
791 var asyncErrors = getIn(formState, 'asyncErrors');
792
793 var syncErrors = getIn(formState, 'syncErrors') || _plain["default"].empty;
794
795 var syncWarnings = getIn(formState, 'syncWarnings') || _plain["default"].empty;
796
797 var registeredFields = getIn(formState, 'registeredFields');
798 var valid = isValid(form, getFormState, false)(state);
799 var validExceptSubmit = isValid(form, getFormState, true)(state);
800 var anyTouched = !!getIn(formState, 'anyTouched');
801 var submitting = !!getIn(formState, 'submitting');
802 var submitFailed = !!getIn(formState, 'submitFailed');
803 var submitSucceeded = !!getIn(formState, 'submitSucceeded');
804 var error = getIn(formState, 'error');
805 var warning = getIn(formState, 'warning');
806 var triggerSubmit = getIn(formState, 'triggerSubmit');
807 return {
808 anyTouched: anyTouched,
809 asyncErrors: asyncErrors,
810 asyncValidating: getIn(formState, 'asyncValidating') || false,
811 dirty: !pristine,
812 error: error,
813 initialized: initialized,
814 invalid: !valid,
815 pristine: pristine,
816 registeredFields: registeredFields,
817 submitting: submitting,
818 submitFailed: submitFailed,
819 submitSucceeded: submitSucceeded,
820 syncErrors: syncErrors,
821 syncWarnings: syncWarnings,
822 triggerSubmit: triggerSubmit,
823 values: values,
824 valid: valid,
825 validExceptSubmit: validExceptSubmit,
826 warning: warning
827 };
828 }, function (dispatch, initialProps) {
829 var bindForm = function bindForm(actionCreator) {
830 return actionCreator.bind(null, initialProps.form);
831 }; // Bind the first parameter on `props.form`
832
833
834 var boundFormACs = (0, _mapValues2["default"])(formActions, bindForm);
835 var boundArrayACs = (0, _mapValues2["default"])(arrayActions, bindForm);
836
837 var boundBlur = function boundBlur(field, value) {
838 return blur(initialProps.form, field, value, !!initialProps.touchOnBlur);
839 };
840
841 var boundChange = function boundChange(field, value) {
842 return change(initialProps.form, field, value, !!initialProps.touchOnChange, !!initialProps.persistentSubmitErrors);
843 };
844
845 var boundFocus = bindForm(focus); // Wrap action creators with `dispatch`
846
847 var connectedFormACs = (0, _redux.bindActionCreators)(boundFormACs, dispatch);
848 var connectedArrayACs = {
849 insert: (0, _redux.bindActionCreators)(boundArrayACs.arrayInsert, dispatch),
850 move: (0, _redux.bindActionCreators)(boundArrayACs.arrayMove, dispatch),
851 pop: (0, _redux.bindActionCreators)(boundArrayACs.arrayPop, dispatch),
852 push: (0, _redux.bindActionCreators)(boundArrayACs.arrayPush, dispatch),
853 remove: (0, _redux.bindActionCreators)(boundArrayACs.arrayRemove, dispatch),
854 removeAll: (0, _redux.bindActionCreators)(boundArrayACs.arrayRemoveAll, dispatch),
855 shift: (0, _redux.bindActionCreators)(boundArrayACs.arrayShift, dispatch),
856 splice: (0, _redux.bindActionCreators)(boundArrayACs.arraySplice, dispatch),
857 swap: (0, _redux.bindActionCreators)(boundArrayACs.arraySwap, dispatch),
858 unshift: (0, _redux.bindActionCreators)(boundArrayACs.arrayUnshift, dispatch)
859 };
860 return (0, _extends2["default"])({}, connectedFormACs, {}, boundArrayACs, {
861 blur: boundBlur,
862 change: boundChange,
863 array: connectedArrayACs,
864 focus: boundFocus,
865 dispatch: dispatch
866 });
867 }, undefined, {
868 forwardRef: true
869 });
870 var ConnectedForm = (0, _hoistNonReactStatics["default"])(connector(Form), WrappedComponent);
871 ConnectedForm.defaultProps = config; // build outer component to expose instance api
872
873 var ReduxForm =
874 /*#__PURE__*/
875 function (_React$Component2) {
876 (0, _inheritsLoose2["default"])(ReduxForm, _React$Component2);
877
878 function ReduxForm() {
879 var _this4;
880
881 for (var _len2 = arguments.length, args = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
882 args[_key2] = arguments[_key2];
883 }
884
885 _this4 = _React$Component2.call.apply(_React$Component2, [this].concat(args)) || this;
886 _this4.ref = _react["default"].createRef();
887 return _this4;
888 }
889
890 var _proto2 = ReduxForm.prototype;
891
892 _proto2.submit = function submit() {
893 return this.ref.current && this.ref.current.submit();
894 };
895
896 _proto2.reset = function reset() {
897 if (this.ref) {
898 this.ref.current.reset();
899 }
900 };
901
902 _proto2.render = function render() {
903 var _this$props13 = this.props,
904 initialValues = _this$props13.initialValues,
905 rest = (0, _objectWithoutPropertiesLoose2["default"])(_this$props13, ["initialValues"]);
906 return (0, _react.createElement)(ConnectedForm, (0, _extends2["default"])({}, rest, {
907 ref: this.ref,
908 // convert initialValues if need to
909 initialValues: fromJS(initialValues)
910 }));
911 };
912
913 (0, _createClass2["default"])(ReduxForm, [{
914 key: "valid",
915 get: function get() {
916 return !!(this.ref.current && this.ref.current.isValid());
917 }
918 }, {
919 key: "invalid",
920 get: function get() {
921 return !this.valid;
922 }
923 }, {
924 key: "pristine",
925 get: function get() {
926 return !!(this.ref.current && this.ref.current.isPristine());
927 }
928 }, {
929 key: "dirty",
930 get: function get() {
931 return !this.pristine;
932 }
933 }, {
934 key: "values",
935 get: function get() {
936 return this.ref.current ? this.ref.current.getValues() : empty;
937 }
938 }, {
939 key: "fieldList",
940 get: function get() {
941 // mainly provided for testing
942 return this.ref.current ? this.ref.current.getFieldList() : [];
943 }
944 }, {
945 key: "wrappedInstance",
946 get: function get() {
947 // for testing
948 return this.ref.current && this.ref.current.wrapped.current;
949 }
950 }]);
951 return ReduxForm;
952 }(_react["default"].Component);
953
954 var WithContext = (0, _hoistNonReactStatics["default"])((0, _ReduxFormContext.withReduxForm)(ReduxForm), WrappedComponent);
955 WithContext.defaultProps = config;
956 return WithContext;
957 };
958 };
959};
960
961var _default = createReduxForm;
962exports["default"] = _default;
\No newline at end of file