musae 1.0.21 → 1.0.22
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/dist/components/form/hooks/use-form.cjs +8 -7
- package/dist/components/form/hooks/use-form.d.ts +1 -1
- package/dist/components/form/hooks/use-form.mjs +5 -4
- package/dist/components/form/use-form.cjs +1 -0
- package/dist/components/form/use-form.mjs +1 -0
- package/dist/utils/form.cjs +54 -62
- package/dist/utils/form.d.ts +3 -3
- package/dist/utils/form.mjs +54 -62
- package/package.json +1 -1
|
@@ -3,27 +3,28 @@
|
|
|
3
3
|
var _objectSpread = require('@babel/runtime/helpers/objectSpread2');
|
|
4
4
|
var _objectWithoutProperties = require('@babel/runtime/helpers/objectWithoutProperties');
|
|
5
5
|
var relax = require('@aiszlab/relax');
|
|
6
|
-
var form = require('../../../utils/form.cjs');
|
|
7
6
|
var useForm$1 = require('../use-form.cjs');
|
|
8
7
|
|
|
9
|
-
var _excluded = ["value"];
|
|
8
|
+
var _excluded = ["value", "form"];
|
|
10
9
|
/**
|
|
11
10
|
* hook for `Form` used internal
|
|
12
11
|
*/
|
|
13
12
|
function useForm() {
|
|
14
13
|
var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
|
|
15
14
|
value = _ref.value,
|
|
15
|
+
_form = _ref.form,
|
|
16
16
|
usingForm = _objectWithoutProperties(_ref, _excluded);
|
|
17
|
-
var form
|
|
18
|
-
value: value
|
|
17
|
+
var form = useForm$1.useForm(_objectSpread({
|
|
18
|
+
value: value,
|
|
19
|
+
form: _form
|
|
19
20
|
}, usingForm));
|
|
20
21
|
relax.useUpdateEffect(function () {
|
|
21
22
|
if (relax.isUndefined(value)) {
|
|
22
23
|
return;
|
|
23
24
|
}
|
|
24
|
-
form
|
|
25
|
-
}, [value, form
|
|
26
|
-
return form
|
|
25
|
+
form.setFieldsValue(value);
|
|
26
|
+
}, [value, form]);
|
|
27
|
+
return form;
|
|
27
28
|
}
|
|
28
29
|
|
|
29
30
|
exports.useForm = useForm;
|
|
@@ -3,5 +3,5 @@ import { type FieldsValue } from "../../../utils/form";
|
|
|
3
3
|
/**
|
|
4
4
|
* hook for `Form` used internal
|
|
5
5
|
*/
|
|
6
|
-
declare function useForm<T extends FieldsValue>({ value, ...usingForm }?: UsingForm<T>): import("../../../types/form").UsedForm<T>;
|
|
6
|
+
declare function useForm<T extends FieldsValue>({ value, form: _form, ...usingForm }?: UsingForm<T>): import("../../../types/form").UsedForm<T>;
|
|
7
7
|
export { useForm };
|
|
@@ -1,25 +1,26 @@
|
|
|
1
1
|
import _objectSpread from '@babel/runtime/helpers/objectSpread2';
|
|
2
2
|
import _objectWithoutProperties from '@babel/runtime/helpers/objectWithoutProperties';
|
|
3
3
|
import { useUpdateEffect, isUndefined } from '@aiszlab/relax';
|
|
4
|
-
import { FORM_TOKEN } from '../../../utils/form.mjs';
|
|
5
4
|
import { useForm as useForm$1 } from '../use-form.mjs';
|
|
6
5
|
|
|
7
|
-
var _excluded = ["value"];
|
|
6
|
+
var _excluded = ["value", "form"];
|
|
8
7
|
/**
|
|
9
8
|
* hook for `Form` used internal
|
|
10
9
|
*/
|
|
11
10
|
function useForm() {
|
|
12
11
|
var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
|
|
13
12
|
value = _ref.value,
|
|
13
|
+
_form = _ref.form,
|
|
14
14
|
usingForm = _objectWithoutProperties(_ref, _excluded);
|
|
15
15
|
var form = useForm$1(_objectSpread({
|
|
16
|
-
value: value
|
|
16
|
+
value: value,
|
|
17
|
+
form: _form
|
|
17
18
|
}, usingForm));
|
|
18
19
|
useUpdateEffect(function () {
|
|
19
20
|
if (isUndefined(value)) {
|
|
20
21
|
return;
|
|
21
22
|
}
|
|
22
|
-
form
|
|
23
|
+
form.setFieldsValue(value);
|
|
23
24
|
}, [value, form]);
|
|
24
25
|
return form;
|
|
25
26
|
}
|
|
@@ -23,6 +23,7 @@ function useForm() {
|
|
|
23
23
|
});
|
|
24
24
|
var form$1 = relax.useDefault(function () {
|
|
25
25
|
var _usedForm$FORM_TOKEN;
|
|
26
|
+
console.log("_usedForm?.[FORM_TOKEN]===========", _usedForm === null || _usedForm === void 0 ? void 0 : _usedForm[form.FORM_TOKEN]);
|
|
26
27
|
var _form = (_usedForm$FORM_TOKEN = _usedForm === null || _usedForm === void 0 ? void 0 : _usedForm[form.FORM_TOKEN]) !== null && _usedForm$FORM_TOKEN !== void 0 ? _usedForm$FORM_TOKEN : new form.Form();
|
|
27
28
|
_form.defaultValue = defaultValue;
|
|
28
29
|
_form.value = value;
|
|
@@ -21,6 +21,7 @@ function useForm() {
|
|
|
21
21
|
});
|
|
22
22
|
var form = useDefault(function () {
|
|
23
23
|
var _usedForm$FORM_TOKEN;
|
|
24
|
+
console.log("_usedForm?.[FORM_TOKEN]===========", _usedForm === null || _usedForm === void 0 ? void 0 : _usedForm[FORM_TOKEN]);
|
|
24
25
|
var _form = (_usedForm$FORM_TOKEN = _usedForm === null || _usedForm === void 0 ? void 0 : _usedForm[FORM_TOKEN]) !== null && _usedForm$FORM_TOKEN !== void 0 ? _usedForm$FORM_TOKEN : new Form();
|
|
25
26
|
_form.defaultValue = defaultValue;
|
|
26
27
|
_form.value = value;
|
package/dist/utils/form.cjs
CHANGED
|
@@ -15,15 +15,16 @@ var rxjs = require('rxjs');
|
|
|
15
15
|
* unique symbols
|
|
16
16
|
*/
|
|
17
17
|
var FORM_TOKEN = Symbol("FORM");
|
|
18
|
-
var
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
18
|
+
var FORM_EVENT = {
|
|
19
|
+
change: "change",
|
|
20
|
+
set_value: "set_value",
|
|
21
|
+
set_fields_value: "set_fields_value",
|
|
22
|
+
set_field_value: "set_field_value",
|
|
23
|
+
validate: "validate",
|
|
24
|
+
initialize: "initialize",
|
|
25
|
+
clear: "clear",
|
|
26
|
+
reset: "reset"
|
|
27
|
+
};
|
|
27
28
|
/**
|
|
28
29
|
* form instance
|
|
29
30
|
*/
|
|
@@ -42,28 +43,25 @@ var Form = /*#__PURE__*/function () {
|
|
|
42
43
|
_classPrivateFieldInitSpec(this, _state, void 0);
|
|
43
44
|
_classPrivateFieldInitSpec(this, _state$, void 0);
|
|
44
45
|
_classPrivateFieldInitSpec(this, _onChange, void 0);
|
|
45
|
-
_classPrivateFieldSet(_defaultValue, this, {});
|
|
46
46
|
_classPrivateFieldSet(_fields, this, new Map());
|
|
47
47
|
_classPrivateFieldSet(_state, this, {
|
|
48
|
-
value: {},
|
|
49
48
|
error: {}
|
|
50
49
|
});
|
|
51
50
|
_classPrivateFieldSet(_onChange, this, null);
|
|
52
51
|
_classPrivateFieldSet(_state$, this, new rxjs.BehaviorSubject({
|
|
53
|
-
|
|
54
|
-
names: [],
|
|
52
|
+
event: FORM_EVENT.initialize,
|
|
55
53
|
value: (_classPrivateFieldGet2 = _classPrivateFieldGet(_state, this).value) !== null && _classPrivateFieldGet2 !== void 0 ? _classPrivateFieldGet2 : {},
|
|
56
54
|
error: _classPrivateFieldGet(_state, this).error
|
|
57
55
|
}));
|
|
58
56
|
_classPrivateFieldGet(_state$, this).subscribe(function (_ref) {
|
|
59
|
-
var _classPrivateFieldGet3;
|
|
60
|
-
var
|
|
57
|
+
var _names$size, _classPrivateFieldGet3;
|
|
58
|
+
var event = _ref.event,
|
|
61
59
|
names = _ref.names,
|
|
62
60
|
value = _ref.value;
|
|
63
|
-
if (names.
|
|
64
|
-
if (
|
|
61
|
+
if (((_names$size = names === null || names === void 0 ? void 0 : names.size) !== null && _names$size !== void 0 ? _names$size : 0) === 0) return;
|
|
62
|
+
if (event !== FORM_EVENT.change) return;
|
|
65
63
|
// value change, handle `onChange` callback
|
|
66
|
-
(_classPrivateFieldGet3 = _classPrivateFieldGet(_onChange, _this)) === null || _classPrivateFieldGet3 === void 0 || _classPrivateFieldGet3.call(_this, _objectSpread({}, value), names);
|
|
64
|
+
(_classPrivateFieldGet3 = _classPrivateFieldGet(_onChange, _this)) === null || _classPrivateFieldGet3 === void 0 || _classPrivateFieldGet3.call(_this, _objectSpread({}, value), Array.from(names !== null && names !== void 0 ? names : []));
|
|
67
65
|
});
|
|
68
66
|
}
|
|
69
67
|
/**
|
|
@@ -80,12 +78,11 @@ var Form = /*#__PURE__*/function () {
|
|
|
80
78
|
}, {
|
|
81
79
|
key: "value",
|
|
82
80
|
set: function set(value) {
|
|
83
|
-
var _ref2;
|
|
84
|
-
_classPrivateFieldGet(_state, this).value = (_ref2 = value !== null && value !== void 0 ? value : _classPrivateFieldGet(
|
|
81
|
+
var _ref2, _classPrivateFieldGet4;
|
|
82
|
+
_classPrivateFieldGet(_state, this).value = (_ref2 = value !== null && value !== void 0 ? value : _classPrivateFieldGet(_state, this).value) !== null && _ref2 !== void 0 ? _ref2 : _classPrivateFieldGet(_defaultValue, this);
|
|
85
83
|
_classPrivateFieldGet(_state$, this).next({
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
value: _classPrivateFieldGet(_state, this).value,
|
|
84
|
+
event: FORM_EVENT.set_value,
|
|
85
|
+
value: (_classPrivateFieldGet4 = _classPrivateFieldGet(_state, this).value) !== null && _classPrivateFieldGet4 !== void 0 ? _classPrivateFieldGet4 : {},
|
|
89
86
|
error: _classPrivateFieldGet(_state, this).error
|
|
90
87
|
});
|
|
91
88
|
}
|
|
@@ -114,9 +111,9 @@ var Form = /*#__PURE__*/function () {
|
|
|
114
111
|
// only listen `name` related to `register` field
|
|
115
112
|
rxjs.filter(function (_ref4) {
|
|
116
113
|
var names = _ref4.names;
|
|
117
|
-
return names.
|
|
114
|
+
return !names || names.size === 0 || names.has(name);
|
|
118
115
|
})).subscribe(function (_ref5) {
|
|
119
|
-
var
|
|
116
|
+
var event = _ref5.event,
|
|
120
117
|
value = _ref5.value,
|
|
121
118
|
error = _ref5.error;
|
|
122
119
|
// callback field state
|
|
@@ -125,7 +122,7 @@ var Form = /*#__PURE__*/function () {
|
|
|
125
122
|
error: error[name]
|
|
126
123
|
});
|
|
127
124
|
// validate
|
|
128
|
-
if (
|
|
125
|
+
if (event === FORM_EVENT.validate) {
|
|
129
126
|
onValidate(error[name]);
|
|
130
127
|
}
|
|
131
128
|
});
|
|
@@ -141,16 +138,12 @@ var Form = /*#__PURE__*/function () {
|
|
|
141
138
|
}, {
|
|
142
139
|
key: "watch",
|
|
143
140
|
value: function watch(name, onChange) {
|
|
144
|
-
var _subscription = _classPrivateFieldGet(_state$, this).pipe(
|
|
145
|
-
// only listen `name` related to `register` field
|
|
146
|
-
rxjs.filter(function (_ref6) {
|
|
141
|
+
var _subscription = _classPrivateFieldGet(_state$, this).pipe(rxjs.filter(function (_ref6) {
|
|
147
142
|
var names = _ref6.names;
|
|
148
|
-
return names.
|
|
149
|
-
}),
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
var source = _ref7.source;
|
|
153
|
-
return source !== ChangingSource.Validate;
|
|
143
|
+
return !names || names.size === 0 || names.has(name);
|
|
144
|
+
}), rxjs.filter(function (_ref7) {
|
|
145
|
+
var event = _ref7.event;
|
|
146
|
+
return event !== FORM_EVENT.validate;
|
|
154
147
|
})).subscribe(function (_ref8) {
|
|
155
148
|
var value = _ref8.value;
|
|
156
149
|
onChange(value[name]);
|
|
@@ -165,8 +158,8 @@ var Form = /*#__PURE__*/function () {
|
|
|
165
158
|
}, {
|
|
166
159
|
key: "getFieldValue",
|
|
167
160
|
value: function getFieldValue(name) {
|
|
168
|
-
var
|
|
169
|
-
return (
|
|
161
|
+
var _classPrivateFieldGet5;
|
|
162
|
+
return (_classPrivateFieldGet5 = _classPrivateFieldGet(_state, this).value) === null || _classPrivateFieldGet5 === void 0 ? void 0 : _classPrivateFieldGet5[name];
|
|
170
163
|
}
|
|
171
164
|
/**
|
|
172
165
|
* get fields value
|
|
@@ -176,8 +169,8 @@ var Form = /*#__PURE__*/function () {
|
|
|
176
169
|
value: function getFieldsValue() {
|
|
177
170
|
var _this3 = this;
|
|
178
171
|
return _classPrivateFieldGet(_fields, this).keys().reduce(function (value, name) {
|
|
179
|
-
var
|
|
180
|
-
value[name] = (
|
|
172
|
+
var _classPrivateFieldGet6;
|
|
173
|
+
value[name] = (_classPrivateFieldGet6 = _classPrivateFieldGet(_state, _this3).value) === null || _classPrivateFieldGet6 === void 0 ? void 0 : _classPrivateFieldGet6[name];
|
|
181
174
|
return value;
|
|
182
175
|
}, {});
|
|
183
176
|
}
|
|
@@ -191,7 +184,7 @@ var Form = /*#__PURE__*/function () {
|
|
|
191
184
|
value: (function () {
|
|
192
185
|
var _validate = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee3() {
|
|
193
186
|
var _this4 = this,
|
|
194
|
-
|
|
187
|
+
_classPrivateFieldGet1;
|
|
195
188
|
var names,
|
|
196
189
|
validated,
|
|
197
190
|
_args3 = arguments;
|
|
@@ -202,12 +195,12 @@ var Form = /*#__PURE__*/function () {
|
|
|
202
195
|
_context3.n = 1;
|
|
203
196
|
return Promise.all(names.map(/*#__PURE__*/function () {
|
|
204
197
|
var _ref9 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee2(name) {
|
|
205
|
-
var
|
|
198
|
+
var _classPrivateFieldGet7, _classPrivateFieldGet8, _classPrivateFieldGet9;
|
|
206
199
|
var rules, error;
|
|
207
200
|
return _regenerator().w(function (_context2) {
|
|
208
201
|
while (1) switch (_context2.n) {
|
|
209
202
|
case 0:
|
|
210
|
-
rules = (
|
|
203
|
+
rules = (_classPrivateFieldGet7 = (_classPrivateFieldGet8 = _classPrivateFieldGet(_fields, _this4).get(name)) === null || _classPrivateFieldGet8 === void 0 || (_classPrivateFieldGet9 = _classPrivateFieldGet8.rules) === null || _classPrivateFieldGet9 === void 0 ? void 0 : _classPrivateFieldGet9.call(_classPrivateFieldGet8)) !== null && _classPrivateFieldGet7 !== void 0 ? _classPrivateFieldGet7 : []; // no valid rule
|
|
211
204
|
if (!(rules.length === 0)) {
|
|
212
205
|
_context2.n = 1;
|
|
213
206
|
break;
|
|
@@ -224,8 +217,8 @@ var Form = /*#__PURE__*/function () {
|
|
|
224
217
|
validate = _ref0.validate, message = _ref0.message;
|
|
225
218
|
_context.n = 1;
|
|
226
219
|
return Promise["try"](function () {
|
|
227
|
-
var _validate2,
|
|
228
|
-
return (_validate2 = validate((
|
|
220
|
+
var _validate2, _classPrivateFieldGet0;
|
|
221
|
+
return (_validate2 = validate((_classPrivateFieldGet0 = _classPrivateFieldGet(_state, _this4).value) === null || _classPrivateFieldGet0 === void 0 ? void 0 : _classPrivateFieldGet0[name])) !== null && _validate2 !== void 0 ? _validate2 : null;
|
|
229
222
|
})["catch"](function (error) {
|
|
230
223
|
console.error(error);
|
|
231
224
|
return false;
|
|
@@ -261,9 +254,9 @@ var Form = /*#__PURE__*/function () {
|
|
|
261
254
|
// notify state change
|
|
262
255
|
// only notify `error`
|
|
263
256
|
_classPrivateFieldGet(_state$, this).next({
|
|
264
|
-
|
|
265
|
-
names: names,
|
|
266
|
-
value: (
|
|
257
|
+
event: FORM_EVENT.validate,
|
|
258
|
+
names: new Set(names),
|
|
259
|
+
value: (_classPrivateFieldGet1 = _classPrivateFieldGet(_state, this).value) !== null && _classPrivateFieldGet1 !== void 0 ? _classPrivateFieldGet1 : {},
|
|
267
260
|
error: _classPrivateFieldGet(_state, this).error
|
|
268
261
|
});
|
|
269
262
|
return _context3.a(2, validated.every(function (error) {
|
|
@@ -284,13 +277,13 @@ var Form = /*#__PURE__*/function () {
|
|
|
284
277
|
}, {
|
|
285
278
|
key: "setFieldValue",
|
|
286
279
|
value: function setFieldValue(name, value) {
|
|
287
|
-
var
|
|
288
|
-
(
|
|
280
|
+
var _classPrivateFieldGet10, _classPrivateFieldGet11;
|
|
281
|
+
(_classPrivateFieldGet11 = (_classPrivateFieldGet10 = _classPrivateFieldGet(_state, this)).value) !== null && _classPrivateFieldGet11 !== void 0 ? _classPrivateFieldGet11 : _classPrivateFieldGet10.value = {};
|
|
289
282
|
_classPrivateFieldGet(_state, this).value[name] = value;
|
|
290
283
|
_classPrivateFieldGet(_state, this).error[name] = null;
|
|
291
284
|
_classPrivateFieldGet(_state$, this).next({
|
|
292
|
-
|
|
293
|
-
names: [name],
|
|
285
|
+
event: FORM_EVENT.set_field_value,
|
|
286
|
+
names: new Set([name]),
|
|
294
287
|
value: _classPrivateFieldGet(_state, this).value,
|
|
295
288
|
error: _classPrivateFieldGet(_state, this).error
|
|
296
289
|
});
|
|
@@ -307,8 +300,8 @@ var Form = /*#__PURE__*/function () {
|
|
|
307
300
|
return [name, null];
|
|
308
301
|
})));
|
|
309
302
|
_classPrivateFieldGet(_state$, this).next({
|
|
310
|
-
|
|
311
|
-
names: names,
|
|
303
|
+
event: FORM_EVENT.set_fields_value,
|
|
304
|
+
names: new Set(names),
|
|
312
305
|
value: _classPrivateFieldGet(_state, this).value,
|
|
313
306
|
error: _classPrivateFieldGet(_state, this).error
|
|
314
307
|
});
|
|
@@ -319,12 +312,12 @@ var Form = /*#__PURE__*/function () {
|
|
|
319
312
|
}, {
|
|
320
313
|
key: "reset",
|
|
321
314
|
value: function reset() {
|
|
315
|
+
var _classPrivateFieldGet12;
|
|
322
316
|
_classPrivateFieldGet(_state, this).value = _classPrivateFieldGet(_defaultValue, this);
|
|
323
317
|
_classPrivateFieldGet(_state, this).error = {};
|
|
324
318
|
_classPrivateFieldGet(_state$, this).next({
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
value: _classPrivateFieldGet(_state, this).value,
|
|
319
|
+
event: FORM_EVENT.reset,
|
|
320
|
+
value: (_classPrivateFieldGet12 = _classPrivateFieldGet(_state, this).value) !== null && _classPrivateFieldGet12 !== void 0 ? _classPrivateFieldGet12 : {},
|
|
328
321
|
error: _classPrivateFieldGet(_state, this).error
|
|
329
322
|
});
|
|
330
323
|
}
|
|
@@ -337,8 +330,7 @@ var Form = /*#__PURE__*/function () {
|
|
|
337
330
|
_classPrivateFieldGet(_state, this).value = {};
|
|
338
331
|
_classPrivateFieldGet(_state, this).error = {};
|
|
339
332
|
_classPrivateFieldGet(_state$, this).next({
|
|
340
|
-
|
|
341
|
-
names: [],
|
|
333
|
+
event: FORM_EVENT.clear,
|
|
342
334
|
value: _classPrivateFieldGet(_state, this).value,
|
|
343
335
|
error: _classPrivateFieldGet(_state, this).error
|
|
344
336
|
});
|
|
@@ -352,16 +344,16 @@ var Form = /*#__PURE__*/function () {
|
|
|
352
344
|
key: "change",
|
|
353
345
|
value: (function () {
|
|
354
346
|
var _change = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee4(name, value) {
|
|
355
|
-
var
|
|
347
|
+
var _classPrivateFieldGet13, _classPrivateFieldGet14;
|
|
356
348
|
return _regenerator().w(function (_context4) {
|
|
357
349
|
while (1) switch (_context4.n) {
|
|
358
350
|
case 0:
|
|
359
|
-
(
|
|
351
|
+
(_classPrivateFieldGet14 = (_classPrivateFieldGet13 = _classPrivateFieldGet(_state, this)).value) !== null && _classPrivateFieldGet14 !== void 0 ? _classPrivateFieldGet14 : _classPrivateFieldGet13.value = {};
|
|
360
352
|
_classPrivateFieldGet(_state, this).value[name] = value;
|
|
361
353
|
_classPrivateFieldGet(_state, this).error[name] = null;
|
|
362
354
|
_classPrivateFieldGet(_state$, this).next({
|
|
363
|
-
|
|
364
|
-
names: [name],
|
|
355
|
+
event: FORM_EVENT.change,
|
|
356
|
+
names: new Set([name]),
|
|
365
357
|
value: _classPrivateFieldGet(_state, this).value,
|
|
366
358
|
error: _classPrivateFieldGet(_state, this).error
|
|
367
359
|
});
|
package/dist/utils/form.d.ts
CHANGED
|
@@ -42,11 +42,11 @@ export declare class Form<T extends FieldsValue> {
|
|
|
42
42
|
/**
|
|
43
43
|
* defaultValue setter
|
|
44
44
|
*/
|
|
45
|
-
set defaultValue(defaultValue: Partial<T
|
|
45
|
+
set defaultValue(defaultValue: Partialable<Partial<T>>);
|
|
46
46
|
/**
|
|
47
47
|
* value setter
|
|
48
48
|
*/
|
|
49
|
-
set value(value: Partial<T
|
|
49
|
+
set value(value: Partialable<Partial<T>>);
|
|
50
50
|
/**
|
|
51
51
|
* change handler setter
|
|
52
52
|
*/
|
|
@@ -63,7 +63,7 @@ export declare class Form<T extends FieldsValue> {
|
|
|
63
63
|
/**
|
|
64
64
|
* get field value
|
|
65
65
|
*/
|
|
66
|
-
getFieldValue(name: keyof T): Partial<T>[keyof T];
|
|
66
|
+
getFieldValue(name: keyof T): Partial<T>[keyof T] | undefined;
|
|
67
67
|
/**
|
|
68
68
|
* get fields value
|
|
69
69
|
*/
|
package/dist/utils/form.mjs
CHANGED
|
@@ -13,15 +13,16 @@ import { filter, BehaviorSubject } from 'rxjs';
|
|
|
13
13
|
* unique symbols
|
|
14
14
|
*/
|
|
15
15
|
var FORM_TOKEN = Symbol("FORM");
|
|
16
|
-
var
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
16
|
+
var FORM_EVENT = {
|
|
17
|
+
change: "change",
|
|
18
|
+
set_value: "set_value",
|
|
19
|
+
set_fields_value: "set_fields_value",
|
|
20
|
+
set_field_value: "set_field_value",
|
|
21
|
+
validate: "validate",
|
|
22
|
+
initialize: "initialize",
|
|
23
|
+
clear: "clear",
|
|
24
|
+
reset: "reset"
|
|
25
|
+
};
|
|
25
26
|
/**
|
|
26
27
|
* form instance
|
|
27
28
|
*/
|
|
@@ -40,28 +41,25 @@ var Form = /*#__PURE__*/function () {
|
|
|
40
41
|
_classPrivateFieldInitSpec(this, _state, void 0);
|
|
41
42
|
_classPrivateFieldInitSpec(this, _state$, void 0);
|
|
42
43
|
_classPrivateFieldInitSpec(this, _onChange, void 0);
|
|
43
|
-
_classPrivateFieldSet(_defaultValue, this, {});
|
|
44
44
|
_classPrivateFieldSet(_fields, this, new Map());
|
|
45
45
|
_classPrivateFieldSet(_state, this, {
|
|
46
|
-
value: {},
|
|
47
46
|
error: {}
|
|
48
47
|
});
|
|
49
48
|
_classPrivateFieldSet(_onChange, this, null);
|
|
50
49
|
_classPrivateFieldSet(_state$, this, new BehaviorSubject({
|
|
51
|
-
|
|
52
|
-
names: [],
|
|
50
|
+
event: FORM_EVENT.initialize,
|
|
53
51
|
value: (_classPrivateFieldGet2 = _classPrivateFieldGet(_state, this).value) !== null && _classPrivateFieldGet2 !== void 0 ? _classPrivateFieldGet2 : {},
|
|
54
52
|
error: _classPrivateFieldGet(_state, this).error
|
|
55
53
|
}));
|
|
56
54
|
_classPrivateFieldGet(_state$, this).subscribe(function (_ref) {
|
|
57
|
-
var _classPrivateFieldGet3;
|
|
58
|
-
var
|
|
55
|
+
var _names$size, _classPrivateFieldGet3;
|
|
56
|
+
var event = _ref.event,
|
|
59
57
|
names = _ref.names,
|
|
60
58
|
value = _ref.value;
|
|
61
|
-
if (names.
|
|
62
|
-
if (
|
|
59
|
+
if (((_names$size = names === null || names === void 0 ? void 0 : names.size) !== null && _names$size !== void 0 ? _names$size : 0) === 0) return;
|
|
60
|
+
if (event !== FORM_EVENT.change) return;
|
|
63
61
|
// value change, handle `onChange` callback
|
|
64
|
-
(_classPrivateFieldGet3 = _classPrivateFieldGet(_onChange, _this)) === null || _classPrivateFieldGet3 === void 0 || _classPrivateFieldGet3.call(_this, _objectSpread({}, value), names);
|
|
62
|
+
(_classPrivateFieldGet3 = _classPrivateFieldGet(_onChange, _this)) === null || _classPrivateFieldGet3 === void 0 || _classPrivateFieldGet3.call(_this, _objectSpread({}, value), Array.from(names !== null && names !== void 0 ? names : []));
|
|
65
63
|
});
|
|
66
64
|
}
|
|
67
65
|
/**
|
|
@@ -78,12 +76,11 @@ var Form = /*#__PURE__*/function () {
|
|
|
78
76
|
}, {
|
|
79
77
|
key: "value",
|
|
80
78
|
set: function set(value) {
|
|
81
|
-
var _ref2;
|
|
82
|
-
_classPrivateFieldGet(_state, this).value = (_ref2 = value !== null && value !== void 0 ? value : _classPrivateFieldGet(
|
|
79
|
+
var _ref2, _classPrivateFieldGet4;
|
|
80
|
+
_classPrivateFieldGet(_state, this).value = (_ref2 = value !== null && value !== void 0 ? value : _classPrivateFieldGet(_state, this).value) !== null && _ref2 !== void 0 ? _ref2 : _classPrivateFieldGet(_defaultValue, this);
|
|
83
81
|
_classPrivateFieldGet(_state$, this).next({
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
value: _classPrivateFieldGet(_state, this).value,
|
|
82
|
+
event: FORM_EVENT.set_value,
|
|
83
|
+
value: (_classPrivateFieldGet4 = _classPrivateFieldGet(_state, this).value) !== null && _classPrivateFieldGet4 !== void 0 ? _classPrivateFieldGet4 : {},
|
|
87
84
|
error: _classPrivateFieldGet(_state, this).error
|
|
88
85
|
});
|
|
89
86
|
}
|
|
@@ -112,9 +109,9 @@ var Form = /*#__PURE__*/function () {
|
|
|
112
109
|
// only listen `name` related to `register` field
|
|
113
110
|
filter(function (_ref4) {
|
|
114
111
|
var names = _ref4.names;
|
|
115
|
-
return names.
|
|
112
|
+
return !names || names.size === 0 || names.has(name);
|
|
116
113
|
})).subscribe(function (_ref5) {
|
|
117
|
-
var
|
|
114
|
+
var event = _ref5.event,
|
|
118
115
|
value = _ref5.value,
|
|
119
116
|
error = _ref5.error;
|
|
120
117
|
// callback field state
|
|
@@ -123,7 +120,7 @@ var Form = /*#__PURE__*/function () {
|
|
|
123
120
|
error: error[name]
|
|
124
121
|
});
|
|
125
122
|
// validate
|
|
126
|
-
if (
|
|
123
|
+
if (event === FORM_EVENT.validate) {
|
|
127
124
|
onValidate(error[name]);
|
|
128
125
|
}
|
|
129
126
|
});
|
|
@@ -139,16 +136,12 @@ var Form = /*#__PURE__*/function () {
|
|
|
139
136
|
}, {
|
|
140
137
|
key: "watch",
|
|
141
138
|
value: function watch(name, onChange) {
|
|
142
|
-
var _subscription = _classPrivateFieldGet(_state$, this).pipe(
|
|
143
|
-
// only listen `name` related to `register` field
|
|
144
|
-
filter(function (_ref6) {
|
|
139
|
+
var _subscription = _classPrivateFieldGet(_state$, this).pipe(filter(function (_ref6) {
|
|
145
140
|
var names = _ref6.names;
|
|
146
|
-
return names.
|
|
147
|
-
}),
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
var source = _ref7.source;
|
|
151
|
-
return source !== ChangingSource.Validate;
|
|
141
|
+
return !names || names.size === 0 || names.has(name);
|
|
142
|
+
}), filter(function (_ref7) {
|
|
143
|
+
var event = _ref7.event;
|
|
144
|
+
return event !== FORM_EVENT.validate;
|
|
152
145
|
})).subscribe(function (_ref8) {
|
|
153
146
|
var value = _ref8.value;
|
|
154
147
|
onChange(value[name]);
|
|
@@ -163,8 +156,8 @@ var Form = /*#__PURE__*/function () {
|
|
|
163
156
|
}, {
|
|
164
157
|
key: "getFieldValue",
|
|
165
158
|
value: function getFieldValue(name) {
|
|
166
|
-
var
|
|
167
|
-
return (
|
|
159
|
+
var _classPrivateFieldGet5;
|
|
160
|
+
return (_classPrivateFieldGet5 = _classPrivateFieldGet(_state, this).value) === null || _classPrivateFieldGet5 === void 0 ? void 0 : _classPrivateFieldGet5[name];
|
|
168
161
|
}
|
|
169
162
|
/**
|
|
170
163
|
* get fields value
|
|
@@ -174,8 +167,8 @@ var Form = /*#__PURE__*/function () {
|
|
|
174
167
|
value: function getFieldsValue() {
|
|
175
168
|
var _this3 = this;
|
|
176
169
|
return _classPrivateFieldGet(_fields, this).keys().reduce(function (value, name) {
|
|
177
|
-
var
|
|
178
|
-
value[name] = (
|
|
170
|
+
var _classPrivateFieldGet6;
|
|
171
|
+
value[name] = (_classPrivateFieldGet6 = _classPrivateFieldGet(_state, _this3).value) === null || _classPrivateFieldGet6 === void 0 ? void 0 : _classPrivateFieldGet6[name];
|
|
179
172
|
return value;
|
|
180
173
|
}, {});
|
|
181
174
|
}
|
|
@@ -189,7 +182,7 @@ var Form = /*#__PURE__*/function () {
|
|
|
189
182
|
value: (function () {
|
|
190
183
|
var _validate = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee3() {
|
|
191
184
|
var _this4 = this,
|
|
192
|
-
|
|
185
|
+
_classPrivateFieldGet1;
|
|
193
186
|
var names,
|
|
194
187
|
validated,
|
|
195
188
|
_args3 = arguments;
|
|
@@ -200,12 +193,12 @@ var Form = /*#__PURE__*/function () {
|
|
|
200
193
|
_context3.n = 1;
|
|
201
194
|
return Promise.all(names.map(/*#__PURE__*/function () {
|
|
202
195
|
var _ref9 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee2(name) {
|
|
203
|
-
var
|
|
196
|
+
var _classPrivateFieldGet7, _classPrivateFieldGet8, _classPrivateFieldGet9;
|
|
204
197
|
var rules, error;
|
|
205
198
|
return _regenerator().w(function (_context2) {
|
|
206
199
|
while (1) switch (_context2.n) {
|
|
207
200
|
case 0:
|
|
208
|
-
rules = (
|
|
201
|
+
rules = (_classPrivateFieldGet7 = (_classPrivateFieldGet8 = _classPrivateFieldGet(_fields, _this4).get(name)) === null || _classPrivateFieldGet8 === void 0 || (_classPrivateFieldGet9 = _classPrivateFieldGet8.rules) === null || _classPrivateFieldGet9 === void 0 ? void 0 : _classPrivateFieldGet9.call(_classPrivateFieldGet8)) !== null && _classPrivateFieldGet7 !== void 0 ? _classPrivateFieldGet7 : []; // no valid rule
|
|
209
202
|
if (!(rules.length === 0)) {
|
|
210
203
|
_context2.n = 1;
|
|
211
204
|
break;
|
|
@@ -222,8 +215,8 @@ var Form = /*#__PURE__*/function () {
|
|
|
222
215
|
validate = _ref0.validate, message = _ref0.message;
|
|
223
216
|
_context.n = 1;
|
|
224
217
|
return Promise["try"](function () {
|
|
225
|
-
var _validate2,
|
|
226
|
-
return (_validate2 = validate((
|
|
218
|
+
var _validate2, _classPrivateFieldGet0;
|
|
219
|
+
return (_validate2 = validate((_classPrivateFieldGet0 = _classPrivateFieldGet(_state, _this4).value) === null || _classPrivateFieldGet0 === void 0 ? void 0 : _classPrivateFieldGet0[name])) !== null && _validate2 !== void 0 ? _validate2 : null;
|
|
227
220
|
})["catch"](function (error) {
|
|
228
221
|
console.error(error);
|
|
229
222
|
return false;
|
|
@@ -259,9 +252,9 @@ var Form = /*#__PURE__*/function () {
|
|
|
259
252
|
// notify state change
|
|
260
253
|
// only notify `error`
|
|
261
254
|
_classPrivateFieldGet(_state$, this).next({
|
|
262
|
-
|
|
263
|
-
names: names,
|
|
264
|
-
value: (
|
|
255
|
+
event: FORM_EVENT.validate,
|
|
256
|
+
names: new Set(names),
|
|
257
|
+
value: (_classPrivateFieldGet1 = _classPrivateFieldGet(_state, this).value) !== null && _classPrivateFieldGet1 !== void 0 ? _classPrivateFieldGet1 : {},
|
|
265
258
|
error: _classPrivateFieldGet(_state, this).error
|
|
266
259
|
});
|
|
267
260
|
return _context3.a(2, validated.every(function (error) {
|
|
@@ -282,13 +275,13 @@ var Form = /*#__PURE__*/function () {
|
|
|
282
275
|
}, {
|
|
283
276
|
key: "setFieldValue",
|
|
284
277
|
value: function setFieldValue(name, value) {
|
|
285
|
-
var
|
|
286
|
-
(
|
|
278
|
+
var _classPrivateFieldGet10, _classPrivateFieldGet11;
|
|
279
|
+
(_classPrivateFieldGet11 = (_classPrivateFieldGet10 = _classPrivateFieldGet(_state, this)).value) !== null && _classPrivateFieldGet11 !== void 0 ? _classPrivateFieldGet11 : _classPrivateFieldGet10.value = {};
|
|
287
280
|
_classPrivateFieldGet(_state, this).value[name] = value;
|
|
288
281
|
_classPrivateFieldGet(_state, this).error[name] = null;
|
|
289
282
|
_classPrivateFieldGet(_state$, this).next({
|
|
290
|
-
|
|
291
|
-
names: [name],
|
|
283
|
+
event: FORM_EVENT.set_field_value,
|
|
284
|
+
names: new Set([name]),
|
|
292
285
|
value: _classPrivateFieldGet(_state, this).value,
|
|
293
286
|
error: _classPrivateFieldGet(_state, this).error
|
|
294
287
|
});
|
|
@@ -305,8 +298,8 @@ var Form = /*#__PURE__*/function () {
|
|
|
305
298
|
return [name, null];
|
|
306
299
|
})));
|
|
307
300
|
_classPrivateFieldGet(_state$, this).next({
|
|
308
|
-
|
|
309
|
-
names: names,
|
|
301
|
+
event: FORM_EVENT.set_fields_value,
|
|
302
|
+
names: new Set(names),
|
|
310
303
|
value: _classPrivateFieldGet(_state, this).value,
|
|
311
304
|
error: _classPrivateFieldGet(_state, this).error
|
|
312
305
|
});
|
|
@@ -317,12 +310,12 @@ var Form = /*#__PURE__*/function () {
|
|
|
317
310
|
}, {
|
|
318
311
|
key: "reset",
|
|
319
312
|
value: function reset() {
|
|
313
|
+
var _classPrivateFieldGet12;
|
|
320
314
|
_classPrivateFieldGet(_state, this).value = _classPrivateFieldGet(_defaultValue, this);
|
|
321
315
|
_classPrivateFieldGet(_state, this).error = {};
|
|
322
316
|
_classPrivateFieldGet(_state$, this).next({
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
value: _classPrivateFieldGet(_state, this).value,
|
|
317
|
+
event: FORM_EVENT.reset,
|
|
318
|
+
value: (_classPrivateFieldGet12 = _classPrivateFieldGet(_state, this).value) !== null && _classPrivateFieldGet12 !== void 0 ? _classPrivateFieldGet12 : {},
|
|
326
319
|
error: _classPrivateFieldGet(_state, this).error
|
|
327
320
|
});
|
|
328
321
|
}
|
|
@@ -335,8 +328,7 @@ var Form = /*#__PURE__*/function () {
|
|
|
335
328
|
_classPrivateFieldGet(_state, this).value = {};
|
|
336
329
|
_classPrivateFieldGet(_state, this).error = {};
|
|
337
330
|
_classPrivateFieldGet(_state$, this).next({
|
|
338
|
-
|
|
339
|
-
names: [],
|
|
331
|
+
event: FORM_EVENT.clear,
|
|
340
332
|
value: _classPrivateFieldGet(_state, this).value,
|
|
341
333
|
error: _classPrivateFieldGet(_state, this).error
|
|
342
334
|
});
|
|
@@ -350,16 +342,16 @@ var Form = /*#__PURE__*/function () {
|
|
|
350
342
|
key: "change",
|
|
351
343
|
value: (function () {
|
|
352
344
|
var _change = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee4(name, value) {
|
|
353
|
-
var
|
|
345
|
+
var _classPrivateFieldGet13, _classPrivateFieldGet14;
|
|
354
346
|
return _regenerator().w(function (_context4) {
|
|
355
347
|
while (1) switch (_context4.n) {
|
|
356
348
|
case 0:
|
|
357
|
-
(
|
|
349
|
+
(_classPrivateFieldGet14 = (_classPrivateFieldGet13 = _classPrivateFieldGet(_state, this)).value) !== null && _classPrivateFieldGet14 !== void 0 ? _classPrivateFieldGet14 : _classPrivateFieldGet13.value = {};
|
|
358
350
|
_classPrivateFieldGet(_state, this).value[name] = value;
|
|
359
351
|
_classPrivateFieldGet(_state, this).error[name] = null;
|
|
360
352
|
_classPrivateFieldGet(_state$, this).next({
|
|
361
|
-
|
|
362
|
-
names: [name],
|
|
353
|
+
event: FORM_EVENT.change,
|
|
354
|
+
names: new Set([name]),
|
|
363
355
|
value: _classPrivateFieldGet(_state, this).value,
|
|
364
356
|
error: _classPrivateFieldGet(_state, this).error
|
|
365
357
|
});
|