Field.js 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614
  1. import _extends from "@babel/runtime/helpers/esm/extends";
  2. import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
  3. import _regeneratorRuntime from "@babel/runtime/helpers/esm/regeneratorRuntime";
  4. import _asyncToGenerator from "@babel/runtime/helpers/esm/asyncToGenerator";
  5. import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
  6. import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray";
  7. import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
  8. import _createClass from "@babel/runtime/helpers/esm/createClass";
  9. import _assertThisInitialized from "@babel/runtime/helpers/esm/assertThisInitialized";
  10. import _inherits from "@babel/runtime/helpers/esm/inherits";
  11. import _createSuper from "@babel/runtime/helpers/esm/createSuper";
  12. import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
  13. var _excluded = ["name"];
  14. import toChildrenArray from "rc-util/es/Children/toArray";
  15. import isEqual from "rc-util/es/isEqual";
  16. import warning from "rc-util/es/warning";
  17. import * as React from 'react';
  18. import FieldContext, { HOOK_MARK } from "./FieldContext";
  19. import ListContext from "./ListContext";
  20. import { toArray } from "./utils/typeUtil";
  21. import { validateRules } from "./utils/validateUtil";
  22. import { containsNamePath, defaultGetValueFromEvent, getNamePath, getValue } from "./utils/valueUtil";
  23. var EMPTY_ERRORS = [];
  24. function requireUpdate(shouldUpdate, prev, next, prevValue, nextValue, info) {
  25. if (typeof shouldUpdate === 'function') {
  26. return shouldUpdate(prev, next, 'source' in info ? {
  27. source: info.source
  28. } : {});
  29. }
  30. return prevValue !== nextValue;
  31. }
  32. // eslint-disable-next-line @typescript-eslint/consistent-indexed-object-style
  33. // We use Class instead of Hooks here since it will cost much code by using Hooks.
  34. var Field = /*#__PURE__*/function (_React$Component) {
  35. _inherits(Field, _React$Component);
  36. var _super = _createSuper(Field);
  37. // ============================== Subscriptions ==============================
  38. function Field(props) {
  39. var _this;
  40. _classCallCheck(this, Field);
  41. _this = _super.call(this, props);
  42. // Register on init
  43. _defineProperty(_assertThisInitialized(_this), "state", {
  44. resetCount: 0
  45. });
  46. _defineProperty(_assertThisInitialized(_this), "cancelRegisterFunc", null);
  47. _defineProperty(_assertThisInitialized(_this), "mounted", false);
  48. /**
  49. * Follow state should not management in State since it will async update by React.
  50. * This makes first render of form can not get correct state value.
  51. */
  52. _defineProperty(_assertThisInitialized(_this), "touched", false);
  53. /**
  54. * Mark when touched & validated. Currently only used for `dependencies`.
  55. * Note that we do not think field with `initialValue` is dirty
  56. * but this will be by `isFieldDirty` func.
  57. */
  58. _defineProperty(_assertThisInitialized(_this), "dirty", false);
  59. _defineProperty(_assertThisInitialized(_this), "validatePromise", void 0);
  60. _defineProperty(_assertThisInitialized(_this), "prevValidating", void 0);
  61. _defineProperty(_assertThisInitialized(_this), "errors", EMPTY_ERRORS);
  62. _defineProperty(_assertThisInitialized(_this), "warnings", EMPTY_ERRORS);
  63. _defineProperty(_assertThisInitialized(_this), "cancelRegister", function () {
  64. var _this$props = _this.props,
  65. preserve = _this$props.preserve,
  66. isListField = _this$props.isListField,
  67. name = _this$props.name;
  68. if (_this.cancelRegisterFunc) {
  69. _this.cancelRegisterFunc(isListField, preserve, getNamePath(name));
  70. }
  71. _this.cancelRegisterFunc = null;
  72. });
  73. // ================================== Utils ==================================
  74. _defineProperty(_assertThisInitialized(_this), "getNamePath", function () {
  75. var _this$props2 = _this.props,
  76. name = _this$props2.name,
  77. fieldContext = _this$props2.fieldContext;
  78. var _fieldContext$prefixN = fieldContext.prefixName,
  79. prefixName = _fieldContext$prefixN === void 0 ? [] : _fieldContext$prefixN;
  80. return name !== undefined ? [].concat(_toConsumableArray(prefixName), _toConsumableArray(name)) : [];
  81. });
  82. _defineProperty(_assertThisInitialized(_this), "getRules", function () {
  83. var _this$props3 = _this.props,
  84. _this$props3$rules = _this$props3.rules,
  85. rules = _this$props3$rules === void 0 ? [] : _this$props3$rules,
  86. fieldContext = _this$props3.fieldContext;
  87. return rules.map(function (rule) {
  88. if (typeof rule === 'function') {
  89. return rule(fieldContext);
  90. }
  91. return rule;
  92. });
  93. });
  94. _defineProperty(_assertThisInitialized(_this), "refresh", function () {
  95. if (!_this.mounted) return;
  96. /**
  97. * Clean up current node.
  98. */
  99. _this.setState(function (_ref) {
  100. var resetCount = _ref.resetCount;
  101. return {
  102. resetCount: resetCount + 1
  103. };
  104. });
  105. });
  106. // Event should only trigger when meta changed
  107. _defineProperty(_assertThisInitialized(_this), "metaCache", null);
  108. _defineProperty(_assertThisInitialized(_this), "triggerMetaEvent", function (destroy) {
  109. var onMetaChange = _this.props.onMetaChange;
  110. if (onMetaChange) {
  111. var _meta = _objectSpread(_objectSpread({}, _this.getMeta()), {}, {
  112. destroy: destroy
  113. });
  114. if (!isEqual(_this.metaCache, _meta)) {
  115. onMetaChange(_meta);
  116. }
  117. _this.metaCache = _meta;
  118. } else {
  119. _this.metaCache = null;
  120. }
  121. });
  122. // ========================= Field Entity Interfaces =========================
  123. // Trigger by store update. Check if need update the component
  124. _defineProperty(_assertThisInitialized(_this), "onStoreChange", function (prevStore, namePathList, info) {
  125. var _this$props4 = _this.props,
  126. shouldUpdate = _this$props4.shouldUpdate,
  127. _this$props4$dependen = _this$props4.dependencies,
  128. dependencies = _this$props4$dependen === void 0 ? [] : _this$props4$dependen,
  129. onReset = _this$props4.onReset;
  130. var store = info.store;
  131. var namePath = _this.getNamePath();
  132. var prevValue = _this.getValue(prevStore);
  133. var curValue = _this.getValue(store);
  134. var namePathMatch = namePathList && containsNamePath(namePathList, namePath);
  135. // `setFieldsValue` is a quick access to update related status
  136. if (info.type === 'valueUpdate' && info.source === 'external' && !isEqual(prevValue, curValue)) {
  137. _this.touched = true;
  138. _this.dirty = true;
  139. _this.validatePromise = null;
  140. _this.errors = EMPTY_ERRORS;
  141. _this.warnings = EMPTY_ERRORS;
  142. _this.triggerMetaEvent();
  143. }
  144. switch (info.type) {
  145. case 'reset':
  146. if (!namePathList || namePathMatch) {
  147. // Clean up state
  148. _this.touched = false;
  149. _this.dirty = false;
  150. _this.validatePromise = undefined;
  151. _this.errors = EMPTY_ERRORS;
  152. _this.warnings = EMPTY_ERRORS;
  153. _this.triggerMetaEvent();
  154. onReset === null || onReset === void 0 || onReset();
  155. _this.refresh();
  156. return;
  157. }
  158. break;
  159. /**
  160. * In case field with `preserve = false` nest deps like:
  161. * - A = 1 => show B
  162. * - B = 1 => show C
  163. * - Reset A, need clean B, C
  164. */
  165. case 'remove':
  166. {
  167. if (shouldUpdate && requireUpdate(shouldUpdate, prevStore, store, prevValue, curValue, info)) {
  168. _this.reRender();
  169. return;
  170. }
  171. break;
  172. }
  173. case 'setField':
  174. {
  175. var data = info.data;
  176. if (namePathMatch) {
  177. if ('touched' in data) {
  178. _this.touched = data.touched;
  179. }
  180. if ('validating' in data && !('originRCField' in data)) {
  181. _this.validatePromise = data.validating ? Promise.resolve([]) : null;
  182. }
  183. if ('errors' in data) {
  184. _this.errors = data.errors || EMPTY_ERRORS;
  185. }
  186. if ('warnings' in data) {
  187. _this.warnings = data.warnings || EMPTY_ERRORS;
  188. }
  189. _this.dirty = true;
  190. _this.triggerMetaEvent();
  191. _this.reRender();
  192. return;
  193. } else if ('value' in data && containsNamePath(namePathList, namePath, true)) {
  194. // Contains path with value should also check
  195. _this.reRender();
  196. return;
  197. }
  198. // Handle update by `setField` with `shouldUpdate`
  199. if (shouldUpdate && !namePath.length && requireUpdate(shouldUpdate, prevStore, store, prevValue, curValue, info)) {
  200. _this.reRender();
  201. return;
  202. }
  203. break;
  204. }
  205. case 'dependenciesUpdate':
  206. {
  207. /**
  208. * Trigger when marked `dependencies` updated. Related fields will all update
  209. */
  210. var dependencyList = dependencies.map(getNamePath);
  211. // No need for `namePathMath` check and `shouldUpdate` check, since `valueUpdate` will be
  212. // emitted earlier and they will work there
  213. // If set it may cause unnecessary twice rerendering
  214. if (dependencyList.some(function (dependency) {
  215. return containsNamePath(info.relatedFields, dependency);
  216. })) {
  217. _this.reRender();
  218. return;
  219. }
  220. break;
  221. }
  222. default:
  223. // 1. If `namePath` exists in `namePathList`, means it's related value and should update
  224. // For example <List name="list"><Field name={['list', 0]}></List>
  225. // If `namePathList` is [['list']] (List value update), Field should be updated
  226. // If `namePathList` is [['list', 0]] (Field value update), List shouldn't be updated
  227. // 2.
  228. // 2.1 If `dependencies` is set, `name` is not set and `shouldUpdate` is not set,
  229. // don't use `shouldUpdate`. `dependencies` is view as a shortcut if `shouldUpdate`
  230. // is not provided
  231. // 2.2 If `shouldUpdate` provided, use customize logic to update the field
  232. // else to check if value changed
  233. if (namePathMatch || (!dependencies.length || namePath.length || shouldUpdate) && requireUpdate(shouldUpdate, prevStore, store, prevValue, curValue, info)) {
  234. _this.reRender();
  235. return;
  236. }
  237. break;
  238. }
  239. if (shouldUpdate === true) {
  240. _this.reRender();
  241. }
  242. });
  243. _defineProperty(_assertThisInitialized(_this), "validateRules", function (options) {
  244. // We should fixed namePath & value to avoid developer change then by form function
  245. var namePath = _this.getNamePath();
  246. var currentValue = _this.getValue();
  247. var _ref2 = options || {},
  248. triggerName = _ref2.triggerName,
  249. _ref2$validateOnly = _ref2.validateOnly,
  250. validateOnly = _ref2$validateOnly === void 0 ? false : _ref2$validateOnly;
  251. // Force change to async to avoid rule OOD under renderProps field
  252. var rootPromise = Promise.resolve().then( /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee() {
  253. var _this$props5, _this$props5$validate, validateFirst, messageVariables, validateDebounce, filteredRules, promise;
  254. return _regeneratorRuntime().wrap(function _callee$(_context) {
  255. while (1) switch (_context.prev = _context.next) {
  256. case 0:
  257. if (_this.mounted) {
  258. _context.next = 2;
  259. break;
  260. }
  261. return _context.abrupt("return", []);
  262. case 2:
  263. _this$props5 = _this.props, _this$props5$validate = _this$props5.validateFirst, validateFirst = _this$props5$validate === void 0 ? false : _this$props5$validate, messageVariables = _this$props5.messageVariables, validateDebounce = _this$props5.validateDebounce; // Start validate
  264. filteredRules = _this.getRules();
  265. if (triggerName) {
  266. filteredRules = filteredRules.filter(function (rule) {
  267. return rule;
  268. }).filter(function (rule) {
  269. var validateTrigger = rule.validateTrigger;
  270. if (!validateTrigger) {
  271. return true;
  272. }
  273. var triggerList = toArray(validateTrigger);
  274. return triggerList.includes(triggerName);
  275. });
  276. }
  277. // Wait for debounce. Skip if no `triggerName` since its from `validateFields / submit`
  278. if (!(validateDebounce && triggerName)) {
  279. _context.next = 10;
  280. break;
  281. }
  282. _context.next = 8;
  283. return new Promise(function (resolve) {
  284. setTimeout(resolve, validateDebounce);
  285. });
  286. case 8:
  287. if (!(_this.validatePromise !== rootPromise)) {
  288. _context.next = 10;
  289. break;
  290. }
  291. return _context.abrupt("return", []);
  292. case 10:
  293. promise = validateRules(namePath, currentValue, filteredRules, options, validateFirst, messageVariables);
  294. promise.catch(function (e) {
  295. return e;
  296. }).then(function () {
  297. var ruleErrors = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : EMPTY_ERRORS;
  298. if (_this.validatePromise === rootPromise) {
  299. var _ruleErrors$forEach;
  300. _this.validatePromise = null;
  301. // Get errors & warnings
  302. var nextErrors = [];
  303. var nextWarnings = [];
  304. (_ruleErrors$forEach = ruleErrors.forEach) === null || _ruleErrors$forEach === void 0 || _ruleErrors$forEach.call(ruleErrors, function (_ref4) {
  305. var warningOnly = _ref4.rule.warningOnly,
  306. _ref4$errors = _ref4.errors,
  307. errors = _ref4$errors === void 0 ? EMPTY_ERRORS : _ref4$errors;
  308. if (warningOnly) {
  309. nextWarnings.push.apply(nextWarnings, _toConsumableArray(errors));
  310. } else {
  311. nextErrors.push.apply(nextErrors, _toConsumableArray(errors));
  312. }
  313. });
  314. _this.errors = nextErrors;
  315. _this.warnings = nextWarnings;
  316. _this.triggerMetaEvent();
  317. _this.reRender();
  318. }
  319. });
  320. return _context.abrupt("return", promise);
  321. case 13:
  322. case "end":
  323. return _context.stop();
  324. }
  325. }, _callee);
  326. })));
  327. if (validateOnly) {
  328. return rootPromise;
  329. }
  330. _this.validatePromise = rootPromise;
  331. _this.dirty = true;
  332. _this.errors = EMPTY_ERRORS;
  333. _this.warnings = EMPTY_ERRORS;
  334. _this.triggerMetaEvent();
  335. // Force trigger re-render since we need sync renderProps with new meta
  336. _this.reRender();
  337. return rootPromise;
  338. });
  339. _defineProperty(_assertThisInitialized(_this), "isFieldValidating", function () {
  340. return !!_this.validatePromise;
  341. });
  342. _defineProperty(_assertThisInitialized(_this), "isFieldTouched", function () {
  343. return _this.touched;
  344. });
  345. _defineProperty(_assertThisInitialized(_this), "isFieldDirty", function () {
  346. // Touched or validate or has initialValue
  347. if (_this.dirty || _this.props.initialValue !== undefined) {
  348. return true;
  349. }
  350. // Form set initialValue
  351. var fieldContext = _this.props.fieldContext;
  352. var _fieldContext$getInte = fieldContext.getInternalHooks(HOOK_MARK),
  353. getInitialValue = _fieldContext$getInte.getInitialValue;
  354. if (getInitialValue(_this.getNamePath()) !== undefined) {
  355. return true;
  356. }
  357. return false;
  358. });
  359. _defineProperty(_assertThisInitialized(_this), "getErrors", function () {
  360. return _this.errors;
  361. });
  362. _defineProperty(_assertThisInitialized(_this), "getWarnings", function () {
  363. return _this.warnings;
  364. });
  365. _defineProperty(_assertThisInitialized(_this), "isListField", function () {
  366. return _this.props.isListField;
  367. });
  368. _defineProperty(_assertThisInitialized(_this), "isList", function () {
  369. return _this.props.isList;
  370. });
  371. _defineProperty(_assertThisInitialized(_this), "isPreserve", function () {
  372. return _this.props.preserve;
  373. });
  374. // ============================= Child Component =============================
  375. _defineProperty(_assertThisInitialized(_this), "getMeta", function () {
  376. // Make error & validating in cache to save perf
  377. _this.prevValidating = _this.isFieldValidating();
  378. var meta = {
  379. touched: _this.isFieldTouched(),
  380. validating: _this.prevValidating,
  381. errors: _this.errors,
  382. warnings: _this.warnings,
  383. name: _this.getNamePath(),
  384. validated: _this.validatePromise === null
  385. };
  386. return meta;
  387. });
  388. // Only return validate child node. If invalidate, will do nothing about field.
  389. _defineProperty(_assertThisInitialized(_this), "getOnlyChild", function (children) {
  390. // Support render props
  391. if (typeof children === 'function') {
  392. var _meta2 = _this.getMeta();
  393. return _objectSpread(_objectSpread({}, _this.getOnlyChild(children(_this.getControlled(), _meta2, _this.props.fieldContext))), {}, {
  394. isFunction: true
  395. });
  396. }
  397. // Filed element only
  398. var childList = toChildrenArray(children);
  399. if (childList.length !== 1 || ! /*#__PURE__*/React.isValidElement(childList[0])) {
  400. return {
  401. child: childList,
  402. isFunction: false
  403. };
  404. }
  405. return {
  406. child: childList[0],
  407. isFunction: false
  408. };
  409. });
  410. // ============================== Field Control ==============================
  411. _defineProperty(_assertThisInitialized(_this), "getValue", function (store) {
  412. var getFieldsValue = _this.props.fieldContext.getFieldsValue;
  413. var namePath = _this.getNamePath();
  414. return getValue(store || getFieldsValue(true), namePath);
  415. });
  416. _defineProperty(_assertThisInitialized(_this), "getControlled", function () {
  417. var childProps = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  418. var _this$props6 = _this.props,
  419. name = _this$props6.name,
  420. trigger = _this$props6.trigger,
  421. validateTrigger = _this$props6.validateTrigger,
  422. getValueFromEvent = _this$props6.getValueFromEvent,
  423. normalize = _this$props6.normalize,
  424. valuePropName = _this$props6.valuePropName,
  425. getValueProps = _this$props6.getValueProps,
  426. fieldContext = _this$props6.fieldContext;
  427. var mergedValidateTrigger = validateTrigger !== undefined ? validateTrigger : fieldContext.validateTrigger;
  428. var namePath = _this.getNamePath();
  429. var getInternalHooks = fieldContext.getInternalHooks,
  430. getFieldsValue = fieldContext.getFieldsValue;
  431. var _getInternalHooks = getInternalHooks(HOOK_MARK),
  432. dispatch = _getInternalHooks.dispatch;
  433. var value = _this.getValue();
  434. var mergedGetValueProps = getValueProps || function (val) {
  435. return _defineProperty({}, valuePropName, val);
  436. };
  437. var originTriggerFunc = childProps[trigger];
  438. var valueProps = name !== undefined ? mergedGetValueProps(value) : {};
  439. // warning when prop value is function
  440. if (process.env.NODE_ENV !== 'production' && valueProps) {
  441. Object.keys(valueProps).forEach(function (key) {
  442. warning(typeof valueProps[key] !== 'function', "It's not recommended to generate dynamic function prop by `getValueProps`. Please pass it to child component directly (prop: ".concat(key, ")"));
  443. });
  444. }
  445. var control = _objectSpread(_objectSpread({}, childProps), valueProps);
  446. // Add trigger
  447. control[trigger] = function () {
  448. // Mark as touched
  449. _this.touched = true;
  450. _this.dirty = true;
  451. _this.triggerMetaEvent();
  452. var newValue;
  453. for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
  454. args[_key] = arguments[_key];
  455. }
  456. if (getValueFromEvent) {
  457. newValue = getValueFromEvent.apply(void 0, args);
  458. } else {
  459. newValue = defaultGetValueFromEvent.apply(void 0, [valuePropName].concat(args));
  460. }
  461. if (normalize) {
  462. newValue = normalize(newValue, value, getFieldsValue(true));
  463. }
  464. if (newValue !== value) {
  465. dispatch({
  466. type: 'updateValue',
  467. namePath: namePath,
  468. value: newValue
  469. });
  470. }
  471. if (originTriggerFunc) {
  472. originTriggerFunc.apply(void 0, args);
  473. }
  474. };
  475. // Add validateTrigger
  476. var validateTriggerList = toArray(mergedValidateTrigger || []);
  477. validateTriggerList.forEach(function (triggerName) {
  478. // Wrap additional function of component, so that we can get latest value from store
  479. var originTrigger = control[triggerName];
  480. control[triggerName] = function () {
  481. if (originTrigger) {
  482. originTrigger.apply(void 0, arguments);
  483. }
  484. // Always use latest rules
  485. var rules = _this.props.rules;
  486. if (rules && rules.length) {
  487. // We dispatch validate to root,
  488. // since it will update related data with other field with same name
  489. dispatch({
  490. type: 'validateField',
  491. namePath: namePath,
  492. triggerName: triggerName
  493. });
  494. }
  495. };
  496. });
  497. return control;
  498. });
  499. if (props.fieldContext) {
  500. var getInternalHooks = props.fieldContext.getInternalHooks;
  501. var _getInternalHooks2 = getInternalHooks(HOOK_MARK),
  502. initEntityValue = _getInternalHooks2.initEntityValue;
  503. initEntityValue(_assertThisInitialized(_this));
  504. }
  505. return _this;
  506. }
  507. _createClass(Field, [{
  508. key: "componentDidMount",
  509. value: function componentDidMount() {
  510. var _this$props7 = this.props,
  511. shouldUpdate = _this$props7.shouldUpdate,
  512. fieldContext = _this$props7.fieldContext;
  513. this.mounted = true;
  514. // Register on init
  515. if (fieldContext) {
  516. var getInternalHooks = fieldContext.getInternalHooks;
  517. var _getInternalHooks3 = getInternalHooks(HOOK_MARK),
  518. registerField = _getInternalHooks3.registerField;
  519. this.cancelRegisterFunc = registerField(this);
  520. }
  521. // One more render for component in case fields not ready
  522. if (shouldUpdate === true) {
  523. this.reRender();
  524. }
  525. }
  526. }, {
  527. key: "componentWillUnmount",
  528. value: function componentWillUnmount() {
  529. this.cancelRegister();
  530. this.triggerMetaEvent(true);
  531. this.mounted = false;
  532. }
  533. }, {
  534. key: "reRender",
  535. value: function reRender() {
  536. if (!this.mounted) return;
  537. this.forceUpdate();
  538. }
  539. }, {
  540. key: "render",
  541. value: function render() {
  542. var resetCount = this.state.resetCount;
  543. var children = this.props.children;
  544. var _this$getOnlyChild = this.getOnlyChild(children),
  545. child = _this$getOnlyChild.child,
  546. isFunction = _this$getOnlyChild.isFunction;
  547. // Not need to `cloneElement` since user can handle this in render function self
  548. var returnChildNode;
  549. if (isFunction) {
  550. returnChildNode = child;
  551. } else if ( /*#__PURE__*/React.isValidElement(child)) {
  552. returnChildNode = /*#__PURE__*/React.cloneElement(child, this.getControlled(child.props));
  553. } else {
  554. warning(!child, '`children` of Field is not validate ReactElement.');
  555. returnChildNode = child;
  556. }
  557. return /*#__PURE__*/React.createElement(React.Fragment, {
  558. key: resetCount
  559. }, returnChildNode);
  560. }
  561. }]);
  562. return Field;
  563. }(React.Component);
  564. _defineProperty(Field, "contextType", FieldContext);
  565. _defineProperty(Field, "defaultProps", {
  566. trigger: 'onChange',
  567. valuePropName: 'value'
  568. });
  569. function WrapperField(_ref6) {
  570. var _restProps$isListFiel;
  571. var name = _ref6.name,
  572. restProps = _objectWithoutProperties(_ref6, _excluded);
  573. var fieldContext = React.useContext(FieldContext);
  574. var listContext = React.useContext(ListContext);
  575. var namePath = name !== undefined ? getNamePath(name) : undefined;
  576. var isMergedListField = (_restProps$isListFiel = restProps.isListField) !== null && _restProps$isListFiel !== void 0 ? _restProps$isListFiel : !!listContext;
  577. var key = 'keep';
  578. if (!isMergedListField) {
  579. key = "_".concat((namePath || []).join('_'));
  580. }
  581. // Warning if it's a directly list field.
  582. // We can still support multiple level field preserve.
  583. if (process.env.NODE_ENV !== 'production' && restProps.preserve === false && isMergedListField && namePath.length <= 1) {
  584. warning(false, '`preserve` should not apply on Form.List fields.');
  585. }
  586. return /*#__PURE__*/React.createElement(Field, _extends({
  587. key: key,
  588. name: namePath,
  589. isListField: isMergedListField
  590. }, restProps, {
  591. fieldContext: fieldContext
  592. }));
  593. }
  594. export default WrapperField;