UNPKG

8.19 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 _objectWithoutPropertiesLoose2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutPropertiesLoose"));
11
12var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
13
14var _inheritsLoose2 = _interopRequireDefault(require("@babel/runtime/helpers/inheritsLoose"));
15
16var _mapValues2 = _interopRequireDefault(require("lodash/mapValues"));
17
18var _react = _interopRequireWildcard(require("react"));
19
20var _propTypes = _interopRequireDefault(require("prop-types"));
21
22var _reactRedux = require("react-redux");
23
24var _redux = require("redux");
25
26var _createFieldArrayProps = _interopRequireDefault(require("./createFieldArrayProps"));
27
28var _plain = _interopRequireDefault(require("./structure/plain"));
29
30var _validateComponentProp = _interopRequireDefault(require("./util/validateComponentProp"));
31
32var propsToNotUpdateFor = ['_reduxForm', 'value'];
33
34var createConnectedFieldArray = function createConnectedFieldArray(structure) {
35 var deepEqual = structure.deepEqual,
36 getIn = structure.getIn,
37 size = structure.size,
38 equals = structure.equals,
39 orderChanged = structure.orderChanged;
40
41 var getSyncError = function getSyncError(syncErrors, name) {
42 // For an array, the error can _ONLY_ be under _error.
43 // This is why this getSyncError is not the same as the
44 // one in Field.
45 return _plain["default"].getIn(syncErrors, name + "._error");
46 };
47
48 var getSyncWarning = function getSyncWarning(syncWarnings, name) {
49 // For an array, the warning can _ONLY_ be under _warning.
50 // This is why this getSyncError is not the same as the
51 // one in Field.
52 return getIn(syncWarnings, name + "._warning");
53 };
54
55 var ConnectedFieldArray =
56 /*#__PURE__*/
57 function (_Component) {
58 (0, _inheritsLoose2["default"])(ConnectedFieldArray, _Component);
59
60 function ConnectedFieldArray() {
61 var _this;
62
63 for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
64 args[_key] = arguments[_key];
65 }
66
67 _this = _Component.call.apply(_Component, [this].concat(args)) || this;
68 _this.ref = _react["default"].createRef();
69
70 _this.getValue = function (index) {
71 return _this.props.value && getIn(_this.props.value, String(index));
72 };
73
74 return _this;
75 }
76
77 var _proto = ConnectedFieldArray.prototype;
78
79 _proto.shouldComponentUpdate = function shouldComponentUpdate(nextProps) {
80 var _this2 = this;
81
82 // Update if the elements of the value array was updated.
83 var thisValue = this.props.value;
84 var nextValue = nextProps.value;
85
86 if (thisValue && nextValue) {
87 var nextValueItemsSame = equals(nextValue, thisValue); //.every(val => ~thisValue.indexOf(val))
88
89 var nextValueItemsOrderChanged = orderChanged(thisValue, nextValue);
90 var thisValueLength = thisValue.length || thisValue.size;
91 var nextValueLength = nextValue.length || nextValue.size;
92
93 if (thisValueLength !== nextValueLength || nextValueItemsSame && nextValueItemsOrderChanged || nextProps.rerenderOnEveryChange && thisValue.some(function (val, index) {
94 return !deepEqual(val, nextValue[index]);
95 })) {
96 return true;
97 }
98 }
99
100 var nextPropsKeys = Object.keys(nextProps);
101 var thisPropsKeys = Object.keys(this.props); // if we have children, we MUST update in React 16
102 // https://twitter.com/erikras/status/915866544558788608
103
104 return !!(this.props.children || nextProps.children || nextPropsKeys.length !== thisPropsKeys.length || nextPropsKeys.some(function (prop) {
105 // useful to debug rerenders
106 // if (!plain.deepEqual(this.props[ prop ], nextProps[ prop ])) {
107 // console.info(prop, 'changed', this.props[ prop ], '==>', nextProps[ prop ])
108 // }
109 return !~propsToNotUpdateFor.indexOf(prop) && !deepEqual(_this2.props[prop], nextProps[prop]);
110 }));
111 };
112
113 _proto.getRenderedComponent = function getRenderedComponent() {
114 return this.ref.current;
115 };
116
117 _proto.render = function render() {
118 var _this$props = this.props,
119 component = _this$props.component,
120 forwardRef = _this$props.forwardRef,
121 name = _this$props.name,
122 _reduxForm = _this$props._reduxForm,
123 validate = _this$props.validate,
124 warn = _this$props.warn,
125 rerenderOnEveryChange = _this$props.rerenderOnEveryChange,
126 rest = (0, _objectWithoutPropertiesLoose2["default"])(_this$props, ["component", "forwardRef", "name", "_reduxForm", "validate", "warn", "rerenderOnEveryChange"]);
127 var props = (0, _createFieldArrayProps["default"])(structure, name, _reduxForm.form, _reduxForm.sectionPrefix, this.getValue, rest);
128
129 if (forwardRef) {
130 props.ref = this.ref;
131 }
132
133 return (0, _react.createElement)(component, props);
134 };
135
136 (0, _createClass2["default"])(ConnectedFieldArray, [{
137 key: "dirty",
138 get: function get() {
139 return this.props.dirty;
140 }
141 }, {
142 key: "pristine",
143 get: function get() {
144 return this.props.pristine;
145 }
146 }, {
147 key: "value",
148 get: function get() {
149 return this.props.value;
150 }
151 }]);
152 return ConnectedFieldArray;
153 }(_react.Component);
154
155 ConnectedFieldArray.propTypes = {
156 component: _validateComponentProp["default"],
157 props: _propTypes["default"].object,
158 rerenderOnEveryChange: _propTypes["default"].bool
159 };
160 ConnectedFieldArray.defaultProps = {
161 rerenderOnEveryChange: false
162 };
163 var connector = (0, _reactRedux.connect)(function (state, ownProps) {
164 var name = ownProps.name,
165 _ownProps$_reduxForm = ownProps._reduxForm,
166 initialValues = _ownProps$_reduxForm.initialValues,
167 getFormState = _ownProps$_reduxForm.getFormState;
168 var formState = getFormState(state);
169 var initial = getIn(formState, "initial." + name) || initialValues && getIn(initialValues, name);
170 var value = getIn(formState, "values." + name);
171 var submitting = getIn(formState, 'submitting');
172 var syncError = getSyncError(getIn(formState, 'syncErrors'), name);
173 var syncWarning = getSyncWarning(getIn(formState, 'syncWarnings'), name);
174 var pristine = deepEqual(value, initial);
175 return {
176 asyncError: getIn(formState, "asyncErrors." + name + "._error"),
177 dirty: !pristine,
178 pristine: pristine,
179 state: getIn(formState, "fields." + name),
180 submitError: getIn(formState, "submitErrors." + name + "._error"),
181 submitFailed: getIn(formState, 'submitFailed'),
182 submitting: submitting,
183 syncError: syncError,
184 syncWarning: syncWarning,
185 value: value,
186 length: size(value)
187 };
188 }, function (dispatch, ownProps) {
189 var name = ownProps.name,
190 _reduxForm = ownProps._reduxForm;
191 var arrayInsert = _reduxForm.arrayInsert,
192 arrayMove = _reduxForm.arrayMove,
193 arrayPop = _reduxForm.arrayPop,
194 arrayPush = _reduxForm.arrayPush,
195 arrayRemove = _reduxForm.arrayRemove,
196 arrayRemoveAll = _reduxForm.arrayRemoveAll,
197 arrayShift = _reduxForm.arrayShift,
198 arraySplice = _reduxForm.arraySplice,
199 arraySwap = _reduxForm.arraySwap,
200 arrayUnshift = _reduxForm.arrayUnshift;
201 return (0, _mapValues2["default"])({
202 arrayInsert: arrayInsert,
203 arrayMove: arrayMove,
204 arrayPop: arrayPop,
205 arrayPush: arrayPush,
206 arrayRemove: arrayRemove,
207 arrayRemoveAll: arrayRemoveAll,
208 arrayShift: arrayShift,
209 arraySplice: arraySplice,
210 arraySwap: arraySwap,
211 arrayUnshift: arrayUnshift
212 }, function (actionCreator) {
213 return (0, _redux.bindActionCreators)(actionCreator.bind(null, name), dispatch);
214 });
215 }, undefined, {
216 forwardRef: true
217 });
218 return connector(ConnectedFieldArray);
219};
220
221var _default = createConnectedFieldArray;
222exports["default"] = _default;
\No newline at end of file