easy-forms-core 1.1.16 → 1.1.18

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/index.d.ts CHANGED
@@ -651,7 +651,7 @@ declare class EasyForm extends BrowserHTMLElement {
651
651
  */
652
652
  private updateSingleField;
653
653
  /**
654
- * Busca un campo por nombre en el schema
654
+ * Busca un campo por nombre en el schema (soporta rutas como 'group.subfield')
655
655
  */
656
656
  private findFieldInSchema;
657
657
  /**
@@ -843,6 +843,10 @@ declare class StateManager {
843
843
  * Obtiene un valor del formulario
844
844
  */
845
845
  getValue(fieldName: string): any;
846
+ /**
847
+ * Crea un ítem nuevo con valores por defecto para un campo array
848
+ */
849
+ createDefaultArrayItem(arrayField: Field): Record<string, any>;
846
850
  /**
847
851
  * Establece un valor
848
852
  */
package/dist/index.js CHANGED
@@ -95,6 +95,22 @@ var SchemaParser = class {
95
95
  `Field "${field.name}" de tipo array debe tener itemSchema`
96
96
  );
97
97
  }
98
+ if ("minItems" in field) {
99
+ const min = field.minItems;
100
+ if (min < 0) {
101
+ throw new Error(
102
+ `Field "${field.name}" de tipo array: minItems debe ser >= 0`
103
+ );
104
+ }
105
+ if ("maxItems" in field) {
106
+ const max = field.maxItems;
107
+ if (min > max) {
108
+ throw new Error(
109
+ `Field "${field.name}" de tipo array: minItems (${min}) no puede ser mayor que maxItems (${max})`
110
+ );
111
+ }
112
+ }
113
+ }
98
114
  break;
99
115
  case "group":
100
116
  if (!("fields" in field) || !field.fields || field.fields.length === 0) {
@@ -2790,6 +2806,26 @@ var StateManager = class {
2790
2806
  values[key] = existingValues[key];
2791
2807
  }
2792
2808
  }
2809
+ for (const field of topLevelFields) {
2810
+ if (field.type === "array" && "minItems" in field && "itemSchema" in field) {
2811
+ const arrayField = field;
2812
+ const minItems = arrayField.minItems ?? 0;
2813
+ if (minItems >= 1 && arrayField.itemSchema?.fields?.length) {
2814
+ const arr = getNestedValue(values, field.name);
2815
+ if (Array.isArray(arr) && arr.length < minItems) {
2816
+ const padded = [...arr];
2817
+ for (let i = arr.length; i < minItems; i++) {
2818
+ const item = {};
2819
+ for (const subField of arrayField.itemSchema.fields) {
2820
+ this.initializeFieldValue(subField, item);
2821
+ }
2822
+ padded.push(item);
2823
+ }
2824
+ setNestedValue(values, field.name, padded);
2825
+ }
2826
+ }
2827
+ }
2828
+ }
2793
2829
  this.state.values = values;
2794
2830
  }
2795
2831
  /**
@@ -2811,9 +2847,25 @@ var StateManager = class {
2811
2847
  values[field.name] = null;
2812
2848
  }
2813
2849
  break;
2814
- case "array":
2815
- values[field.name] = [];
2850
+ case "array": {
2851
+ const arrayField = field;
2852
+ const minItems = arrayField.minItems ?? 0;
2853
+ const itemFields = arrayField.itemSchema?.fields;
2854
+ if (minItems >= 1 && itemFields?.length) {
2855
+ const arr = [];
2856
+ for (let i = 0; i < minItems; i++) {
2857
+ const item = {};
2858
+ for (const subField of itemFields) {
2859
+ this.initializeFieldValue(subField, item);
2860
+ }
2861
+ arr.push(item);
2862
+ }
2863
+ values[field.name] = arr;
2864
+ } else {
2865
+ values[field.name] = [];
2866
+ }
2816
2867
  break;
2868
+ }
2817
2869
  case "group":
2818
2870
  values[field.name] = {};
2819
2871
  if ("fields" in field) {
@@ -2862,6 +2914,19 @@ var StateManager = class {
2862
2914
  getValue(fieldName) {
2863
2915
  return getNestedValue(this.state.values, fieldName);
2864
2916
  }
2917
+ /**
2918
+ * Crea un ítem nuevo con valores por defecto para un campo array
2919
+ */
2920
+ createDefaultArrayItem(arrayField) {
2921
+ const itemSchema = arrayField.itemSchema;
2922
+ const itemFields = itemSchema?.fields;
2923
+ if (!itemFields?.length) return {};
2924
+ const item = {};
2925
+ for (const subField of itemFields) {
2926
+ this.initializeFieldValue(subField, item);
2927
+ }
2928
+ return item;
2929
+ }
2865
2930
  /**
2866
2931
  * Establece un valor
2867
2932
  */
