@nutui/nutui-react 3.0.0 → 3.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (207) hide show
  1. package/CHANGELOG.md +24 -0
  2. package/dist/cjs/hooks/use-client-rect.js +1 -0
  3. package/dist/cjs/packages/actionsheet/style/style.css +12 -0
  4. package/dist/cjs/packages/address/style/style.css +12 -0
  5. package/dist/cjs/packages/calendar/style/style.css +12 -0
  6. package/dist/cjs/packages/cascader/style/style.css +12 -0
  7. package/dist/cjs/packages/configprovider/types.d.ts +1 -1
  8. package/dist/cjs/packages/datepicker/datepicker.js +8 -6
  9. package/dist/cjs/packages/datepicker/style/style.css +82 -0
  10. package/dist/cjs/packages/datepickerview/datepickerview.d.ts +3 -0
  11. package/dist/cjs/packages/datepickerview/datepickerview.js +118 -0
  12. package/dist/cjs/packages/datepickerview/index.d.ts +3 -0
  13. package/dist/cjs/packages/{subsidenavbar → datepickerview}/index.js +2 -2
  14. package/dist/cjs/packages/datepickerview/style/datepickerview.scss +4 -0
  15. package/dist/cjs/packages/datepickerview/style/index.js +2 -0
  16. package/dist/cjs/packages/datepickerview/style/style.css +78 -0
  17. package/dist/cjs/packages/datepickerview/types.d.ts +15 -0
  18. package/dist/cjs/packages/datepickerview/types.js +4 -0
  19. package/dist/cjs/packages/dialog/dialogwrap.js +3 -0
  20. package/dist/cjs/packages/dialog/style/dialog.scss +0 -1
  21. package/dist/cjs/packages/dialog/style/style.css +0 -1
  22. package/dist/cjs/packages/drag/style/drag.scss +1 -0
  23. package/dist/cjs/packages/drag/style/style.css +1 -0
  24. package/dist/cjs/packages/fixednav/fixednav.js +20 -14
  25. package/dist/cjs/packages/fixednav/style/fixednav.scss +0 -15
  26. package/dist/cjs/packages/fixednav/style/style.css +0 -14
  27. package/dist/cjs/packages/form/__tests__/merge.spec.d.ts +1 -0
  28. package/dist/cjs/packages/form/__tests__/merge.spec.js +545 -0
  29. package/dist/cjs/packages/form/form.d.ts +2 -0
  30. package/dist/cjs/packages/form/form.js +9 -2
  31. package/dist/cjs/packages/form/index.d.ts +2 -1
  32. package/dist/cjs/packages/form/index.js +1 -0
  33. package/dist/cjs/packages/form/style/form.scss +0 -66
  34. package/dist/cjs/packages/form/style/style.css +31 -21
  35. package/dist/cjs/packages/form/types.d.ts +2 -1
  36. package/dist/cjs/packages/form/useform.d.ts +2 -1
  37. package/dist/cjs/packages/form/useform.js +342 -278
  38. package/dist/cjs/packages/formitem/formitem.d.ts +1 -0
  39. package/dist/cjs/packages/formitem/formitem.js +55 -44
  40. package/dist/cjs/packages/formitem/style/formitem.scss +101 -21
  41. package/dist/cjs/packages/formitem/style/style.css +89 -12
  42. package/dist/cjs/packages/imagepreview/style/style.css +12 -0
  43. package/dist/cjs/packages/input/style/input.scss +6 -4
  44. package/dist/cjs/packages/input/style/style.css +4 -0
  45. package/dist/cjs/packages/numberkeyboard/style/style.css +12 -0
  46. package/dist/cjs/packages/nutui.react.build.d.ts +11 -15
  47. package/dist/cjs/packages/nutui.react.build.js +8 -16
  48. package/dist/cjs/packages/nutui.react.d.ts +16 -22
  49. package/dist/cjs/packages/nutui.react.js +13 -23
  50. package/dist/cjs/packages/nutui.react.scss.d.ts +5 -7
  51. package/dist/cjs/packages/nutui.react.scss.js +5 -7
  52. package/dist/cjs/packages/overlay/style/overlay.scss +0 -4
  53. package/dist/cjs/packages/picker/style/index.js +1 -0
  54. package/dist/cjs/packages/picker/style/style.css +82 -0
  55. package/dist/cjs/packages/popover/style/style.css +12 -0
  56. package/dist/cjs/packages/popup/style/popup.scss +12 -4
  57. package/dist/cjs/packages/popup/style/style.css +12 -0
  58. package/dist/cjs/packages/shortpassword/shortpassword.js +5 -7
  59. package/dist/cjs/packages/shortpassword/style/shortpassword.scss +10 -0
  60. package/dist/cjs/packages/shortpassword/style/style.css +21 -0
  61. package/dist/cjs/packages/swipe/style/style.css +3 -3
  62. package/dist/cjs/packages/swipe/style/swipe.scss +7 -3
  63. package/dist/cjs/packages/tabbar/style/style.css +6 -0
  64. package/dist/cjs/packages/tabbaritem/style/style.css +6 -0
  65. package/dist/cjs/packages/tabbaritem/style/tabbaritem.scss +6 -0
  66. package/dist/cjs/packages/timeselect/style/style.css +12 -0
  67. package/dist/cjs/packages/tour/style/style.css +12 -0
  68. package/dist/cjs/utils/get-rect-by-taro.d.ts +1 -1
  69. package/dist/cjs/utils/get-rect-by-taro.js +4 -1
  70. package/dist/cjs/utils/get-system-info.js +4 -5
  71. package/dist/cjs/utils/merge.d.ts +14 -0
  72. package/dist/cjs/utils/merge.js +98 -0
  73. package/dist/cjs/utils/platform-taro.js +4 -5
  74. package/dist/cjs/utils/to-array.d.ts +1 -0
  75. package/dist/cjs/utils/to-array.js +18 -0
  76. package/dist/es/hooks/use-client-rect.js +1 -0
  77. package/dist/es/packages/actionsheet/style/style.css +12 -0
  78. package/dist/es/packages/address/style/style.css +12 -0
  79. package/dist/es/packages/calendar/style/style.css +12 -0
  80. package/dist/es/packages/cascader/style/style.css +12 -0
  81. package/dist/es/packages/configprovider/types.d.ts +1 -1
  82. package/dist/es/packages/datepicker/datepicker.js +8 -6
  83. package/dist/es/packages/datepicker/style/style.css +82 -0
  84. package/dist/es/packages/datepickerview/datepickerview.d.ts +3 -0
  85. package/dist/es/packages/datepickerview/datepickerview.js +106 -0
  86. package/dist/es/packages/datepickerview/index.d.ts +3 -0
  87. package/dist/es/packages/datepickerview/index.js +2 -0
  88. package/dist/es/packages/datepickerview/style/datepickerview.scss +4 -0
  89. package/dist/es/packages/datepickerview/style/index.js +2 -0
  90. package/dist/es/packages/datepickerview/style/style.css +78 -0
  91. package/dist/es/packages/datepickerview/types.d.ts +15 -0
  92. package/dist/es/packages/datepickerview/types.js +1 -0
  93. package/dist/es/packages/dialog/dialogwrap.js +3 -0
  94. package/dist/es/packages/dialog/style/dialog.scss +0 -1
  95. package/dist/es/packages/dialog/style/style.css +0 -1
  96. package/dist/es/packages/drag/style/drag.scss +1 -0
  97. package/dist/es/packages/drag/style/style.css +1 -0
  98. package/dist/es/packages/fixednav/fixednav.js +20 -14
  99. package/dist/es/packages/fixednav/style/fixednav.scss +0 -15
  100. package/dist/es/packages/fixednav/style/style.css +0 -14
  101. package/dist/es/packages/form/__tests__/merge.spec.d.ts +1 -0
  102. package/dist/es/packages/form/__tests__/merge.spec.js +541 -0
  103. package/dist/es/packages/form/form.d.ts +10 -0
  104. package/dist/es/packages/form/form.js +9 -2
  105. package/dist/es/packages/form/index.d.ts +2 -1
  106. package/dist/es/packages/form/index.js +2 -1
  107. package/dist/es/packages/form/style/form.scss +0 -66
  108. package/dist/es/packages/form/style/style.css +31 -21
  109. package/dist/es/packages/form/types.d.ts +2 -1
  110. package/dist/es/packages/form/useform.d.ts +2 -1
  111. package/dist/es/packages/form/useform.js +340 -279
  112. package/dist/es/packages/formitem/formitem.d.ts +1 -0
  113. package/dist/es/packages/formitem/formitem.js +55 -44
  114. package/dist/es/packages/formitem/style/formitem.scss +101 -21
  115. package/dist/es/packages/formitem/style/style.css +89 -12
  116. package/dist/es/packages/imagepreview/style/style.css +12 -0
  117. package/dist/es/packages/input/style/input.scss +6 -4
  118. package/dist/es/packages/input/style/style.css +4 -0
  119. package/dist/es/packages/numberkeyboard/style/style.css +12 -0
  120. package/dist/es/packages/nutui.react.build.d.ts +11 -15
  121. package/dist/es/packages/nutui.react.build.js +11 -15
  122. package/dist/es/packages/overlay/style/overlay.scss +0 -4
  123. package/dist/es/packages/picker/style/index.js +1 -0
  124. package/dist/es/packages/picker/style/style.css +82 -0
  125. package/dist/es/packages/popover/style/style.css +12 -0
  126. package/dist/es/packages/popup/style/popup.scss +12 -4
  127. package/dist/es/packages/popup/style/style.css +12 -0
  128. package/dist/es/packages/shortpassword/shortpassword.js +5 -7
  129. package/dist/es/packages/shortpassword/style/shortpassword.scss +10 -0
  130. package/dist/es/packages/shortpassword/style/style.css +21 -0
  131. package/dist/es/packages/swipe/style/style.css +3 -3
  132. package/dist/es/packages/swipe/style/swipe.scss +7 -3
  133. package/dist/es/packages/tabbar/style/style.css +6 -0
  134. package/dist/es/packages/tabbaritem/style/style.css +6 -0
  135. package/dist/es/packages/tabbaritem/style/tabbaritem.scss +6 -0
  136. package/dist/es/packages/timeselect/style/style.css +12 -0
  137. package/dist/es/packages/tour/style/style.css +12 -0
  138. package/dist/es/utils/get-rect-by-taro.d.ts +1 -1
  139. package/dist/es/utils/get-rect-by-taro.js +4 -1
  140. package/dist/es/utils/get-system-info.js +4 -4
  141. package/dist/es/utils/merge.d.ts +14 -0
  142. package/dist/es/utils/merge.js +68 -0
  143. package/dist/es/utils/platform-taro.js +4 -4
  144. package/dist/es/utils/to-array.d.ts +1 -0
  145. package/dist/es/utils/to-array.js +8 -0
  146. package/dist/nutui.react.umd.js +1483 -1416
  147. package/dist/style.css +1 -1
  148. package/dist/style.scss +33 -35
  149. package/dist/styles/variables-jmapp.scss +0 -50
  150. package/dist/styles/variables-jrkf.scss +0 -50
  151. package/dist/styles/variables.scss +0 -49
  152. package/package.json +1 -1
  153. package/dist/cjs/packages/sidenavbar/context.d.ts +0 -1
  154. package/dist/cjs/packages/sidenavbar/context.js +0 -12
  155. package/dist/cjs/packages/sidenavbar/index.d.ts +0 -3
  156. package/dist/cjs/packages/sidenavbar/index.js +0 -12
  157. package/dist/cjs/packages/sidenavbar/sidenavbar.d.ts +0 -14
  158. package/dist/cjs/packages/sidenavbar/sidenavbar.js +0 -70
  159. package/dist/cjs/packages/sidenavbar/style/index.js +0 -2
  160. package/dist/cjs/packages/sidenavbar/style/sidenavbar.scss +0 -89
  161. package/dist/cjs/packages/sidenavbar/style/style.css +0 -443
  162. package/dist/cjs/packages/sidenavbar/utils.d.ts +0 -3
  163. package/dist/cjs/packages/sidenavbar/utils.js +0 -23
  164. package/dist/cjs/packages/sidenavbaritem/index.d.ts +0 -3
  165. package/dist/cjs/packages/sidenavbaritem/index.js +0 -12
  166. package/dist/cjs/packages/sidenavbaritem/sidenavbaritem.d.ts +0 -11
  167. package/dist/cjs/packages/sidenavbaritem/sidenavbaritem.js +0 -34
  168. package/dist/cjs/packages/sidenavbaritem/style/index.js +0 -1
  169. package/dist/cjs/packages/sidenavbaritem/style/sidenavbaritem.scss +0 -13
  170. package/dist/cjs/packages/sidenavbaritem/style/style.css +0 -14
  171. package/dist/cjs/packages/subsidenavbar/index.d.ts +0 -3
  172. package/dist/cjs/packages/subsidenavbar/style/css.js +0 -1
  173. package/dist/cjs/packages/subsidenavbar/style/index.js +0 -1
  174. package/dist/cjs/packages/subsidenavbar/style/style.css +0 -20
  175. package/dist/cjs/packages/subsidenavbar/style/subsidenavbar.scss +0 -26
  176. package/dist/cjs/packages/subsidenavbar/subsidenavbar.d.ts +0 -13
  177. package/dist/cjs/packages/subsidenavbar/subsidenavbar.js +0 -88
  178. package/dist/es/packages/sidenavbar/context.d.ts +0 -1
  179. package/dist/es/packages/sidenavbar/context.js +0 -2
  180. package/dist/es/packages/sidenavbar/index.d.ts +0 -3
  181. package/dist/es/packages/sidenavbar/index.js +0 -2
  182. package/dist/es/packages/sidenavbar/sidenavbar.d.ts +0 -38
  183. package/dist/es/packages/sidenavbar/sidenavbar.js +0 -58
  184. package/dist/es/packages/sidenavbar/style/css.js +0 -1
  185. package/dist/es/packages/sidenavbar/style/index.js +0 -2
  186. package/dist/es/packages/sidenavbar/style/sidenavbar.scss +0 -89
  187. package/dist/es/packages/sidenavbar/style/style.css +0 -443
  188. package/dist/es/packages/sidenavbar/utils.d.ts +0 -3
  189. package/dist/es/packages/sidenavbar/utils.js +0 -14
  190. package/dist/es/packages/sidenavbaritem/index.d.ts +0 -3
  191. package/dist/es/packages/sidenavbaritem/index.js +0 -2
  192. package/dist/es/packages/sidenavbaritem/sidenavbaritem.d.ts +0 -11
  193. package/dist/es/packages/sidenavbaritem/sidenavbaritem.js +0 -23
  194. package/dist/es/packages/sidenavbaritem/style/css.js +0 -1
  195. package/dist/es/packages/sidenavbaritem/style/index.js +0 -1
  196. package/dist/es/packages/sidenavbaritem/style/sidenavbaritem.scss +0 -13
  197. package/dist/es/packages/sidenavbaritem/style/style.css +0 -14
  198. package/dist/es/packages/subsidenavbar/index.d.ts +0 -3
  199. package/dist/es/packages/subsidenavbar/index.js +0 -2
  200. package/dist/es/packages/subsidenavbar/style/css.js +0 -1
  201. package/dist/es/packages/subsidenavbar/style/index.js +0 -1
  202. package/dist/es/packages/subsidenavbar/style/style.css +0 -20
  203. package/dist/es/packages/subsidenavbar/style/subsidenavbar.scss +0 -26
  204. package/dist/es/packages/subsidenavbar/subsidenavbar.d.ts +0 -13
  205. package/dist/es/packages/subsidenavbar/subsidenavbar.js +0 -77
  206. /package/dist/cjs/packages/{sidenavbar → datepickerview}/style/css.js +0 -0
  207. /package/dist/{cjs/packages/sidenavbaritem → es/packages/datepickerview}/style/css.js +0 -0
