@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.
- package/CHANGELOG.md +19 -0
- package/dist/cjs/hooks/use-client-rect.js +1 -0
- package/dist/cjs/packages/datepicker/style/style.css +70 -0
- package/dist/cjs/packages/datepickerview/style/index.js +1 -0
- package/dist/cjs/packages/datepickerview/style/style.css +70 -0
- package/dist/cjs/packages/fixednav/fixednav.js +20 -14
- package/dist/cjs/packages/fixednav/style/fixednav.scss +0 -15
- package/dist/cjs/packages/fixednav/style/index.js +1 -0
- package/dist/cjs/packages/fixednav/style/style.css +97 -14
- package/dist/cjs/packages/form/__tests__/merge.spec.d.ts +1 -0
- package/dist/cjs/packages/form/__tests__/merge.spec.js +545 -0
- package/dist/cjs/packages/form/form.d.ts +2 -0
- package/dist/cjs/packages/form/form.js +9 -2
- package/dist/cjs/packages/form/index.d.ts +2 -1
- package/dist/cjs/packages/form/index.js +1 -0
- package/dist/cjs/packages/form/style/form.scss +0 -66
- package/dist/cjs/packages/form/style/style.css +31 -21
- package/dist/cjs/packages/form/types.d.ts +2 -1
- package/dist/cjs/packages/form/useform.d.ts +2 -1
- package/dist/cjs/packages/form/useform.js +342 -278
- package/dist/cjs/packages/formitem/formitem.d.ts +1 -0
- package/dist/cjs/packages/formitem/formitem.js +55 -44
- package/dist/cjs/packages/formitem/style/formitem.scss +101 -21
- package/dist/cjs/packages/formitem/style/style.css +89 -12
- package/dist/cjs/packages/picker/style/index.js +1 -0
- package/dist/cjs/packages/picker/style/style.css +70 -0
- package/dist/cjs/packages/swipe/style/style.css +3 -3
- package/dist/cjs/packages/swipe/style/swipe.scss +7 -3
- package/dist/cjs/utils/get-rect-by-taro.d.ts +1 -1
- package/dist/cjs/utils/get-rect-by-taro.js +4 -1
- package/dist/cjs/utils/get-system-info.js +4 -5
- package/dist/cjs/utils/merge.d.ts +14 -0
- package/dist/cjs/utils/merge.js +98 -0
- package/dist/cjs/utils/platform-taro.js +4 -5
- package/dist/cjs/utils/to-array.d.ts +1 -0
- package/dist/cjs/utils/to-array.js +18 -0
- package/dist/es/hooks/use-client-rect.js +1 -0
- package/dist/es/packages/datepicker/style/style.css +70 -0
- package/dist/es/packages/datepickerview/style/index.js +1 -0
- package/dist/es/packages/datepickerview/style/style.css +70 -0
- package/dist/es/packages/fixednav/fixednav.js +20 -14
- package/dist/es/packages/fixednav/style/fixednav.scss +0 -15
- package/dist/es/packages/fixednav/style/index.js +1 -0
- package/dist/es/packages/fixednav/style/style.css +97 -14
- package/dist/es/packages/form/__tests__/merge.spec.d.ts +1 -0
- package/dist/es/packages/form/__tests__/merge.spec.js +541 -0
- package/dist/es/packages/form/form.d.ts +10 -0
- package/dist/es/packages/form/form.js +9 -2
- package/dist/es/packages/form/index.d.ts +2 -1
- package/dist/es/packages/form/index.js +2 -1
- package/dist/es/packages/form/style/form.scss +0 -66
- package/dist/es/packages/form/style/style.css +31 -21
- package/dist/es/packages/form/types.d.ts +2 -1
- package/dist/es/packages/form/useform.d.ts +2 -1
- package/dist/es/packages/form/useform.js +340 -279
- package/dist/es/packages/formitem/formitem.d.ts +1 -0
- package/dist/es/packages/formitem/formitem.js +55 -44
- package/dist/es/packages/formitem/style/formitem.scss +101 -21
- package/dist/es/packages/formitem/style/style.css +89 -12
- package/dist/es/packages/picker/style/index.js +1 -0
- package/dist/es/packages/picker/style/style.css +70 -0
- package/dist/es/packages/swipe/style/style.css +3 -3
- package/dist/es/packages/swipe/style/swipe.scss +7 -3
- package/dist/es/utils/get-rect-by-taro.d.ts +1 -1
- package/dist/es/utils/get-rect-by-taro.js +4 -1
- package/dist/es/utils/get-system-info.js +4 -4
- package/dist/es/utils/merge.d.ts +14 -0
- package/dist/es/utils/merge.js +68 -0
- package/dist/es/utils/platform-taro.js +4 -4
- package/dist/es/utils/to-array.d.ts +1 -0
- package/dist/es/utils/to-array.js +8 -0
- package/dist/nutui.react.umd.js +555 -422
- package/dist/style.css +1 -1
- package/dist/style.scss +41 -41
- 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 {
|
|
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
|
|
15
|
+
*/ var FormStore = /*#__PURE__*/ function() {
|
|
14
16
|
"use strict";
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
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
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
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
|
-
|
|
48
|
-
|
|
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
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
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
|
-
|
|
61
|
-
});
|
|
62
|
-
/**
|
|
62
|
+
/**
|
|
63
63
|
* 设置 form 的初始值,之后在 reset 的时候使用
|
|
64
64
|
* @param values
|
|
65
65
|
* @param init
|
|
66
|
-
*/ _define_property(this, "setInitialValues", function(
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
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
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
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
|
-
|
|
87
|
-
var
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
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
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
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
|
-
|
|
200
|
-
|
|
244
|
+
4,
|
|
245
|
+
_this2.validateFields()
|
|
201
246
|
];
|
|
202
|
-
case
|
|
203
|
-
_state.sent();
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
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
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
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
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
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
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
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
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
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
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
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
|
-
|
|
287
|
-
|
|
288
|
-
|
|
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
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
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
|
-
|
|
312
|
-
|
|
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
|
+
};
|