@@ -6485,7 +6550,9 @@ var EasyForm = class extends BrowserHTMLElement {
6485
6550
  content.className = "easy-form-group-content";
6486
6551
  if ("fields" in field && field.fields) {
6487
6552
  for (const subField of field.fields) {
6488
- const fieldElement = this.renderField(subField);
6553
+ const fullName = subField.name.startsWith(field.name + ".") ? subField.name : `${field.name}.${subField.name}`;
6554
+ const subFieldWithPath = { ...subField, name: fullName };
6555
+ const fieldElement = this.renderField(subFieldWithPath);
6489
6556
  if (fieldElement) {
6490
6557
  content.appendChild(fieldElement);
6491
6558
  }
@@ -6528,7 +6595,9 @@ var EasyForm = class extends BrowserHTMLElement {
6528
6595
  }
6529
6596
  if ("fields" in field && field.fields) {
6530
6597
  for (const subField of field.fields) {
6531
- const fieldElement = this.renderField(subField);
6598
+ const fullName = subField.name.startsWith(field.name + ".") ? subField.name : `${field.name}.${subField.name}`;
6599
+ const subFieldWithPath = { ...subField, name: fullName };
6600
+ const fieldElement = this.renderField(subFieldWithPath);
6532
6601
  if (fieldElement) {
6533
6602
  groupContainer.appendChild(fieldElement);
6534
6603
  }
@@ -6581,6 +6650,8 @@ var EasyForm = class extends BrowserHTMLElement {
6581
6650
  }
6582
6651
  const values = this.stateManager.getValue(field.name) || [];
6583
6652
  const arrayField = field;
6653
+ const minItems = arrayField.minItems ?? 0;
6654
+ const maxItems = arrayField.maxItems ?? Infinity;
6584
6655
  const itemsContainer = document.createElement("div");
6585
6656
  itemsContainer.className = "easy-form-array-items";
6586
6657
  for (let i = 0; i < values.length; i++) {
@@ -6602,7 +6673,8 @@ var EasyForm = class extends BrowserHTMLElement {
6602
6673
  removeButton.type = "button";
6603
6674
  removeButton.textContent = "Eliminar";
6604
6675
  removeButton.className = "easy-form-array-remove";
6605
- if (this.disabled || this.loading) {
6676
+ const atMinItems = values.length <= minItems;
6677
+ if (this.disabled || this.loading || atMinItems) {
6606
6678
  removeButton.disabled = true;
6607
6679
  } else {
6608
6680
  removeButton.addEventListener("click", () => {
@@ -6619,11 +6691,13 @@ var EasyForm = class extends BrowserHTMLElement {
6619
6691
  addButton.type = "button";
6620
6692
  addButton.textContent = "Agregar";
6621
6693
  addButton.className = "easy-form-array-add";
6622
- if (this.disabled || this.loading) {
6694
+ const atMaxItems = values.length >= maxItems;
6695
+ if (this.disabled || this.loading || atMaxItems) {
6623
6696
  addButton.disabled = true;
6624
6697
  } else {
6625
6698
  addButton.addEventListener("click", () => {
6626
- const newValues = [...values, {}];
6699
+ const newItem = this.stateManager.createDefaultArrayItem(field);
6700
+ const newValues = [...values, newItem];
6627
6701
  this.handleFieldChange(field.name, newValues);
6628
6702
  });
6629
6703
  }
@@ -6921,10 +6995,22 @@ var EasyForm = class extends BrowserHTMLElement {
6921
6995
  }
6922
6996
  }
6923
6997
  /**
6924
- * Busca un campo por nombre en el schema
6998
+ * Busca un campo por nombre en el schema (soporta rutas como 'group.subfield')
6925
6999
  */
6926
7000
  findFieldInSchema(schema, name) {
6927
7001
  const fields = schema.fields || [];
7002
+ const dot = name.indexOf(".");
7003
+ if (dot > 0) {
7004
+ const parentName = name.slice(0, dot);
7005
+ const childName = name.slice(dot + 1);
7006
+ const parent = fields.find((f) => f.name === parentName);
7007
+ if (!parent) return null;
7008
+ if ((parent.type === "group" || parent.type === "row") && "fields" in parent && parent.fields) {
7009
+ const found = this.findFieldInSchema({ fields: parent.fields }, childName);
7010
+ return found ? { ...found, name } : null;
7011
+ }
7012
+ return null;
7013
+ }
6928
7014
  for (const field of fields) {
6929
7015
  if (field.name === name) {
6930
7016
  return field;