@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
@@ -1,330 +1,371 @@
1
1
  import { _ as _async_to_generator } from "@swc/helpers/_/_async_to_generator";
2
2
  import { _ as _class_call_check } from "@swc/helpers/_/_class_call_check";
3
+ import { _ as _create_class } from "@swc/helpers/_/_create_class";
3
4
  import { _ as _define_property } from "@swc/helpers/_/_define_property";
4
5
  import { _ as _object_spread } from "@swc/helpers/_/_object_spread";
6
+ import { _ as _sliced_to_array } from "@swc/helpers/_/_sliced_to_array";
5
7
  import { _ as _to_consumable_array } from "@swc/helpers/_/_to_consumable_array";
6
8
  import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
7
- import { _ as _ts_values } from "@swc/helpers/_/_ts_values";
8
- import { useRef } from "react";
9
+ import { useEffect, useRef, useState } from "react";
9
10
  import Schema from "async-validator";
11
+ import { merge, recursive } from "../../utils/merge";
10
12
  export var SECRET = 'NUT_FORM_INTERNAL';
11
13
  /**
12
14
  * 用于存储表单的数据
13
- */ var FormStore = function FormStore() {
15
+ */ var FormStore = /*#__PURE__*/ function() {
14
16
  "use strict";
15
- var _this = this;
16
- var _this1 = this;
17
- _class_call_check(this, FormStore);
18
- var _this2 = this;
19
- // 初始化数据
20
- _define_property(this, "initialValues", {});
21
- _define_property(this, "updateList", []);
22
- // 存放表单中所有的数据 eg. {password: "ddd",username: "123"}
23
- _define_property(this, "store", {});
24
- // 所有的组件实例
25
- _define_property(this, "fieldEntities", []);
26
- // 校验成功或失败的回调,onFinish、onFinishFailed
27
- _define_property(this, "callbacks", {});
28
- _define_property(this, "errors", {});
29
- /**
17
+ function FormStore() {
18
+ var _this = this;
19
+ var _this1 = this;
20
+ _class_call_check(this, FormStore);
21
+ var _this2 = this;
22
+ // 初始化数据
23
+ _define_property(this, "initialValues", {});
24
+ _define_property(this, "updateList", []);
25
+ // 存放表单中所有的数据 eg. {password: "ddd",username: "123"}
26
+ _define_property(this, "store", {});
27
+ // 所有的组件实例
28
+ _define_property(this, "fieldEntities", []);
29
+ // 校验成功或失败的回调,onFinish、onFinishFailed
30
+ _define_property(this, "callbacks", {});
31
+ _define_property(this, "errors", {});
32
+ /**
30
33
  * 注册组件实例
31
34
  * @param field
32
35
  */ _define_property(this, "registerField", function(field) {
33
- _this.fieldEntities.push(field);
34
- return function() {
35
- _this.fieldEntities = _this.fieldEntities.filter(function(item) {
36
- return item !== field;
37
- });
38
- if (_this.store) {
39
- delete _this.store[field.props.name];
40
- }
41
- };
42
- });
43
- /**
36
+ _this.fieldEntities.push(field);
37
+ return function() {
38
+ _this.fieldEntities = _this.fieldEntities.filter(function(item) {
39
+ return item !== field;
40
+ });
41
+ };
42
+ });
43
+ /**
44
44
  * 获取 formItem 的值
45
45
  * @param name
46
46
  */ _define_property(this, "getFieldValue", function(name) {
47
- var _this_store;
48
- return (_this_store = _this.store) === null || _this_store === void 0 ? void 0 : _this_store[name];
49
- });
50
- /**
47
+ var _this_store;
48
+ return (_this_store = _this.store) === null || _this_store === void 0 ? void 0 : _this_store[name];
49
+ });
50
+ /**
51
51
  * 获取全部字段
52
52
  */ _define_property(this, "getFieldsValue", function(nameList) {
53
- if (typeof nameList === 'boolean') {
54
- return JSON.parse(JSON.stringify(_this.store));
55
- }
56
- var fieldsValue = {};
57
- nameList.forEach(function(field) {
58
- fieldsValue[field] = _this.getFieldValue(field);
53
+ if (typeof nameList === 'boolean') {
54
+ return JSON.parse(JSON.stringify(_this.store));
55
+ }
56
+ var fieldsValue = {};
57
+ nameList.forEach(function(field) {
58
+ fieldsValue[field] = _this.getFieldValue(field);
59
+ });
60
+ return fieldsValue;
59
61
  });
60
- return fieldsValue;
61
- });
62
- /**
62
+ /**
63
63
  * 设置 form 的初始值,之后在 reset 的时候使用
64
64
  * @param values
65
65
  * @param init
66
- */ _define_property(this, "setInitialValues", function(values, init) {
67
- if (init) {
68
- _this.initialValues = values;
69
- _this.store = values;
70
- }
71
- });
72
- /**
66
+ */ _define_property(this, "setInitialValues", function(initialValues, init) {
67
+ _this.initialValues = initialValues || {};
68
+ if (init) {
69
+ var nextStore = merge(initialValues, _this.store);
70
+ _this.updateStore(nextStore);
71
+ _this.notifyWatch();
72
+ }
73
+ });
74
+ /**
73
75
  * 存储组件数据
74
76
  * @param newStore { [name]: newValue }
75
77
  */ _define_property(this, "setFieldsValue", function(newStore) {
76
- var needValidate = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : true;
77
- _this1.store = _object_spread({}, _this1.store, newStore);
78
- _this1.fieldEntities.forEach(function(entity) {
79
- var name = entity.props.name;
80
- Object.keys(newStore).forEach(function(key) {
81
- if (key === name) {
82
- entity.onStoreChange('update');
78
+ var nextStore = recursive(true, _this.store, newStore);
79
+ _this.updateStore(nextStore);
80
+ _this.fieldEntities.forEach(function(entity) {
81
+ var name = entity.props.name;
82
+ Object.keys(newStore).forEach(function(key) {
83
+ if (key === name) {
84
+ entity.onStoreChange('update');
85
+ }
86
+ });
87
+ });
88
+ _this.updateList.forEach(function(item) {
89
+ var shouldUpdate = item.condition;
90
+ if (typeof item.condition === 'function') {
91
+ shouldUpdate = item.condition();
92
+ }
93
+ if (shouldUpdate) {
94
+ item.entity.onStoreChange('update');
83
95
  }
84
96
  });
97
+ _this.notifyWatch();
85
98
  });
86
- _this1.updateList.forEach(function(item) {
87
- var shouldUpdate = item.condition;
88
- if (typeof item.condition === 'function') {
89
- shouldUpdate = item.condition();
90
- }
91
- if (shouldUpdate) {
92
- item.entity.onStoreChange('update');
93
- }
99
+ _define_property(this, "setFieldValue", function(name, value) {
100
+ var store = _define_property({}, name, value);
101
+ _this.setFieldsValue(store);
102
+ _this.notifyWatch([
103
+ name
104
+ ]);
105
+ });
106
+ _define_property(this, "setCallback", function(callback) {
107
+ _this.callbacks = _object_spread({}, _this.callbacks, callback);
94
108
  });
95
- needValidate && _this1.validateFields();
96
- });
97
- _define_property(this, "setCallback", function(callback) {
98
- _this.callbacks = _object_spread({}, _this.callbacks, callback);
99
- });
100
- _define_property(this, "validateFields", /*#__PURE__*/ function() {
101
- var _ref = _async_to_generator(function(nameList) {
102
- var filterEntitys, errs, _iteratorNormalCompletion, _didIteratorError, _iteratorError, _loop, _iterator, _step, err;
109
+ _define_property(this, "validateEntities", /*#__PURE__*/ function() {
110
+ var _ref = _async_to_generator(function(entity, errs) {
111
+ var _entity_props, name, _entity_props_rules, rules, descriptor, validator, _this_store, param, errors, _errs;
112
+ return _ts_generator(this, function(_state) {
113
+ switch(_state.label){
114
+ case 0:
115
+ _entity_props = entity.props, name = _entity_props.name, _entity_props_rules = _entity_props.rules, rules = _entity_props_rules === void 0 ? [] : _entity_props_rules;
116
+ if (!name) {
117
+ console.warn('Form field missing name property');
118
+ return [
119
+ 2
120
+ ];
121
+ }
122
+ descriptor = {};
123
+ if (rules.length) {
124
+ // 多条校验规则
125
+ if (rules.length > 1) {
126
+ descriptor[name] = [];
127
+ rules.forEach(function(v) {
128
+ descriptor[name].push(v);
129
+ });
130
+ } else {
131
+ descriptor[name] = rules[0];
132
+ }
133
+ }
134
+ validator = new Schema(descriptor);
135
+ _state.label = 1;
136
+ case 1:
137
+ _state.trys.push([
138
+ 1,
139
+ 3,
140
+ 4,
141
+ 5
142
+ ]);
143
+ return [
144
+ 4,
145
+ validator.validate(_define_property({}, name, (_this_store = _this2.store) === null || _this_store === void 0 ? void 0 : _this_store[name]))
146
+ ];
147
+ case 2:
148
+ _state.sent();
149
+ return [
150
+ 3,
151
+ 5
152
+ ];
153
+ case 3:
154
+ param = _state.sent();
155
+ errors = param.errors;
156
+ if (errors) {
157
+ ;
158
+ (_errs = errs).push.apply(_errs, _to_consumable_array(errors));
159
+ _this2.errors[name] = errors;
160
+ }
161
+ return [
162
+ 3,
163
+ 5
164
+ ];
165
+ case 4:
166
+ if (!errs || errs.length === 0) {
167
+ _this2.errors[name] = [];
168
+ }
169
+ return [
170
+ 7
171
+ ];
172
+ case 5:
173
+ entity.onStoreChange('validate');
174
+ return [
175
+ 2
176
+ ];
177
+ }
178
+ });
179
+ });
180
+ return function(entity, errs) {
181
+ return _ref.apply(this, arguments);
182
+ };
183
+ }());
184
+ _define_property(this, "validateFields", /*#__PURE__*/ function() {
185
+ var _ref = _async_to_generator(function(nameList) {
186
+ var filterEntities, errs;
187
+ return _ts_generator(this, function(_state) {
188
+ switch(_state.label){
189
+ case 0:
190
+ filterEntities = [];
191
+ // this.errors.length = 0
192
+ if (!nameList || nameList.length === 0) {
193
+ filterEntities = _this2.fieldEntities;
194
+ } else {
195
+ filterEntities = _this2.fieldEntities.filter(function(param) {
196
+ var name = param.props.name;
197
+ return nameList.includes(name);
198
+ });
199
+ }
200
+ errs = [];
201
+ return [
202
+ 4,
203
+ Promise.all(filterEntities.map(/*#__PURE__*/ function() {
204
+ var _ref = _async_to_generator(function(entity) {
205
+ return _ts_generator(this, function(_state) {
206
+ switch(_state.label){
207
+ case 0:
208
+ return [
209
+ 4,
210
+ _this2.validateEntities(entity, errs)
211
+ ];
212
+ case 1:
213
+ _state.sent();
214
+ return [
215
+ 2
216
+ ];
217
+ }
218
+ });
219
+ });
220
+ return function(entity) {
221
+ return _ref.apply(this, arguments);
222
+ };
223
+ }()))
224
+ ];
225
+ case 1:
226
+ _state.sent();
227
+ return [
228
+ 2,
229
+ errs
230
+ ];
231
+ }
232
+ });
233
+ });
234
+ return function(nameList) {
235
+ return _ref.apply(this, arguments);
236
+ };
237
+ }());
238
+ _define_property(this, "submit", /*#__PURE__*/ _async_to_generator(function() {
239
+ var errors, _this_callbacks_onFinish, _this_callbacks, _this_callbacks_onFinishFailed, _this_callbacks1;
103
240
  return _ts_generator(this, function(_state) {
104
241
  switch(_state.label){
105
242
  case 0:
106
- filterEntitys = [];
107
- errs = [];
108
- _this2.errors.length = 0;
109
- if (!nameList || nameList.length === 0) {
110
- filterEntitys = _this2.fieldEntities;
111
- } else {
112
- filterEntitys = _this2.fieldEntities.filter(function(param) {
113
- var name = param.props.name;
114
- return nameList.includes(name);
115
- });
116
- }
117
- _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
118
- _state.label = 1;
119
- case 1:
120
- _state.trys.push([
121
- 1,
122
- 6,
123
- 7,
124
- 8
125
- ]);
126
- _loop = function() {
127
- var entity, _entity_props, name, _entity_props_rules, rules, descriptor, validator, _this_store, param, errors, _errs;
128
- return _ts_generator(this, function(_state) {
129
- switch(_state.label){
130
- case 0:
131
- entity = _step.value;
132
- _entity_props = entity.props, name = _entity_props.name, _entity_props_rules = _entity_props.rules, rules = _entity_props_rules === void 0 ? [] : _entity_props_rules;
133
- descriptor = {};
134
- if (rules.length) {
135
- // 多条校验规则
136
- if (rules.length > 1) {
137
- descriptor[name] = [];
138
- rules.forEach(function(v) {
139
- descriptor[name].push(v);
140
- });
141
- } else {
142
- descriptor[name] = rules[0];
143
- }
144
- }
145
- validator = new Schema(descriptor);
146
- _state.label = 1;
147
- case 1:
148
- _state.trys.push([
149
- 1,
150
- 3,
151
- 4,
152
- 5
153
- ]);
154
- return [
155
- 4,
156
- validator.validate(_define_property({}, name, (_this_store = _this2.store) === null || _this_store === void 0 ? void 0 : _this_store[name]))
157
- ];
158
- case 2:
159
- _state.sent();
160
- return [
161
- 3,
162
- 5
163
- ];
164
- case 3:
165
- param = _state.sent();
166
- errors = param.errors;
167
- if (errors) {
168
- ;
169
- (_errs = errs).push.apply(_errs, _to_consumable_array(errors));
170
- _this2.errors[name] = errors;
171
- }
172
- return [
173
- 3,
174
- 5
175
- ];
176
- case 4:
177
- if (!errs || errs.length === 0) {
178
- _this2.errors[name] = [];
179
- }
180
- return [
181
- 7
182
- ];
183
- case 5:
184
- entity.onStoreChange('validate');
185
- return [
186
- 2
187
- ];
188
- }
189
- });
190
- };
191
- _iterator = filterEntitys[Symbol.iterator]();
192
- _state.label = 2;
193
- case 2:
194
- if (!!(_iteratorNormalCompletion = (_step = _iterator.next()).done)) return [
195
- 3,
196
- 5
197
- ];
198
243
  return [
199
- 5,
200
- _ts_values(_loop())
244
+ 4,
245
+ _this2.validateFields()
201
246
  ];
202
- case 3:
203
- _state.sent();
204
- _state.label = 4;
205
- case 4:
206
- _iteratorNormalCompletion = true;
247
+ case 1:
248
+ errors = _state.sent();
249
+ if (errors.length === 0) {
250
+ ;
251
+ (_this_callbacks_onFinish = (_this_callbacks = _this2.callbacks).onFinish) === null || _this_callbacks_onFinish === void 0 ? void 0 : _this_callbacks_onFinish.call(_this_callbacks, _this2.store);
252
+ } else if (errors.length > 0) {
253
+ ;
254
+ (_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);
255
+ }
207
256
  return [
208
- 3,
209
257
  2
210
258
  ];
211
- case 5:
212
- return [
213
- 3,
214
- 8
215
- ];
216
- case 6:
217
- err = _state.sent();
218
- _didIteratorError = true;
219
- _iteratorError = err;
220
- return [
221
- 3,
222
- 8
223
- ];
224
- case 7:
225
- try {
226
- if (!_iteratorNormalCompletion && _iterator.return != null) {
227
- _iterator.return();
228
- }
229
- } finally{
230
- if (_didIteratorError) {
231
- throw _iteratorError;
259
+ }
260
+ });
261
+ }));
262
+ _define_property(this, "resetFields", function(namePaths) {
263
+ if (namePaths && namePaths.length) {
264
+ namePaths.forEach(function(path) {
265
+ _this.errors[path] = null;
266
+ _this.fieldEntities.forEach(function(entity) {
267
+ var name = entity.props.name;
268
+ if (name === path) {
269
+ if (path in _this.initialValues) {
270
+ _this.updateStore(_define_property({}, path, _this.initialValues[path]));
271
+ } else {
272
+ delete _this.store[path];
232
273
  }
274
+ entity.onStoreChange('reset');
233
275
  }
234
- return [
235
- 7
236
- ];
237
- case 8:
238
- return [
239
- 2,
240
- errs
241
- ];
242
- }
276
+ });
277
+ });
278
+ } else {
279
+ var nextStore = merge({}, _this.initialValues);
280
+ _this.updateStore(nextStore);
281
+ _this.fieldEntities.forEach(function(entity) {
282
+ entity.onStoreChange('reset');
283
+ });
284
+ }
285
+ });
286
+ // 监听事件
287
+ _define_property(this, "registerUpdate", function(field, shouldUpdate) {
288
+ _this.updateList.push({
289
+ entity: field,
290
+ condition: shouldUpdate
243
291
  });
292
+ return function() {
293
+ _this.updateList = _this.updateList.filter(function(i) {
294
+ return i.entity !== field;
295
+ });
296
+ };
244
297
  });
245
- return function(nameList) {
246
- return _ref.apply(this, arguments);
247
- };
248
- }());
249
- _define_property(this, "submit", /*#__PURE__*/ _async_to_generator(function() {
250
- var errors, _this_callbacks_onFinish, _this_callbacks, _this_callbacks_onFinishFailed, _this_callbacks1;
251
- return _ts_generator(this, function(_state) {
252
- switch(_state.label){
253
- case 0:
254
- return [
255
- 4,
256
- _this2.validateFields()
257
- ];
258
- case 1:
259
- errors = _state.sent();
260
- if (errors.length === 0) {
261
- ;
262
- (_this_callbacks_onFinish = (_this_callbacks = _this2.callbacks).onFinish) === null || _this_callbacks_onFinish === void 0 ? void 0 : _this_callbacks_onFinish.call(_this_callbacks, _this2.store);
263
- } else if (errors.length > 0) {
264
- ;
265
- (_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);
266
- }
267
- return [
268
- 2
269
- ];
298
+ _define_property(this, "dispatch", function(param) {
299
+ var name = param.name;
300
+ _this.validateFields([
301
+ name
302
+ ]);
303
+ });
304
+ _define_property(this, "getInternal", function(key) {
305
+ if (key === SECRET) {
306
+ return {
307
+ registerField: _this.registerField,
308
+ setCallback: _this.setCallback,
309
+ setInitialValues: _this.setInitialValues,
310
+ dispatch: _this.dispatch,
311
+ store: _this.store,
312
+ fieldEntities: _this.fieldEntities,
313
+ registerUpdate: _this.registerUpdate,
314
+ registerWatch: _this.registerWatch
315
+ };
270
316
  }
271
317
  });
272
- }));
273
- _define_property(this, "resetFields", function() {
274
- _this.errors.length = 0;
275
- _this.store = _this.initialValues;
276
- _this.fieldEntities.forEach(function(entity) {
277
- entity.onStoreChange('reset');
318
+ _define_property(this, "getForm", function() {
319
+ return {
320
+ getFieldValue: _this.getFieldValue,
321
+ getFieldsValue: _this.getFieldsValue,
322
+ setFieldsValue: _this.setFieldsValue,
323
+ setFieldValue: _this.setFieldValue,
324
+ resetFields: _this.resetFields,
325
+ validateFields: _this.validateFields,
326
+ submit: _this.submit,
327
+ errors: _this.errors,
328
+ getInternal: _this.getInternal
329
+ };
278
330
  });
279
- });
280
- // 监听事件
281
- _define_property(this, "registerUpdate", function(field, shouldUpdate) {
282
- _this.updateList.push({
283
- entity: field,
284
- condition: shouldUpdate
331
+ _define_property(this, "watchList", []);
332
+ _define_property(this, "registerWatch", function(callback) {
333
+ _this.watchList.push(callback);
334
+ return function() {
335
+ _this.watchList = _this.watchList.filter(function(fn) {
336
+ return fn !== callback;
337
+ });
338
+ };
285
339
  });
286
- return function() {
287
- _this.updateList = _this.updateList.filter(function(i) {
288
- return i.entity !== field;
289
- });
340
+ _define_property(this, "notifyWatch", function() {
341
+ var namePath = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : [];
342
+ if (_this1.watchList.length) {
343
+ var allValues;
344
+ if (!namePath || namePath.length === 0) {
345
+ allValues = _this1.getFieldsValue(true);
346
+ } else {
347
+ allValues = _this1.getFieldsValue(namePath);
348
+ }
349
+ _this1.watchList.forEach(function(callback) {
350
+ callback(allValues, namePath);
351
+ });
352
+ }
353
+ });
354
+ this.callbacks = {
355
+ onFinish: function() {},
356
+ onFinishFailed: function() {}
290
357
  };
291
- });
292
- _define_property(this, "dispatch", function(param) {
293
- var name = param.name;
294
- _this.validateFields([
295
- name
296
- ]);
297
- });
298
- _define_property(this, "getInternal", function(key) {
299
- if (key === SECRET) {
300
- return {
301
- registerField: _this.registerField,
302
- setCallback: _this.setCallback,
303
- setInitialValues: _this.setInitialValues,
304
- dispatch: _this.dispatch,
305
- store: _this.store,
306
- fieldEntities: _this.fieldEntities,
307
- registerUpdate: _this.registerUpdate
308
- };
358
+ }
359
+ _create_class(FormStore, [
360
+ {
361
+ key: "updateStore",
362
+ value: function updateStore(nextStore) {
363
+ this.store = nextStore;
364
+ }
309
365
  }
310
- });
311
- _define_property(this, "getForm", function() {
312
- return {
313
- getFieldValue: _this.getFieldValue,
314
- getFieldsValue: _this.getFieldsValue,
315
- setFieldsValue: _this.setFieldsValue,
316
- resetFields: _this.resetFields,
317
- validateFields: _this.validateFields,
318
- submit: _this.submit,
319
- errors: _this.errors,
320
- getInternal: _this.getInternal
321
- };
322
- });
323
- this.callbacks = {
324
- onFinish: function() {},
325
- onFinishFailed: function() {}
326
- };
327
- };
366
+ ]);
367
+ return FormStore;
368
+ }();
328
369
  export var useForm = function(form) {
329
370
  var formRef = useRef();
330
371
  if (!formRef.current) {
@@ -339,3 +380,23 @@ export var useForm = function(form) {
339
380
  formRef.current
340
381
  ];
341
382
  };
383
+ export var useWatch = function(path, form) {
384
+ var formInstance = form.getInternal(SECRET);
385
+ var _useState = _sliced_to_array(useState(), 2), value = _useState[0], setValue = _useState[1];
386
+ useEffect(function() {
387
+ var unsubscribe = formInstance.registerWatch(function(data, namePath) {
388
+ var value = data[path];
389
+ setValue(value);
390
+ });
391
+ var initialValue = form.getFieldsValue(true);
392
+ if (value !== initialValue[path]) {
393
+ setValue(initialValue[path]);
394
+ }
395
+ return function() {
396
+ return unsubscribe();
397
+ };
398
+ }, [
399
+ form
400
+ ]);
401
+ return value;
402
+ };