@flowgram.ai/form 0.1.6 → 0.1.7
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/esm/index.js +47 -2
- package/dist/esm/index.js.map +1 -1
- package/dist/index.d.mts +3 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +54 -9
- package/dist/index.js.map +1 -1
- package/package.json +5 -5
package/dist/index.js
CHANGED
|
@@ -105,6 +105,16 @@ function shallowSetIn(obj, path, value) {
|
|
|
105
105
|
}
|
|
106
106
|
return res;
|
|
107
107
|
}
|
|
108
|
+
function keepValidKeys(obj, validKeys) {
|
|
109
|
+
const validKeysSet = new Set(validKeys);
|
|
110
|
+
const newObj = {};
|
|
111
|
+
Object.keys(obj).forEach((key) => {
|
|
112
|
+
if (validKeysSet.has(key)) {
|
|
113
|
+
newObj[key] = obj[key];
|
|
114
|
+
}
|
|
115
|
+
});
|
|
116
|
+
return newObj;
|
|
117
|
+
}
|
|
108
118
|
|
|
109
119
|
// src/utils/dom.ts
|
|
110
120
|
function isReactChangeEvent(e) {
|
|
@@ -522,6 +532,9 @@ function toForm(model) {
|
|
|
522
532
|
get values() {
|
|
523
533
|
return model.values;
|
|
524
534
|
},
|
|
535
|
+
set values(v) {
|
|
536
|
+
model.values = v;
|
|
537
|
+
},
|
|
525
538
|
state: toFormState(model.state),
|
|
526
539
|
getValueIn: (name) => model.getValueIn(name),
|
|
527
540
|
setValueIn: (name, value) => model.setValueIn(name, value)
|
|
@@ -622,7 +635,8 @@ function Field({
|
|
|
622
635
|
if (fieldModel.renderCount > 1) {
|
|
623
636
|
fieldModel.renderCount = fieldModel.renderCount - 1;
|
|
624
637
|
} else {
|
|
625
|
-
fieldModel.
|
|
638
|
+
const newFieldModel = formModel.getField(fieldModel.name);
|
|
639
|
+
if (newFieldModel === fieldModel) fieldModel.dispose();
|
|
626
640
|
}
|
|
627
641
|
};
|
|
628
642
|
}, [fieldModel]);
|
|
@@ -738,6 +752,9 @@ var Store = class {
|
|
|
738
752
|
get values() {
|
|
739
753
|
return this._values;
|
|
740
754
|
}
|
|
755
|
+
set values(v) {
|
|
756
|
+
this._values = v;
|
|
757
|
+
}
|
|
741
758
|
setInitialValues(values) {
|
|
742
759
|
this._values = values;
|
|
743
760
|
}
|
|
@@ -1139,6 +1156,7 @@ var FieldArrayModel = class extends FieldModel {
|
|
|
1139
1156
|
f.dispose();
|
|
1140
1157
|
});
|
|
1141
1158
|
this.form.fieldMap = newFieldMap;
|
|
1159
|
+
this.form.alignStateWithFieldMap();
|
|
1142
1160
|
}
|
|
1143
1161
|
swap(from, to) {
|
|
1144
1162
|
if (!this.value) {
|
|
@@ -1214,6 +1232,15 @@ var FormModel = class {
|
|
|
1214
1232
|
get values() {
|
|
1215
1233
|
return (0, import_lodash8.cloneDeep)(this.store.values) || (0, import_lodash8.cloneDeep)(this.initialValues);
|
|
1216
1234
|
}
|
|
1235
|
+
set values(v) {
|
|
1236
|
+
const prevValues = this.values;
|
|
1237
|
+
this.store.values = v;
|
|
1238
|
+
this.fireOnFormValuesChange({
|
|
1239
|
+
values: this.values,
|
|
1240
|
+
prevValues,
|
|
1241
|
+
name: ""
|
|
1242
|
+
});
|
|
1243
|
+
}
|
|
1217
1244
|
get validationTrigger() {
|
|
1218
1245
|
return this._options.validateTrigger;
|
|
1219
1246
|
}
|
|
@@ -1376,6 +1403,23 @@ var FormModel = class {
|
|
|
1376
1403
|
this.onValidateEmitter.fire(this.state);
|
|
1377
1404
|
return (0, import_lodash8.flatten)(feedbacksArr).filter(Boolean);
|
|
1378
1405
|
}
|
|
1406
|
+
alignStateWithFieldMap() {
|
|
1407
|
+
const keys = Array.from(this.fieldMap.keys());
|
|
1408
|
+
if (this.state.errors) {
|
|
1409
|
+
this.state.errors = keepValidKeys(this.state.errors, keys);
|
|
1410
|
+
}
|
|
1411
|
+
if (this.state.warnings) {
|
|
1412
|
+
this.state.warnings = keepValidKeys(this.state.warnings, keys);
|
|
1413
|
+
}
|
|
1414
|
+
this.fieldMap.forEach((f) => {
|
|
1415
|
+
if (f.state.errors) {
|
|
1416
|
+
f.state.errors = keepValidKeys(f.state.errors, keys);
|
|
1417
|
+
}
|
|
1418
|
+
if (f.state.warnings) {
|
|
1419
|
+
f.state.warnings = keepValidKeys(f.state.warnings, keys);
|
|
1420
|
+
}
|
|
1421
|
+
});
|
|
1422
|
+
}
|
|
1379
1423
|
dispose() {
|
|
1380
1424
|
this.fieldMap.forEach((f) => f.dispose());
|
|
1381
1425
|
this.store.dispose();
|
|
@@ -1432,9 +1476,9 @@ function useForm() {
|
|
|
1432
1476
|
|
|
1433
1477
|
// src/react/use-watch.ts
|
|
1434
1478
|
var import_react4 = require("react");
|
|
1435
|
-
var
|
|
1479
|
+
var import_utils14 = require("@flowgram.ai/utils");
|
|
1436
1480
|
function useWatch(name) {
|
|
1437
|
-
const refresh = (0,
|
|
1481
|
+
const refresh = (0, import_utils14.useRefresh)();
|
|
1438
1482
|
const formModel = useFormModel();
|
|
1439
1483
|
if (!formModel) {
|
|
1440
1484
|
throw new Error("[Form] error in useWatch, formModel not found");
|
|
@@ -1454,7 +1498,7 @@ function useWatch(name) {
|
|
|
1454
1498
|
// src/react/field-array.tsx
|
|
1455
1499
|
var React4 = __toESM(require("react"));
|
|
1456
1500
|
var import_lodash10 = require("lodash");
|
|
1457
|
-
var
|
|
1501
|
+
var import_utils16 = require("@flowgram.ai/utils");
|
|
1458
1502
|
var import_reactive4 = require("@flowgram.ai/reactive");
|
|
1459
1503
|
function FieldArray({
|
|
1460
1504
|
name,
|
|
@@ -1469,7 +1513,7 @@ function FieldArray({
|
|
|
1469
1513
|
[]
|
|
1470
1514
|
);
|
|
1471
1515
|
const field = React4.useMemo(() => toFieldArray(fieldModel), [fieldModel]);
|
|
1472
|
-
const refresh = (0,
|
|
1516
|
+
const refresh = (0, import_utils16.useRefresh)();
|
|
1473
1517
|
const fieldModelState = (0, import_reactive4.useReadonlyReactiveState)(fieldModel.reactiveState);
|
|
1474
1518
|
const formModelState = (0, import_reactive4.useReadonlyReactiveState)(formModel.reactiveState);
|
|
1475
1519
|
const fieldState = toFieldState(fieldModelState);
|
|
@@ -1480,7 +1524,7 @@ function FieldArray({
|
|
|
1480
1524
|
formModel.setInitValueIn(name, defaultValue);
|
|
1481
1525
|
refresh();
|
|
1482
1526
|
}
|
|
1483
|
-
const disposableCollection = new
|
|
1527
|
+
const disposableCollection = new import_utils16.DisposableCollection();
|
|
1484
1528
|
disposableCollection.push(
|
|
1485
1529
|
fieldModel.onValueChange(() => {
|
|
1486
1530
|
refresh();
|
|
@@ -1501,7 +1545,8 @@ function FieldArray({
|
|
|
1501
1545
|
if (fieldModel.renderCount > 1) {
|
|
1502
1546
|
fieldModel.renderCount = fieldModel.renderCount - 1;
|
|
1503
1547
|
} else {
|
|
1504
|
-
fieldModel.
|
|
1548
|
+
const newFieldModel = formModel.getField(fieldModel.name);
|
|
1549
|
+
if (newFieldModel === fieldModel) fieldModel.dispose();
|
|
1505
1550
|
}
|
|
1506
1551
|
};
|
|
1507
1552
|
}, [fieldModel]);
|
|
@@ -1519,11 +1564,11 @@ function FieldArray({
|
|
|
1519
1564
|
|
|
1520
1565
|
// src/react/use-field.ts
|
|
1521
1566
|
var import_react5 = require("react");
|
|
1522
|
-
var
|
|
1567
|
+
var import_utils18 = require("@flowgram.ai/utils");
|
|
1523
1568
|
function useField(name) {
|
|
1524
1569
|
const currentFieldModel = (0, import_react5.useContext)(FieldModelContext);
|
|
1525
1570
|
const formModel = useFormModel();
|
|
1526
|
-
const refresh = (0,
|
|
1571
|
+
const refresh = (0, import_utils18.useRefresh)();
|
|
1527
1572
|
const fieldModel = name ? formModel.getField(name) : currentFieldModel;
|
|
1528
1573
|
(0, import_react5.useEffect)(() => {
|
|
1529
1574
|
let disposable;
|