@astroapps/forms-core 1.2.3 → 2.0.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/src/formNode.ts CHANGED
@@ -115,21 +115,26 @@ class FormTreeImpl extends FormTree {
115
115
  }
116
116
 
117
117
  export function legacyFormNode(definition: ControlDefinition) {
118
- return createFormTree([definition]).rootNode.getChildNodes()[0];
118
+ return createFormTree([definition]).rootNode;
119
119
  }
120
120
 
121
121
  export function createFormTree(
122
122
  controls: ControlDefinition[],
123
123
  getForm: FormTreeLookup = { getForm: () => undefined },
124
124
  ): FormTree {
125
- return new FormTreeImpl(getForm, {
126
- type: ControlDefinitionType.Group,
127
- children: controls,
128
- groupOptions: {
129
- type: GroupRenderType.Standard,
130
- hideTitle: true,
131
- },
132
- } as GroupedControlsDefinition);
125
+ return new FormTreeImpl(
126
+ getForm,
127
+ controls.length === 1
128
+ ? controls[0]
129
+ : ({
130
+ type: ControlDefinitionType.Group,
131
+ children: controls,
132
+ groupOptions: {
133
+ type: GroupRenderType.Standard,
134
+ hideTitle: true,
135
+ },
136
+ } as GroupedControlsDefinition),
137
+ );
133
138
  }
134
139
 
135
140
  export function createFormLookup<A extends Record<string, ControlDefinition[]>>(
@@ -247,48 +252,3 @@ export function visitFormDataInContext<A>(
247
252
  const dataNode = lookupDataNode(node.definition, parentContext);
248
253
  return visitFormData(node, dataNode ?? parentContext, cb, !dataNode);
249
254
  }
250
-
251
- export interface FormDataNode {
252
- parent?: FormDataNode;
253
- formNode: FormNode;
254
- parentData: SchemaDataNode;
255
- childIndex?: number;
256
- }
257
-
258
- export function visitFormDataNode<A>(
259
- node: FormDataNode,
260
- visitFn: (node: FormDataNode, data?: SchemaDataNode) => A | undefined,
261
- ): A | undefined {
262
- const dataNode = lookupDataNode(node.formNode.definition, node.parentData);
263
- const v = visitFn(node, dataNode);
264
- if (v !== undefined) return v;
265
-
266
- const parentData = dataNode ?? node.parentData;
267
- if (parentData.schema.field.collection && parentData.elementIndex == null) {
268
- const elemCount = parentData.control.elements.length;
269
- for (let i = 0; i < elemCount; i++) {
270
- const v = visitChildren(parentData.getChildElement(i));
271
- if (v !== undefined) return v;
272
- }
273
- return undefined;
274
- } else {
275
- return visitChildren(parentData);
276
- }
277
-
278
- function visitChildren(parentData: SchemaDataNode) {
279
- const children = node.formNode.getChildNodes();
280
- for (let i = 0; i < children.length; i++) {
281
- const child = children[i];
282
- const res = visitFormDataNode(
283
- {
284
- formNode: child,
285
- parent: node,
286
- parentData,
287
- childIndex: i,
288
- },
289
- visitFn,
290
- );
291
- if (res !== undefined) return res;
292
- }
293
- }
294
- }