@@ -14,336 +14,380 @@ _export(exports, {
14
14
  },
15
15
  useForm: function() {
16
16
  return useForm;
17
+ },
18
+ useWatch: function() {
19
+ return useWatch;
17
20
  }
18
21
  });
19
22
  var _interop_require_default = require("@swc/helpers/_/_interop_require_default");
20
23
  var _async_to_generator = require("@swc/helpers/_/_async_to_generator");
21
24
  var _class_call_check = require("@swc/helpers/_/_class_call_check");
25
+ var _create_class = require("@swc/helpers/_/_create_class");
22
26
  var _define_property = require("@swc/helpers/_/_define_property");
23
27
  var _object_spread = require("@swc/helpers/_/_object_spread");
28
+ var _sliced_to_array = require("@swc/helpers/_/_sliced_to_array");
24
29
  var _to_consumable_array = require("@swc/helpers/_/_to_consumable_array");
25
30
  var _ts_generator = require("@swc/helpers/_/_ts_generator");
26
- var _ts_values = require("@swc/helpers/_/_ts_values");
27
31
  var _react = require("react");
28
32
  var _asyncvalidator = /*#__PURE__*/ _interop_require_default._(require("async-validator"));
33
+ var _merge = require("../../utils/merge");
29
34
  var SECRET = 'NUT_FORM_INTERNAL';
