@astroapps/forms-core 1.0.2 → 1.1.1
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/formNode.d.ts +7 -0
- package/lib/formState.d.ts +4 -0
- package/lib/index.cjs +56 -12
- package/lib/index.cjs.map +1 -1
- package/lib/index.js +54 -14
- package/lib/index.js.map +1 -1
- package/package.json +1 -1
- package/src/formNode.ts +45 -0
- package/src/formState.ts +52 -31
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
|
@@ -10,6 +10,7 @@ export interface ControlState {
|
|
|
10
10
|
definition: ControlDefinition;
|
|
11
11
|
schemaInterface: SchemaInterface;
|
|
12
12
|
dataNode?: SchemaDataNode | undefined;
|
|
13
|
+
display?: string;
|
|
13
14
|
stateId?: string;
|
|
14
15
|
style?: object;
|
|
15
16
|
layoutStyle?: object;
|
|
@@ -19,6 +20,7 @@ export interface ControlState {
|
|
|
19
20
|
disabled: boolean;
|
|
20
21
|
clearHidden: boolean;
|
|
21
22
|
variables: Record<string, any>;
|
|
23
|
+
meta: Control<Record<string, any>>;
|
|
22
24
|
}
|
|
23
25
|
export interface FormContextOptions {
|
|
24
26
|
readonly?: boolean | null;
|
|
@@ -39,6 +41,8 @@ export interface FormState {
|
|
|
39
41
|
getControlState(parent: SchemaDataNode, formNode: FormNode, context: FormContextOptions, runAsync: (af: () => void) => void): ControlState;
|
|
40
42
|
cleanup(): void;
|
|
41
43
|
evalExpression(expr: EntityExpression, context: ExpressionEvalContext): void;
|
|
44
|
+
getExistingControlState(parent: SchemaDataNode, formNode: FormNode, stateKey?: string): ControlState | undefined;
|
|
42
45
|
}
|
|
46
|
+
export declare function getControlStateId(parent: SchemaDataNode, formNode: FormNode, stateKey?: string): string;
|
|
43
47
|
export declare function createFormState(schemaInterface: SchemaInterface, evaluators?: Record<string, ExpressionEval<any>>): FormState;
|
|
44
48
|
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.
|
|
@@ -1474,6 +1503,9 @@ function setupValidation(controlImpl, definition, dataNode, schemaInterface, par
|
|
|
1474
1503
|
}, function (c) {}, controlImpl);
|
|
1475
1504
|
}
|
|
1476
1505
|
|
|
1506
|
+
function getControlStateId(parent, formNode, stateKey) {
|
|
1507
|
+
return parent.id + "$" + formNode.id + (stateKey != null ? stateKey : "");
|
|
1508
|
+
}
|
|
1477
1509
|
function createFormState(schemaInterface, evaluators) {
|
|
1478
1510
|
if (evaluators === void 0) {
|
|
1479
1511
|
evaluators = defaultEvaluators;
|
|
@@ -1490,9 +1522,17 @@ function createFormState(schemaInterface, evaluators) {
|
|
|
1490
1522
|
// console.log("Cleanup form state");
|
|
1491
1523
|
controlStates.cleanup();
|
|
1492
1524
|
},
|
|
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
|
+
},
|
|
1493
1534
|
getControlState: function getControlState(parent, formNode, context, runAsync) {
|
|
1494
|
-
var
|
|
1495
|
-
var stateId = parent.id + "$" + formNode.id + ((_context$stateKey = context.stateKey) != null ? _context$stateKey : "");
|
|
1535
|
+
var stateId = getControlStateId(parent, formNode, context.stateKey);
|
|
1496
1536
|
var controlImpl = controlStates.fields[stateId];
|
|
1497
1537
|
controlImpl.value = context;
|
|
1498
1538
|
function evalExpr(scope, init, nk, e, coerce) {
|
|
@@ -1522,14 +1562,6 @@ function createFormState(schemaInterface, evaluators) {
|
|
|
1522
1562
|
var definition = createOverrideProxy(def, definitionOverrides);
|
|
1523
1563
|
var of = definitionOverrides.fields;
|
|
1524
1564
|
var df = displayOverrides.fields;
|
|
1525
|
-
controls.createScopedEffect(function (c) {
|
|
1526
|
-
var textDisplay = isDisplayControl(def) && isTextDisplay(def.displayData) ? def.displayData : undefined;
|
|
1527
|
-
evalExpr(c, textDisplay == null ? void 0 : textDisplay.text, df.text, textDisplay && firstExpr(formNode, exports.DynamicPropertyType.Display), coerceString);
|
|
1528
|
-
}, displayOverrides);
|
|
1529
|
-
controls.createScopedEffect(function (c) {
|
|
1530
|
-
var htmlDisplay = isDisplayControl(def) && isHtmlDisplay(def.displayData) ? def.displayData : undefined;
|
|
1531
|
-
evalExpr(c, htmlDisplay == null ? void 0 : htmlDisplay.html, df.html, htmlDisplay && firstExpr(formNode, exports.DynamicPropertyType.Display), coerceString);
|
|
1532
|
-
}, displayOverrides);
|
|
1533
1565
|
controls.updateComputedValue(of.displayData, function () {
|
|
1534
1566
|
return isDisplayControl(def) ? createOverrideProxy(def.displayData, displayOverrides) : undefined;
|
|
1535
1567
|
});
|
|
@@ -1570,7 +1602,8 @@ function createFormState(schemaInterface, evaluators) {
|
|
|
1570
1602
|
clearHidden: false,
|
|
1571
1603
|
hidden: false,
|
|
1572
1604
|
variables: (_controlImpl$fields$v = controlImpl.fields.variables.current.value) != null ? _controlImpl$fields$v : {},
|
|
1573
|
-
stateId: stateId
|
|
1605
|
+
stateId: stateId,
|
|
1606
|
+
meta: controls.newControl({})
|
|
1574
1607
|
});
|
|
1575
1608
|
var _control$fields = control.fields,
|
|
1576
1609
|
dataNode = _control$fields.dataNode,
|
|
@@ -1580,7 +1613,8 @@ function createFormState(schemaInterface, evaluators) {
|
|
|
1580
1613
|
layoutStyle = _control$fields.layoutStyle,
|
|
1581
1614
|
allowedOptions = _control$fields.allowedOptions,
|
|
1582
1615
|
disabled = _control$fields.disabled,
|
|
1583
|
-
variables = _control$fields.variables
|
|
1616
|
+
variables = _control$fields.variables,
|
|
1617
|
+
display = _control$fields.display;
|
|
1584
1618
|
controls.createScopedEffect(function (c) {
|
|
1585
1619
|
return evalExpr(c, undefined, style, firstExpr(formNode, exports.DynamicPropertyType.Style), coerceStyle);
|
|
1586
1620
|
}, scope);
|
|
@@ -1592,6 +1626,9 @@ function createFormState(schemaInterface, evaluators) {
|
|
|
1592
1626
|
return x;
|
|
1593
1627
|
});
|
|
1594
1628
|
}, scope);
|
|
1629
|
+
controls.createScopedEffect(function (c) {
|
|
1630
|
+
return evalExpr(c, undefined, display, firstExpr(formNode, exports.DynamicPropertyType.Display), coerceString);
|
|
1631
|
+
}, scope);
|
|
1595
1632
|
controls.updateComputedValue(dataNode, function () {
|
|
1596
1633
|
return lookupDataNode(definition, parent);
|
|
1597
1634
|
});
|
|
@@ -1614,6 +1651,11 @@ function createFormState(schemaInterface, evaluators) {
|
|
|
1614
1651
|
dn.control.disabled = disabled.value;
|
|
1615
1652
|
}
|
|
1616
1653
|
}, scope);
|
|
1654
|
+
controls.createSyncEffect(function () {
|
|
1655
|
+
if (isDisplayControl(def)) {
|
|
1656
|
+
if (isTextDisplay(def.displayData)) df.text.value = display.value;else if (isHtmlDisplay(def.displayData)) df.html.value = display.value;
|
|
1657
|
+
}
|
|
1658
|
+
}, displayOverrides);
|
|
1617
1659
|
setupValidation(controlImpl, definition, dataNode, schemaInterface, parent, formNode, runAsync);
|
|
1618
1660
|
controls.createSyncEffect(function () {
|
|
1619
1661
|
var _dataNode$value;
|
|
@@ -1747,6 +1789,7 @@ exports.doubleField = doubleField;
|
|
|
1747
1789
|
exports.fieldPathForDefinition = fieldPathForDefinition;
|
|
1748
1790
|
exports.findField = findField;
|
|
1749
1791
|
exports.fontAwesomeIcon = fontAwesomeIcon;
|
|
1792
|
+
exports.getControlStateId = getControlStateId;
|
|
1750
1793
|
exports.getDisplayOnlyOptions = getDisplayOnlyOptions;
|
|
1751
1794
|
exports.getGroupRendererOptions = getGroupRendererOptions;
|
|
1752
1795
|
exports.getJsonPath = getJsonPath;
|
|
@@ -1816,5 +1859,6 @@ exports.visitControlDataArray = visitControlDataArray;
|
|
|
1816
1859
|
exports.visitControlDefinition = visitControlDefinition;
|
|
1817
1860
|
exports.visitFormData = visitFormData;
|
|
1818
1861
|
exports.visitFormDataInContext = visitFormDataInContext;
|
|
1862
|
+
exports.visitFormDataNode = visitFormDataNode;
|
|
1819
1863
|
exports.withScalarOptions = withScalarOptions;
|
|
1820
1864
|
//# sourceMappingURL=index.cjs.map
|