@nutui/nutui-react 3.0.1 → 3.0.3-beta.0

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 (75) hide show
  1. package/CHANGELOG.md +19 -0
  2. package/dist/cjs/hooks/use-client-rect.js +1 -0
  3. package/dist/cjs/packages/datepicker/style/style.css +70 -0
  4. package/dist/cjs/packages/datepickerview/style/index.js +1 -0
  5. package/dist/cjs/packages/datepickerview/style/style.css +70 -0
  6. package/dist/cjs/packages/fixednav/fixednav.js +20 -14
  7. package/dist/cjs/packages/fixednav/style/fixednav.scss +0 -15
  8. package/dist/cjs/packages/fixednav/style/index.js +1 -0
  9. package/dist/cjs/packages/fixednav/style/style.css +97 -14
  10. package/dist/cjs/packages/form/__tests__/merge.spec.d.ts +1 -0
  11. package/dist/cjs/packages/form/__tests__/merge.spec.js +545 -0
  12. package/dist/cjs/packages/form/form.d.ts +2 -0
  13. package/dist/cjs/packages/form/form.js +9 -2
  14. package/dist/cjs/packages/form/index.d.ts +2 -1
  15. package/dist/cjs/packages/form/index.js +1 -0
  16. package/dist/cjs/packages/form/style/form.scss +0 -66
  17. package/dist/cjs/packages/form/style/style.css +31 -21
  18. package/dist/cjs/packages/form/types.d.ts +2 -1
  19. package/dist/cjs/packages/form/useform.d.ts +2 -1
  20. package/dist/cjs/packages/form/useform.js +342 -278
  21. package/dist/cjs/packages/formitem/formitem.d.ts +1 -0
  22. package/dist/cjs/packages/formitem/formitem.js +55 -44
  23. package/dist/cjs/packages/formitem/style/formitem.scss +101 -21
  24. package/dist/cjs/packages/formitem/style/style.css +89 -12
  25. package/dist/cjs/packages/picker/style/index.js +1 -0
  26. package/dist/cjs/packages/picker/style/style.css +70 -0
  27. package/dist/cjs/packages/swipe/style/style.css +3 -3
  28. package/dist/cjs/packages/swipe/style/swipe.scss +7 -3
  29. package/dist/cjs/utils/get-rect-by-taro.d.ts +1 -1
  30. package/dist/cjs/utils/get-rect-by-taro.js +4 -1
  31. package/dist/cjs/utils/get-system-info.js +4 -5
  32. package/dist/cjs/utils/merge.d.ts +14 -0
  33. package/dist/cjs/utils/merge.js +98 -0
  34. package/dist/cjs/utils/platform-taro.js +4 -5
  35. package/dist/cjs/utils/to-array.d.ts +1 -0
  36. package/dist/cjs/utils/to-array.js +18 -0
  37. package/dist/es/hooks/use-client-rect.js +1 -0
  38. package/dist/es/packages/datepicker/style/style.css +70 -0
  39. package/dist/es/packages/datepickerview/style/index.js +1 -0
  40. package/dist/es/packages/datepickerview/style/style.css +70 -0
  41. package/dist/es/packages/fixednav/fixednav.js +20 -14
  42. package/dist/es/packages/fixednav/style/fixednav.scss +0 -15
  43. package/dist/es/packages/fixednav/style/index.js +1 -0
  44. package/dist/es/packages/fixednav/style/style.css +97 -14
  45. package/dist/es/packages/form/__tests__/merge.spec.d.ts +1 -0
  46. package/dist/es/packages/form/__tests__/merge.spec.js +541 -0
  47. package/dist/es/packages/form/form.d.ts +10 -0
  48. package/dist/es/packages/form/form.js +9 -2
  49. package/dist/es/packages/form/index.d.ts +2 -1
  50. package/dist/es/packages/form/index.js +2 -1
  51. package/dist/es/packages/form/style/form.scss +0 -66
  52. package/dist/es/packages/form/style/style.css +31 -21
  53. package/dist/es/packages/form/types.d.ts +2 -1
  54. package/dist/es/packages/form/useform.d.ts +2 -1
  55. package/dist/es/packages/form/useform.js +340 -279
  56. package/dist/es/packages/formitem/formitem.d.ts +1 -0
  57. package/dist/es/packages/formitem/formitem.js +55 -44
  58. package/dist/es/packages/formitem/style/formitem.scss +101 -21
  59. package/dist/es/packages/formitem/style/style.css +89 -12
  60. package/dist/es/packages/picker/style/index.js +1 -0
  61. package/dist/es/packages/picker/style/style.css +70 -0
  62. package/dist/es/packages/swipe/style/style.css +3 -3
  63. package/dist/es/packages/swipe/style/swipe.scss +7 -3
  64. package/dist/es/utils/get-rect-by-taro.d.ts +1 -1
  65. package/dist/es/utils/get-rect-by-taro.js +4 -1
  66. package/dist/es/utils/get-system-info.js +4 -4
  67. package/dist/es/utils/merge.d.ts +14 -0
  68. package/dist/es/utils/merge.js +68 -0
  69. package/dist/es/utils/platform-taro.js +4 -4
  70. package/dist/es/utils/to-array.d.ts +1 -0
  71. package/dist/es/utils/to-array.js +8 -0
  72. package/dist/nutui.react.umd.js +555 -422
  73. package/dist/style.css +1 -1
  74. package/dist/style.scss +41 -41
  75. package/package.json +1 -1
@@ -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
+ };