30
35
  /**
31
36
  * 用于存储表单的数据
32
- */ var FormStore = function FormStore() {
37
+ */ var FormStore = /*#__PURE__*/ function() {
33
38
  "use strict";
34
- var _this = this;
35
- var _this1 = this;
36
- (0, _class_call_check._)(this, FormStore);
37
- var _this2 = this;
38
- // 初始化数据
39
- (0, _define_property._)(this, "initialValues", {});
40
- (0, _define_property._)(this, "updateList", []);
41
- // 存放表单中所有的数据 eg. {password: "ddd",username: "123"}
42
- (0, _define_property._)(this, "store", {});
43
- // 所有的组件实例
44
- (0, _define_property._)(this, "fieldEntities", []);
45
- // 校验成功或失败的回调,onFinish、onFinishFailed
46
- (0, _define_property._)(this, "callbacks", {});
47
- (0, _define_property._)(this, "errors", {});
48
- /**
39
+ function FormStore() {
40
+ var _this = this;
41
+ var _this1 = this;
42
+ (0, _class_call_check._)(this, FormStore);
43
+ var _this2 = this;
44
+ // 初始化数据
45
+ (0, _define_property._)(this, "initialValues", {});
46
+ (0, _define_property._)(this, "updateList", []);
47
+ // 存放表单中所有的数据 eg. {password: "ddd",username: "123"}
48
+ (0, _define_property._)(this, "store", {});
49
+ // 所有的组件实例
50
+ (0, _define_property._)(this, "fieldEntities", []);
51
+ // 校验成功或失败的回调,onFinish、onFinishFailed
52
+ (0, _define_property._)(this, "callbacks", {});
53
+ (0, _define_property._)(this, "errors", {});
54
+ /**
49
55
  * 注册组件实例
50
56
  * @param field
51
57
  */ (0, _define_property._)(this, "registerField", function(field) {
52
- _this.fieldEntities.push(field);
53
- return function() {
54
- _this.fieldEntities = _this.fieldEntities.filter(function(item) {
55
- return item !== field;
56
- });
57
- if (_this.store) {
58
- delete _this.store[field.props.name];
59
- }
60
- };
61
- });
62
- /**
58
+ _this.fieldEntities.push(field);
59
+ return function() {
60
+ _this.fieldEntities = _this.fieldEntities.filter(function(item) {
61
+ return item !== field;
62
+ });
63
+ };
64
+ });
65
+ /**
63
66
  * 获取 formItem 的值
64
67
  * @param name
65
68
  */ (0, _define_property._)(this, "getFieldValue", function(name) {
66
- var _this_store;
67
- return (_this_store = _this.store) === null || _this_store === void 0 ? void 0 : _this_store[name];
68
- });
69
- /**
69
+ var _this_store;
70
+ return (_this_store = _this.store) === null || _this_store === void 0 ? void 0 : _this_store[name];
71
+ });
72
+ /**
70
73
  * 获取全部字段
71
74
  */ (0, _define_property._)(this, "getFieldsValue", function(nameList) {
72
- if (typeof nameList === 'boolean') {
73
- return JSON.parse(JSON.stringify(_this.store));
74
- }
75
- var fieldsValue = {};
76
- nameList.forEach(function(field) {
77
- fieldsValue[field] = _this.getFieldValue(field);
75
+ if (typeof nameList === 'boolean') {
76
+ return JSON.parse(JSON.stringify(_this.store));
77
+ }
78
+ var fieldsValue = {};
79
+ nameList.forEach(function(field) {
80
+ fieldsValue[field] = _this.getFieldValue(field);
81
+ });
82
+ return fieldsValue;
78
83
  });
79
- return fieldsValue;
80
- });
81
- /**
84
+ /**
82
85
  * 设置 form 的初始值,之后在 reset 的时候使用
83
86
  * @param values
84
87
  * @param init
85
- */ (0, _define_property._)(this, "setInitialValues", function(values, init) {
86
- if (init) {
87
- _this.initialValues = values;
88
- _this.store = values;
89
- }
90
- });
91
- /**
88
+ */ (0, _define_property._)(this, "setInitialValues", function(initialValues, init) {
89
+ _this.initialValues = initialValues || {};
90
+ if (init) {
91
+ var nextStore = (0, _merge.merge)(initialValues, _this.store);
92
+ _this.updateStore(nextStore);
93
+ _this.notifyWatch();
94
+ }
95
+ });
96
+ /**
92
97
  * 存储组件数据
93
98
  * @param newStore { [name]: newValue }
94
99
  */ (0, _define_property._)(this, "setFieldsValue", function(newStore) {
95
- var needValidate = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : true;
96
- _this1.store = (0, _object_spread._)({}, _this1.store, newStore);
97
- _this1.fieldEntities.forEach(function(entity) {
98
- var name = entity.props.name;
99
- Object.keys(newStore).forEach(function(key) {
100
- if (key === name) {
101
- entity.onStoreChange('update');
100
+ var nextStore = (0, _merge.recursive)(true, _this.store, newStore);
101
+ _this.updateStore(nextStore);
102
+ _this.fieldEntities.forEach(function(entity) {
103
+ var name = entity.props.name;
104
+ Object.keys(newStore).forEach(function(key) {
105
+ if (key === name) {
106
+ entity.onStoreChange('update');
107
+ }
108
+ });
109
+ });
110
+ _this.updateList.forEach(function(item) {
111
+ var shouldUpdate = item.condition;
112
+ if (typeof item.condition === 'function') {
113
+ shouldUpdate = item.condition();
114
+ }
115
+ if (shouldUpdate) {
116
+ item.entity.onStoreChange('update');
102
117
  }
103
118
  });
119
+ _this.notifyWatch();
104
120
  });
105
- _this1.updateList.forEach(function(item) {
106
- var shouldUpdate = item.condition;
107
- if (typeof item.condition === 'function') {
108
- shouldUpdate = item.condition();
109
- }
110
- if (shouldUpdate) {
111
- item.entity.onStoreChange('update');
112
- }
121
+ (0, _define_property._)(this, "setFieldValue", function(name, value) {
122
+ var store = (0, _define_property._)({}, name, value);
123
+ _this.setFieldsValue(store);
124
+ _this.notifyWatch([
125
+ name
126
+ ]);
113
127
  });
114
- needValidate && _this1.validateFields();
115
- });
116
- (0, _define_property._)(this, "setCallback", function(callback) {
117
- _this.callbacks = (0, _object_spread._)({}, _this.callbacks, callback);
118
- });
119
- (0, _define_property._)(this, "validateFields", /*#__PURE__*/ function() {
120
- var _ref = (0, _async_to_generator._)(function(nameList) {
121
- var filterEntitys, errs, _iteratorNormalCompletion, _didIteratorError, _iteratorError, _loop, _iterator, _step, err;
128
+ (0, _define_property._)(this, "setCallback", function(callback) {
129
+ _this.callbacks = (0, _object_spread._)({}, _this.callbacks, callback);
130
+ });
131
+ (0, _define_property._)(this, "validateEntities", /*#__PURE__*/ function() {
132
+ var _ref = (0, _async_to_generator._)(function(entity, errs) {
133
+ var _entity_props, name, _entity_props_rules, rules, descriptor, validator, _this_store, param, errors, _errs;
134
+ return (0, _ts_generator._)(this, function(_state) {
135
+ switch(_state.label){
136
+ case 0:
137
+ _entity_props = entity.props, name = _entity_props.name, _entity_props_rules = _entity_props.rules, rules = _entity_props_rules === void 0 ? [] : _entity_props_rules;
138
+ if (!name) {
139
+ console.warn('Form field missing name property');
140
+ return [
141
+ 2
142
+ ];
143
+ }
144
+ descriptor = {};
145
+ if (rules.length) {
146
+ // 多条校验规则
147
+ if (rules.length > 1) {
148
+ descriptor[name] = [];
149
+ rules.forEach(function(v) {
150
+ descriptor[name].push(v);
151
+ });
152
+ } else {
153
+ descriptor[name] = rules[0];
154
+ }
155
+ }
156
+ validator = new _asyncvalidator.default(descriptor);
157
+ _state.label = 1;
158
+ case 1:
159
+ _state.trys.push([
160
+ 1,
161
+ 3,
162
+ 4,
163
+ 5
164
+ ]);
165
+ return [
166
+ 4,
167
+ validator.validate((0, _define_property._)({}, name, (_this_store = _this2.store) === null || _this_store === void 0 ? void 0 : _this_store[name]))
168
+ ];
169
+ case 2:
170
+ _state.sent();
171
+ return [
172
+ 3,
173
+ 5
174
+ ];
175
+ case 3:
176
+ param = _state.sent();
177
+ errors = param.errors;
178
+ if (errors) {
179
+ ;
180
+ (_errs = errs).push.apply(_errs, (0, _to_consumable_array._)(errors));
181
+ _this2.errors[name] = errors;
182
+ }
183
+ return [
184
+ 3,
185
+ 5
186
+ ];
187
+ case 4:
188
+ if (!errs || errs.length === 0) {
189
+ _this2.errors[name] = [];
190
+ }
191
+ return [
192
+ 7
193
+ ];
194
+ case 5:
195
+ entity.onStoreChange('validate');
196
+ return [
197
+ 2
198
+ ];
199
+ }
200
+ });
201
+ });
202
+ return function(entity, errs) {
203
+ return _ref.apply(this, arguments);
204
+ };
205
+ }());
206
+ (0, _define_property._)(this, "validateFields", /*#__PURE__*/ function() {
207
+ var _ref = (0, _async_to_generator._)(function(nameList) {
208
+ var filterEntities, errs;
209
+ return (0, _ts_generator._)(this, function(_state) {
210
+ switch(_state.label){
211
+ case 0:
212
+ filterEntities = [];
213
+ // this.errors.length = 0
214
+ if (!nameList || nameList.length === 0) {
215
+ filterEntities = _this2.fieldEntities;
216
+ } else {
217
+ filterEntities = _this2.fieldEntities.filter(function(param) {
218
+ var name = param.props.name;
219
+ return nameList.includes(name);
220
+ });
221
+ }
222
+ errs = [];
223
+ return [
224
+ 4,
225
+ Promise.all(filterEntities.map(/*#__PURE__*/ function() {
226
+ var _ref = (0, _async_to_generator._)(function(entity) {
227
+ return (0, _ts_generator._)(this, function(_state) {
228
+ switch(_state.label){
229
+ case 0:
230
+ return [
231
+ 4,
232
+ _this2.validateEntities(entity, errs)
233
+ ];
234
+ case 1:
235
+ _state.sent();
236
+ return [
237
+ 2
238
+ ];
239
+ }
240
+ });
241
+ });
242
+ return function(entity) {
243
+ return _ref.apply(this, arguments);
244
+ };
245
+ }()))
246
+ ];
247
+ case 1:
248
+ _state.sent();
249
+ return [
250
+ 2,
251
+ errs
252
+ ];
253
+ }
254
+ });
255
+ });
256
+ return function(nameList) {
257
+ return _ref.apply(this, arguments);
258
+ };
259
+ }());
260
+ (0, _define_property._)(this, "submit", /*#__PURE__*/ (0, _async_to_generator._)(function() {
261
+ var errors, _this_callbacks_onFinish, _this_callbacks, _this_callbacks_onFinishFailed, _this_callbacks1;
122
262
  return (0, _ts_generator._)(this, function(_state) {
123
263
  switch(_state.label){
124
264
  case 0:
125
- filterEntitys = [];
126
- errs = [];
127
- _this2.errors.length = 0;
128
- if (!nameList || nameList.length === 0) {
129
- filterEntitys = _this2.fieldEntities;
130
- } else {
131
- filterEntitys = _this2.fieldEntities.filter(function(param) {
132
- var name = param.props.name;
133
- return nameList.includes(name);
134
- });
135
- }
136
- _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
137
- _state.label = 1;
138
- case 1:
139
- _state.trys.push([
140
- 1,
141
- 6,
142
- 7,
143
- 8
144
- ]);
145
- _loop = function _loop() {
146
- var entity, _entity_props, name, _entity_props_rules, rules, descriptor, validator, _this_store, param, errors, _errs;
147
- return (0, _ts_generator._)(this, function(_state) {
148
- switch(_state.label){
149
- case 0:
150
- entity = _step.value;
151
- _entity_props = entity.props, name = _entity_props.name, _entity_props_rules = _entity_props.rules, rules = _entity_props_rules === void 0 ? [] : _entity_props_rules;
152
- descriptor = {};
153
- if (rules.length) {
154
- // 多条校验规则
155
- if (rules.length > 1) {
156
- descriptor[name] = [];
157
- rules.forEach(function(v) {
158
- descriptor[name].push(v);
159
- });
160
- } else {
161
- descriptor[name] = rules[0];
162
- }
163
- }
164
- validator = new _asyncvalidator.default(descriptor);
165
- _state.label = 1;
166
- case 1:
167
- _state.trys.push([
168
- 1,
169
- 3,
170
- 4,
171
- 5
172
- ]);
173
- return [
174
- 4,
175
- validator.validate((0, _define_property._)({}, name, (_this_store = _this2.store) === null || _this_store === void 0 ? void 0 : _this_store[name]))
176
- ];
177
- case 2:
178
- _state.sent();
179
- return [
180
- 3,
181
- 5
182
- ];
183
- case 3:
184
- param = _state.sent();
185
- errors = param.errors;
186
- if (errors) {
187
- ;
188
- (_errs = errs).push.apply(_errs, (0, _to_consumable_array._)(errors));
189
- _this2.errors[name] = errors;
190
- }
191
- return [
192
- 3,
193
- 5
194
- ];
195
- case 4:
196
- if (!errs || errs.length === 0) {
197
- _this2.errors[name] = [];
198
- }
199
- return [
200
- 7
201
- ];
202
- case 5:
203
- entity.onStoreChange('validate');
204
- return [
205
- 2
206
- ];
207
- }
208
- });
209
- };
210
- _iterator = filterEntitys[Symbol.iterator]();
211
- _state.label = 2;
212
- case 2:
213
- if (!!(_iteratorNormalCompletion = (_step = _iterator.next()).done)) return [
214
- 3,
215
- 5
216
- ];
217
265
  return [
218
- 5,
219
- (0, _ts_values._)(_loop())
266
+ 4,
267
+ _this2.validateFields()
220
268
  ];
221
- case 3:
222
- _state.sent();
223
- _state.label = 4;
224
- case 4:
225
- _iteratorNormalCompletion = true;
269
+ case 1:
270
+ errors = _state.sent();
271
+ if (errors.length === 0) {
272
+ ;
273
+ (_this_callbacks_onFinish = (_this_callbacks = _this2.callbacks).onFinish) === null || _this_callbacks_onFinish === void 0 ? void 0 : _this_callbacks_onFinish.call(_this_callbacks, _this2.store);
274
+ } else if (errors.length > 0) {
275
+ ;
276
+ (_this_callbacks_onFinishFailed = (_this_callbacks1 = _this2.callbacks).onFinishFailed) === null || _this_callbacks_onFinishFailed === void 0 ? void 0 : _this_callbacks_onFinishFailed.call(_this_callbacks1, _this2.store, errors);
277
+ }
226
278
  return [
227
- 3,
228
279
  2
229
280
  ];
230
- case 5:
231
- return [
232
- 3,
233
- 8
234
- ];
235
- case 6:
236
- err = _state.sent();
237
- _didIteratorError = true;
238
- _iteratorError = err;
239
- return [
240
- 3,
241
- 8
242
- ];
243
- case 7:
244
- try {
245
- if (!_iteratorNormalCompletion && _iterator.return != null) {
246
- _iterator.return();
247
- }
248
- } finally{
249
- if (_didIteratorError) {
250
- throw _iteratorError;
281
+ }
282
+ });
283
+ }));
284
+ (0, _define_property._)(this, "resetFields", function(namePaths) {
285
+ if (namePaths && namePaths.length) {
286
+ namePaths.forEach(function(path) {
287
+ _this.errors[path] = null;
288
+ _this.fieldEntities.forEach(function(entity) {
289
+ var name = entity.props.name;
290
+ if (name === path) {
291
+ if (path in _this.initialValues) {
292
+ _this.updateStore((0, _define_property._)({}, path, _this.initialValues[path]));
293
+ } else {
294
+ delete _this.store[path];
251
295
  }
296
+ entity.onStoreChange('reset');
252
297
  }
253
- return [
254
- 7
255
- ];
256
- case 8:
257
- return [
258
- 2,
259
- errs
260
- ];
261
- }
298
+ });
299
+ });
300
+ } else {
301
+ var nextStore = (0, _merge.merge)({}, _this.initialValues);
302
+ _this.updateStore(nextStore);
303
+ _this.fieldEntities.forEach(function(entity) {
304
+ entity.onStoreChange('reset');
305
+ });
306
+ }
307
+ });
308
+ // 监听事件
309
+ (0, _define_property._)(this, "registerUpdate", function(field, shouldUpdate) {
310
+ _this.updateList.push({
311
+ entity: field,
312
+ condition: shouldUpdate
262
313
  });
314
+ return function() {
315
+ _this.updateList = _this.updateList.filter(function(i) {
316
+ return i.entity !== field;
317
+ });
318
+ };
263
319
  });
264
- return function(nameList) {
265
- return _ref.apply(this, arguments);
266
- };
267
- }());
268
- (0, _define_property._)(this, "submit", /*#__PURE__*/ (0, _async_to_generator._)(function() {
269
- var errors, _this_callbacks_onFinish, _this_callbacks, _this_callbacks_onFinishFailed, _this_callbacks1;
270
- return (0, _ts_generator._)(this, function(_state) {
271
- switch(_state.label){
272
- case 0:
273
- return [
274
- 4,
275
- _this2.validateFields()
276
- ];
277
- case 1:
278
- errors = _state.sent();
279
- if (errors.length === 0) {
280
- ;
281
- (_this_callbacks_onFinish = (_this_callbacks = _this2.callbacks).onFinish) === null || _this_callbacks_onFinish === void 0 ? void 0 : _this_callbacks_onFinish.call(_this_callbacks, _this2.store);
282
- } else if (errors.length > 0) {
283
- ;
284
- (_this_callbacks_onFinishFailed = (_this_callbacks1 = _this2.callbacks).onFinishFailed) === null || _this_callbacks_onFinishFailed === void 0 ? void 0 : _this_callbacks_onFinishFailed.call(_this_callbacks1, _this2.store, errors);
285
- }
286
- return [
287
- 2
288
- ];
320
+ (0, _define_property._)(this, "dispatch", function(param) {
321
+ var name = param.name;
322
+ _this.validateFields([
323
+ name
324
+ ]);
325
+ });
326
+ (0, _define_property._)(this, "getInternal", function(key) {
327
+ if (key === SECRET) {
328
+ return {
329
+ registerField: _this.registerField,
330
+ setCallback: _this.setCallback,
331
+ setInitialValues: _this.setInitialValues,
332
+ dispatch: _this.dispatch,
333
+ store: _this.store,
334
+ fieldEntities: _this.fieldEntities,
335
+ registerUpdate: _this.registerUpdate,
336
+ registerWatch: _this.registerWatch
337
+ };
289
338
  }
290
339
  });
291
- }));
292
- (0, _define_property._)(this, "resetFields", function() {
293
- _this.errors.length = 0;
294
- _this.store = _this.initialValues;
295
- _this.fieldEntities.forEach(function(entity) {
296
- entity.onStoreChange('reset');
340
+ (0, _define_property._)(this, "getForm", function() {
341
+ return {
342
+ getFieldValue: _this.getFieldValue,
343
+ getFieldsValue: _this.getFieldsValue,
344
+ setFieldsValue: _this.setFieldsValue,
345
+ setFieldValue: _this.setFieldValue,
346
+ resetFields: _this.resetFields,
347
+ validateFields: _this.validateFields,
348
+ submit: _this.submit,
349
+ errors: _this.errors,
350
+ getInternal: _this.getInternal
351
+ };
352
+ });
353
+ (0, _define_property._)(this, "watchList", []);
354
+ (0, _define_property._)(this, "registerWatch", function(callback) {
355
+ _this.watchList.push(callback);
356
+ return function() {
357
+ _this.watchList = _this.watchList.filter(function(fn) {
358
+ return fn !== callback;
359
+ });
360
+ };
297
361
  });
298
- });
299
- // 监听事件
300
- (0, _define_property._)(this, "registerUpdate", function(field, shouldUpdate) {
301
- _this.updateList.push({
302
- entity: field,
303
- condition: shouldUpdate
362
+ (0, _define_property._)(this, "notifyWatch", function() {
363
+ var namePath = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : [];
364
+ if (_this1.watchList.length) {
365
+ var allValues;
366
+ if (!namePath || namePath.length === 0) {
367
+ allValues = _this1.getFieldsValue(true);
368
+ } else {
369
+ allValues = _this1.getFieldsValue(namePath);
370
+ }
371
+ _this1.watchList.forEach(function(callback) {
372
+ callback(allValues, namePath);
373
+ });
374
+ }
304
375
  });
305
- return function() {
306
- _this.updateList = _this.updateList.filter(function(i) {
307
- return i.entity !== field;
308
- });
376
+ this.callbacks = {
377
+ onFinish: function onFinish() {},
378
+ onFinishFailed: function onFinishFailed() {}
309
379
  };
310
- });
311
- (0, _define_property._)(this, "dispatch", function(param) {
312
- var name = param.name;
313
- _this.validateFields([
314
- name
315
- ]);
316
- });
317
- (0, _define_property._)(this, "getInternal", function(key) {
318
- if (key === SECRET) {
319
- return {
320
- registerField: _this.registerField,
321
- setCallback: _this.setCallback,
322
- setInitialValues: _this.setInitialValues,
323
- dispatch: _this.dispatch,
324
- store: _this.store,
325
- fieldEntities: _this.fieldEntities,
326
- registerUpdate: _this.registerUpdate
327
- };
380
+ }
381
+ (0, _create_class._)(FormStore, [
382
+ {
383
+ key: "updateStore",
384
+ value: function updateStore(nextStore) {
385
+ this.store = nextStore;
386
+ }
328
387
  }
329
- });
330
- (0, _define_property._)(this, "getForm", function() {
331
- return {
332
- getFieldValue: _this.getFieldValue,
333
- getFieldsValue: _this.getFieldsValue,
334
- setFieldsValue: _this.setFieldsValue,
335
- resetFields: _this.resetFields,
336
- validateFields: _this.validateFields,
337
- submit: _this.submit,
338
- errors: _this.errors,
339
- getInternal: _this.getInternal
340
- };
341
- });
342
- this.callbacks = {
343
- onFinish: function onFinish() {},
344
- onFinishFailed: function onFinishFailed() {}
345
- };
346
- };
388
+ ]);
389
+ return FormStore;
390
+ }();
347
391
  var useForm = function useForm(form) {
348
392
  var formRef = (0, _react.useRef)();
349
393
  if (!formRef.current) {
@@ -358,3 +402,23 @@ var useForm = function useForm(form) {
358
402
  formRef.current
359
403
  ];
360
404
  };
405
+ var useWatch = function useWatch(path, form) {
406
+ var formInstance = form.getInternal(SECRET);
407
+ var _useState = (0, _sliced_to_array._)((0, _react.useState)(), 2), value = _useState[0], setValue = _useState[1];
408
+ (0, _react.useEffect)(function() {
409
+ var unsubscribe = formInstance.registerWatch(function(data, namePath) {
410
+ var value = data[path];
411
+ setValue(value);
412
+ });
413
+ var initialValue = form.getFieldsValue(true);
414
+ if (value !== initialValue[path]) {
415
+ setValue(initialValue[path]);
416
+ }
417
+ return function() {
418
+ return unsubscribe();
419
+ };
420
+ }, [
421
+ form
422
+ ]);
423
+ return value;
424
+ };