@astroapps/forms-core 1.0.1 → 1.1.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/lib/evalExpression.d.ts +1 -0
- package/lib/formNode.d.ts +7 -0
- package/lib/formState.d.ts +4 -1
- package/lib/index.cjs +62 -12
- package/lib/index.cjs.map +1 -1
- package/lib/index.js +60 -14
- package/lib/index.js.map +1 -1
- package/lib/validators.d.ts +2 -1
- package/package.json +1 -1
- package/src/evalExpression.ts +4 -2
- package/src/formNode.ts +45 -0
- package/src/formState.ts +34 -3
- package/src/validators.ts +5 -1
package/lib/evalExpression.d.ts
CHANGED
|
@@ -8,6 +8,7 @@ export interface ExpressionEvalContext {
|
|
|
8
8
|
dataNode: SchemaDataNode;
|
|
9
9
|
schemaInterface: SchemaInterface;
|
|
10
10
|
variables?: Value<Record<string, any> | undefined>;
|
|
11
|
+
runAsync(effect: () => void): void;
|
|
11
12
|
}
|
|
12
13
|
export type ExpressionEval<T extends EntityExpression> = (expr: T, context: ExpressionEvalContext) => void;
|
|
13
14
|
export declare const jsonataEval: ExpressionEval<JsonataExpression>;
|
package/lib/formNode.d.ts
CHANGED
|
@@ -43,3 +43,10 @@ export declare function visitControlData<A>(definition: ControlDefinition, ctx:
|
|
|
43
43
|
export type ControlDataVisitor<A> = (dataNode: SchemaDataNode, definition: DataControlDefinition) => A | undefined;
|
|
44
44
|
export declare function visitFormData<A>(node: FormNode, dataNode: SchemaDataNode, cb: ControlDataVisitor<A>, notSelf?: boolean): A | undefined;
|
|
45
45
|
export declare function visitFormDataInContext<A>(parentContext: SchemaDataNode, node: FormNode, cb: ControlDataVisitor<A>): A | undefined;
|
|
46
|
+
export interface FormDataNode {
|
|
47
|
+
parent?: FormDataNode;
|
|
48
|
+
formNode: FormNode;
|
|
49
|
+
parentData: SchemaDataNode;
|
|
50
|
+
childIndex?: number;
|
|
51
|
+
}
|
|
52
|
+
export declare function visitFormDataNode<A>(node: FormDataNode, visitFn: (node: FormDataNode, data?: SchemaDataNode) => A | undefined): A | undefined;
|
package/lib/formState.d.ts
CHANGED
|
@@ -19,6 +19,7 @@ export interface ControlState {
|
|
|
19
19
|
disabled: boolean;
|
|
20
20
|
clearHidden: boolean;
|
|
21
21
|
variables: Record<string, any>;
|
|
22
|
+
meta: Control<Record<string, any>>;
|
|
22
23
|
}
|
|
23
24
|
export interface FormContextOptions {
|
|
24
25
|
readonly?: boolean | null;
|
|
@@ -36,9 +37,11 @@ export interface FormContextData {
|
|
|
36
37
|
optionSelected?: boolean;
|
|
37
38
|
}
|
|
38
39
|
export interface FormState {
|
|
39
|
-
getControlState(parent: SchemaDataNode, formNode: FormNode, context: FormContextOptions): ControlState;
|
|
40
|
+
getControlState(parent: SchemaDataNode, formNode: FormNode, context: FormContextOptions, runAsync: (af: () => void) => void): ControlState;
|
|
40
41
|
cleanup(): void;
|
|
41
42
|
evalExpression(expr: EntityExpression, context: ExpressionEvalContext): void;
|
|
43
|
+
getExistingControlState(parent: SchemaDataNode, formNode: FormNode, stateKey?: string): ControlState | undefined;
|
|
42
44
|
}
|
|
45
|
+
export declare function getControlStateId(parent: SchemaDataNode, formNode: FormNode, stateKey?: string): string;
|
|
43
46
|
export declare function createFormState(schemaInterface: SchemaInterface, evaluators?: Record<string, ExpressionEval<any>>): FormState;
|
|
44
47
|
export declare function createOverrideProxy<A extends object, B extends Record<string, any>>(proxyFor: A, handlers: Control<B>): A;
|
package/lib/index.cjs
CHANGED
|
@@ -1227,6 +1227,35 @@ function visitFormDataInContext(parentContext, node, cb) {
|
|
|
1227
1227
|
var dataNode = lookupDataNode(node.definition, parentContext);
|
|
1228
1228
|
return visitFormData(node, dataNode != null ? dataNode : parentContext, cb, !dataNode);
|
|
1229
1229
|
}
|
|
1230
|
+
function visitFormDataNode(node, visitFn) {
|
|
1231
|
+
var dataNode = lookupDataNode(node.formNode.definition, node.parentData);
|
|
1232
|
+
var v = visitFn(node, dataNode);
|
|
1233
|
+
if (v !== undefined) return v;
|
|
1234
|
+
var parentData = dataNode != null ? dataNode : node.parentData;
|
|
1235
|
+
if (parentData.schema.field.collection && parentData.elementIndex == null) {
|
|
1236
|
+
var elemCount = parentData.control.elements.length;
|
|
1237
|
+
for (var i = 0; i < elemCount; i++) {
|
|
1238
|
+
var _v = visitChildren(parentData.getChildElement(i));
|
|
1239
|
+
if (_v !== undefined) return _v;
|
|
1240
|
+
}
|
|
1241
|
+
return undefined;
|
|
1242
|
+
} else {
|
|
1243
|
+
return visitChildren(parentData);
|
|
1244
|
+
}
|
|
1245
|
+
function visitChildren(parentData) {
|
|
1246
|
+
var children = node.formNode.getChildNodes();
|
|
1247
|
+
for (var _i2 = 0; _i2 < children.length; _i2++) {
|
|
1248
|
+
var child = children[_i2];
|
|
1249
|
+
var res = visitFormDataNode({
|
|
1250
|
+
formNode: child,
|
|
1251
|
+
parent: node,
|
|
1252
|
+
parentData: parentData,
|
|
1253
|
+
childIndex: _i2
|
|
1254
|
+
}, visitFn);
|
|
1255
|
+
if (res !== undefined) return res;
|
|
1256
|
+
}
|
|
1257
|
+
}
|
|
1258
|
+
}
|
|
1230
1259
|
|
|
1231
1260
|
/**
|
|
1232
1261
|
* Converts a JSON path array to a string.
|
|
@@ -1313,7 +1342,8 @@ var jsonataEval = function jsonataEval(expr, _ref4) {
|
|
|
1313
1342
|
var scope = _ref4.scope,
|
|
1314
1343
|
returnResult = _ref4.returnResult,
|
|
1315
1344
|
dataNode = _ref4.dataNode,
|
|
1316
|
-
variables = _ref4.variables
|
|
1345
|
+
variables = _ref4.variables,
|
|
1346
|
+
runAsync = _ref4.runAsync;
|
|
1317
1347
|
var path = getJsonPath(dataNode);
|
|
1318
1348
|
var pathString = jsonPathString(path, function (x) {
|
|
1319
1349
|
return "#$i[" + x + "]";
|
|
@@ -1335,7 +1365,10 @@ var jsonataEval = function jsonataEval(expr, _ref4) {
|
|
|
1335
1365
|
};
|
|
1336
1366
|
}
|
|
1337
1367
|
});
|
|
1338
|
-
controls.createAsyncEffect(runJsonata, scope);
|
|
1368
|
+
var asyncEffect = controls.createAsyncEffect(runJsonata, scope);
|
|
1369
|
+
runAsync(function () {
|
|
1370
|
+
return asyncEffect.start();
|
|
1371
|
+
});
|
|
1339
1372
|
};
|
|
1340
1373
|
var uuidEval = function uuidEval(_, ctx) {
|
|
1341
1374
|
ctx.returnResult(uuid.v4());
|
|
@@ -1355,11 +1388,12 @@ var jsonataValidator = function jsonataValidator(validation, context) {
|
|
|
1355
1388
|
dataNode: context.parentData,
|
|
1356
1389
|
returnResult: function returnResult(v) {
|
|
1357
1390
|
controls.trackControlChange(context.data.control, controls.ControlChange.Validate);
|
|
1358
|
-
console.log("Setting jsonata error", v);
|
|
1391
|
+
// console.log("Setting jsonata error", v);
|
|
1359
1392
|
context.data.control.setError("jsonata", v == null ? void 0 : v.toString());
|
|
1360
1393
|
},
|
|
1361
1394
|
schemaInterface: context.schemaInterface,
|
|
1362
|
-
variables: context.formContext.fields.variables
|
|
1395
|
+
variables: context.formContext.fields.variables,
|
|
1396
|
+
runAsync: context.runAsync
|
|
1363
1397
|
});
|
|
1364
1398
|
};
|
|
1365
1399
|
var lengthValidator = function lengthValidator(lv, context) {
|
|
@@ -1427,7 +1461,7 @@ function createValidators(def, context) {
|
|
|
1427
1461
|
});
|
|
1428
1462
|
}
|
|
1429
1463
|
}
|
|
1430
|
-
function setupValidation(controlImpl, definition, dataNode, schemaInterface, parent, formNode) {
|
|
1464
|
+
function setupValidation(controlImpl, definition, dataNode, schemaInterface, parent, formNode, runAsync) {
|
|
1431
1465
|
var validationEnabled = createScopedComputed(controlImpl, function () {
|
|
1432
1466
|
return !definition.hidden;
|
|
1433
1467
|
});
|
|
@@ -1448,7 +1482,8 @@ function setupValidation(controlImpl, definition, dataNode, schemaInterface, par
|
|
|
1448
1482
|
addCleanup: function addCleanup(cleanup) {
|
|
1449
1483
|
validatorsScope.addCleanup(cleanup);
|
|
1450
1484
|
},
|
|
1451
|
-
formContext: controlImpl
|
|
1485
|
+
formContext: controlImpl,
|
|
1486
|
+
runAsync: runAsync
|
|
1452
1487
|
});
|
|
1453
1488
|
controls.createEffect(function () {
|
|
1454
1489
|
if (!validationEnabled.value) return undefined;
|
|
@@ -1468,6 +1503,9 @@ function setupValidation(controlImpl, definition, dataNode, schemaInterface, par
|
|
|
1468
1503
|
}, function (c) {}, controlImpl);
|
|
1469
1504
|
}
|
|
1470
1505
|
|
|
1506
|
+
function getControlStateId(parent, formNode, stateKey) {
|
|
1507
|
+
return parent.id + "$" + formNode.id + (stateKey != null ? stateKey : "");
|
|
1508
|
+
}
|
|
1471
1509
|
function createFormState(schemaInterface, evaluators) {
|
|
1472
1510
|
if (evaluators === void 0) {
|
|
1473
1511
|
evaluators = defaultEvaluators;
|
|
@@ -1484,9 +1522,17 @@ function createFormState(schemaInterface, evaluators) {
|
|
|
1484
1522
|
// console.log("Cleanup form state");
|
|
1485
1523
|
controlStates.cleanup();
|
|
1486
1524
|
},
|
|
1487
|
-
|
|
1488
|
-
var
|
|
1489
|
-
var
|
|
1525
|
+
getExistingControlState: function getExistingControlState(parent, formNode, stateKey) {
|
|
1526
|
+
var stateId = getControlStateId(parent, formNode, stateKey);
|
|
1527
|
+
var control = controls.getCurrentFields(controlStates)[stateId];
|
|
1528
|
+
if (control) {
|
|
1529
|
+
var _getMetaValue;
|
|
1530
|
+
return (_getMetaValue = controls.getMetaValue(control, "impl")) == null ? void 0 : _getMetaValue.value;
|
|
1531
|
+
}
|
|
1532
|
+
return undefined;
|
|
1533
|
+
},
|
|
1534
|
+
getControlState: function getControlState(parent, formNode, context, runAsync) {
|
|
1535
|
+
var stateId = getControlStateId(parent, formNode, context.stateKey);
|
|
1490
1536
|
var controlImpl = controlStates.fields[stateId];
|
|
1491
1537
|
controlImpl.value = context;
|
|
1492
1538
|
function evalExpr(scope, init, nk, e, coerce) {
|
|
@@ -1499,7 +1545,8 @@ function createFormState(schemaInterface, evaluators) {
|
|
|
1499
1545
|
scope: scope,
|
|
1500
1546
|
dataNode: parent,
|
|
1501
1547
|
variables: controlImpl.fields.variables,
|
|
1502
|
-
schemaInterface: schemaInterface
|
|
1548
|
+
schemaInterface: schemaInterface,
|
|
1549
|
+
runAsync: runAsync
|
|
1503
1550
|
});
|
|
1504
1551
|
return true;
|
|
1505
1552
|
}
|
|
@@ -1563,7 +1610,8 @@ function createFormState(schemaInterface, evaluators) {
|
|
|
1563
1610
|
clearHidden: false,
|
|
1564
1611
|
hidden: false,
|
|
1565
1612
|
variables: (_controlImpl$fields$v = controlImpl.fields.variables.current.value) != null ? _controlImpl$fields$v : {},
|
|
1566
|
-
stateId: stateId
|
|
1613
|
+
stateId: stateId,
|
|
1614
|
+
meta: controls.newControl({})
|
|
1567
1615
|
});
|
|
1568
1616
|
var _control$fields = control.fields,
|
|
1569
1617
|
dataNode = _control$fields.dataNode,
|
|
@@ -1607,7 +1655,7 @@ function createFormState(schemaInterface, evaluators) {
|
|
|
1607
1655
|
dn.control.disabled = disabled.value;
|
|
1608
1656
|
}
|
|
1609
1657
|
}, scope);
|
|
1610
|
-
setupValidation(controlImpl, definition, dataNode, schemaInterface, parent, formNode);
|
|
1658
|
+
setupValidation(controlImpl, definition, dataNode, schemaInterface, parent, formNode, runAsync);
|
|
1611
1659
|
controls.createSyncEffect(function () {
|
|
1612
1660
|
var _dataNode$value;
|
|
1613
1661
|
var dn = (_dataNode$value = dataNode.value) == null ? void 0 : _dataNode$value.control;
|
|
@@ -1740,6 +1788,7 @@ exports.doubleField = doubleField;
|
|
|
1740
1788
|
exports.fieldPathForDefinition = fieldPathForDefinition;
|
|
1741
1789
|
exports.findField = findField;
|
|
1742
1790
|
exports.fontAwesomeIcon = fontAwesomeIcon;
|
|
1791
|
+
exports.getControlStateId = getControlStateId;
|
|
1743
1792
|
exports.getDisplayOnlyOptions = getDisplayOnlyOptions;
|
|
1744
1793
|
exports.getGroupRendererOptions = getGroupRendererOptions;
|
|
1745
1794
|
exports.getJsonPath = getJsonPath;
|
|
@@ -1809,5 +1858,6 @@ exports.visitControlDataArray = visitControlDataArray;
|
|
|
1809
1858
|
exports.visitControlDefinition = visitControlDefinition;
|
|
1810
1859
|
exports.visitFormData = visitFormData;
|
|
1811
1860
|
exports.visitFormDataInContext = visitFormDataInContext;
|
|
1861
|
+
exports.visitFormDataNode = visitFormDataNode;
|
|
1812
1862
|
exports.withScalarOptions = withScalarOptions;
|
|
1813
1863
|
//# sourceMappingURL=index.cjs.map
|