FormItem.js
12.9 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
import _typeof from "@babel/runtime/helpers/typeof";
import _toConsumableArray from "@babel/runtime/helpers/toConsumableArray";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
import _extends from "@babel/runtime/helpers/extends";
import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
var __rest = this && this.__rest || function (s, e) {
var t = {};
for (var p in s) {
if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p];
}
if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]];
}
return t;
};
import * as React from 'react';
import { useContext, useRef } from 'react';
import isEqual from 'lodash/isEqual';
import classNames from 'classnames';
import { Field } from 'rc-field-form';
import FieldContext from "rc-field-form/es/FieldContext";
import { supportRef } from "rc-util/es/ref";
import omit from 'omit.js';
import Row from '../grid/row';
import { ConfigContext } from '../config-provider';
import { tuple } from '../_util/type';
import devWarning from '../_util/devWarning';
import FormItemLabel from './FormItemLabel';
import FormItemInput from './FormItemInput';
import { FormContext, FormItemContext } from './context';
import { toArray, getFieldId } from './util';
import { cloneElement, isValidElement } from '../_util/reactNode';
import useFrameState from './hooks/useFrameState';
import useItemRef from './hooks/useItemRef';
var ValidateStatuses = tuple('success', 'warning', 'error', 'validating', '');
var MemoInput = /*#__PURE__*/React.memo(function (_ref) {
var children = _ref.children;
return children;
}, function (prev, next) {
return prev.value === next.value && prev.update === next.update;
});
function hasValidName(name) {
if (name === null) {
devWarning(false, 'Form.Item', '`null` is passed as `name` property');
}
return !(name === undefined || name === null);
}
function FormItem(props) {
var name = props.name,
fieldKey = props.fieldKey,
noStyle = props.noStyle,
dependencies = props.dependencies,
customizePrefixCls = props.prefixCls,
style = props.style,
className = props.className,
shouldUpdate = props.shouldUpdate,
hasFeedback = props.hasFeedback,
help = props.help,
rules = props.rules,
validateStatus = props.validateStatus,
children = props.children,
required = props.required,
label = props.label,
messageVariables = props.messageVariables,
_props$trigger = props.trigger,
trigger = _props$trigger === void 0 ? 'onChange' : _props$trigger,
validateTrigger = props.validateTrigger,
hidden = props.hidden,
restProps = __rest(props, ["name", "fieldKey", "noStyle", "dependencies", "prefixCls", "style", "className", "shouldUpdate", "hasFeedback", "help", "rules", "validateStatus", "children", "required", "label", "messageVariables", "trigger", "validateTrigger", "hidden"]);
var destroyRef = useRef(false);
var _useContext = useContext(ConfigContext),
getPrefixCls = _useContext.getPrefixCls;
var _useContext2 = useContext(FormContext),
formName = _useContext2.name,
requiredMark = _useContext2.requiredMark;
var _useContext3 = useContext(FormItemContext),
updateItemErrors = _useContext3.updateItemErrors;
var _React$useState = React.useState(!!help),
_React$useState2 = _slicedToArray(_React$useState, 2),
domErrorVisible = _React$useState2[0],
innerSetDomErrorVisible = _React$useState2[1];
var _useFrameState = useFrameState({}),
_useFrameState2 = _slicedToArray(_useFrameState, 2),
inlineErrors = _useFrameState2[0],
setInlineErrors = _useFrameState2[1];
var _useContext4 = useContext(FieldContext),
contextValidateTrigger = _useContext4.validateTrigger;
var mergedValidateTrigger = validateTrigger !== undefined ? validateTrigger : contextValidateTrigger;
function setDomErrorVisible(visible) {
if (!destroyRef.current) {
innerSetDomErrorVisible(visible);
}
}
var hasName = hasValidName(name); // Cache Field NamePath
var nameRef = useRef([]); // Should clean up if Field removed
React.useEffect(function () {
return function () {
destroyRef.current = true;
updateItemErrors(nameRef.current.join('__SPLIT__'), []);
};
}, []);
var prefixCls = getPrefixCls('form', customizePrefixCls); // ======================== Errors ========================
// Collect noStyle Field error to the top FormItem
var updateChildItemErrors = noStyle ? updateItemErrors : function (subName, subErrors) {
setInlineErrors(function () {
var prevInlineErrors = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
if (!isEqual(prevInlineErrors[subName], subErrors)) {
return _extends(_extends({}, prevInlineErrors), _defineProperty({}, subName, subErrors));
}
return prevInlineErrors;
});
}; // ===================== Children Ref =====================
var getItemRef = useItemRef();
function renderLayout(baseChildren, fieldId, meta, isRequired) {
var _itemClassName;
var _a;
if (noStyle && !hidden) {
return baseChildren;
} // ======================== Errors ========================
// >>> collect sub errors
var subErrorList = [];
Object.keys(inlineErrors).forEach(function (subName) {
subErrorList = [].concat(_toConsumableArray(subErrorList), _toConsumableArray(inlineErrors[subName] || []));
}); // >>> merged errors
var mergedErrors;
if (help !== undefined && help !== null) {
mergedErrors = toArray(help);
} else {
mergedErrors = meta ? meta.errors : [];
mergedErrors = [].concat(_toConsumableArray(mergedErrors), _toConsumableArray(subErrorList));
} // ======================== Status ========================
var mergedValidateStatus = '';
if (validateStatus !== undefined) {
mergedValidateStatus = validateStatus;
} else if (meta === null || meta === void 0 ? void 0 : meta.validating) {
mergedValidateStatus = 'validating';
} else if (((_a = meta === null || meta === void 0 ? void 0 : meta.errors) === null || _a === void 0 ? void 0 : _a.length) || subErrorList.length) {
mergedValidateStatus = 'error';
} else if (meta === null || meta === void 0 ? void 0 : meta.touched) {
mergedValidateStatus = 'success';
}
var itemClassName = (_itemClassName = {}, _defineProperty(_itemClassName, "".concat(prefixCls, "-item"), true), _defineProperty(_itemClassName, "".concat(prefixCls, "-item-with-help"), domErrorVisible || help), _defineProperty(_itemClassName, "".concat(className), !!className), _defineProperty(_itemClassName, "".concat(prefixCls, "-item-has-feedback"), mergedValidateStatus && hasFeedback), _defineProperty(_itemClassName, "".concat(prefixCls, "-item-has-success"), mergedValidateStatus === 'success'), _defineProperty(_itemClassName, "".concat(prefixCls, "-item-has-warning"), mergedValidateStatus === 'warning'), _defineProperty(_itemClassName, "".concat(prefixCls, "-item-has-error"), mergedValidateStatus === 'error'), _defineProperty(_itemClassName, "".concat(prefixCls, "-item-is-validating"), mergedValidateStatus === 'validating'), _defineProperty(_itemClassName, "".concat(prefixCls, "-item-hidden"), hidden), _itemClassName); // ======================= Children =======================
return /*#__PURE__*/React.createElement(Row, _extends({
className: classNames(itemClassName),
style: style,
key: "row"
}, omit(restProps, ['colon', 'extra', 'getValueFromEvent', 'getValueProps', 'hasFeedback', 'help', 'htmlFor', 'id', 'initialValue', 'isListField', 'label', 'labelAlign', 'labelCol', 'normalize', 'preserve', 'required', 'tooltip', 'validateFirst', 'validateStatus', 'valuePropName', 'wrapperCol', '_internalItemRender'])), /*#__PURE__*/React.createElement(FormItemLabel, _extends({
htmlFor: fieldId,
required: isRequired,
requiredMark: requiredMark
}, props, {
prefixCls: prefixCls
})), /*#__PURE__*/React.createElement(FormItemInput, _extends({}, props, meta, {
errors: mergedErrors,
prefixCls: prefixCls,
status: mergedValidateStatus,
onDomErrorVisibleChange: setDomErrorVisible,
validateStatus: mergedValidateStatus
}), /*#__PURE__*/React.createElement(FormItemContext.Provider, {
value: {
updateItemErrors: updateChildItemErrors
}
}, baseChildren)));
}
var isRenderProps = typeof children === 'function'; // Record for real component render
var updateRef = useRef(0);
updateRef.current += 1;
if (!hasName && !isRenderProps && !dependencies) {
return renderLayout(children);
}
var variables = {};
if (typeof label === 'string') {
variables.label = label;
}
if (messageVariables) {
variables = _extends(_extends({}, variables), messageVariables);
}
return /*#__PURE__*/React.createElement(Field, _extends({}, props, {
messageVariables: variables,
trigger: trigger,
validateTrigger: mergedValidateTrigger,
onReset: function onReset() {
setDomErrorVisible(false);
}
}), function (control, meta, context) {
var errors = meta.errors;
var mergedName = toArray(name).length && meta ? meta.name : [];
var fieldId = getFieldId(mergedName, formName);
if (noStyle) {
nameRef.current = _toConsumableArray(mergedName);
if (fieldKey) {
var fieldKeys = Array.isArray(fieldKey) ? fieldKey : [fieldKey];
nameRef.current = [].concat(_toConsumableArray(mergedName.slice(0, -1)), _toConsumableArray(fieldKeys));
}
updateItemErrors(nameRef.current.join('__SPLIT__'), errors);
}
var isRequired = required !== undefined ? required : !!(rules && rules.some(function (rule) {
if (rule && _typeof(rule) === 'object' && rule.required) {
return true;
}
if (typeof rule === 'function') {
var ruleEntity = rule(context);
return ruleEntity && ruleEntity.required;
}
return false;
})); // ======================= Children =======================
var mergedControl = _extends({}, control);
var childNode = null;
devWarning(!(shouldUpdate && dependencies), 'Form.Item', "`shouldUpdate` and `dependencies` shouldn't be used together. See https://ant.design/components/form/#dependencies.");
if (Array.isArray(children) && hasName) {
devWarning(false, 'Form.Item', '`children` is array of render props cannot have `name`.');
childNode = children;
} else if (isRenderProps && (!(shouldUpdate || dependencies) || hasName)) {
devWarning(!!(shouldUpdate || dependencies), 'Form.Item', '`children` of render props only work with `shouldUpdate` or `dependencies`.');
devWarning(!hasName, 'Form.Item', "Do not use `name` with `children` of render props since it's not a field.");
} else if (dependencies && !isRenderProps && !hasName) {
devWarning(false, 'Form.Item', 'Must set `name` or use render props when `dependencies` is set.');
} else if (isValidElement(children)) {
devWarning(children.props.defaultValue === undefined, 'Form.Item', '`defaultValue` will not work on controlled Field. You should use `initialValues` of Form instead.');
var childProps = _extends(_extends({}, children.props), mergedControl);
if (!childProps.id) {
childProps.id = fieldId;
}
if (supportRef(children)) {
childProps.ref = getItemRef(mergedName, children);
} // We should keep user origin event handler
var triggers = new Set([].concat(_toConsumableArray(toArray(trigger)), _toConsumableArray(toArray(mergedValidateTrigger))));
triggers.forEach(function (eventName) {
childProps[eventName] = function () {
var _a2, _c2;
var _a, _b, _c;
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
(_a = mergedControl[eventName]) === null || _a === void 0 ? void 0 : (_a2 = _a).call.apply(_a2, [mergedControl].concat(args));
(_c = (_b = children.props)[eventName]) === null || _c === void 0 ? void 0 : (_c2 = _c).call.apply(_c2, [_b].concat(args));
};
});
childNode = /*#__PURE__*/React.createElement(MemoInput, {
value: mergedControl[props.valuePropName || 'value'],
update: updateRef.current
}, cloneElement(children, childProps));
} else if (isRenderProps && (shouldUpdate || dependencies) && !hasName) {
childNode = children(context);
} else {
devWarning(!mergedName.length, 'Form.Item', '`name` is only used for validate React element. If you are using Form.Item as layout display, please remove `name` instead.');
childNode = children;
}
return renderLayout(childNode, fieldId, meta, isRequired);
});
}
export default FormItem;