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.
@@ -602,7 +602,7 @@ declare class EasyForm extends BrowserHTMLElement {
602
602
  */
603
603
  private updateSingleField;
604
604
  /**
605
- * Busca un campo por nombre en el schema
605
+ * Busca un campo por nombre en el schema (soporta rutas como 'group.subfield')
606
606
  */
607
607
  private findFieldInSchema;
608
608
  /**
package/dist/easy-form.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) {
@@ -2784,6 +2800,26 @@ var StateManager = class {
2784
2800
  values[key] = existingValues[key];
2785
2801
  }
2786
2802
  }
2803
+ for (const field of topLevelFields) {
2804
+ if (field.type === "array" && "minItems" in field && "itemSchema" in field) {
2805
+ const arrayField = field;
2806
+ const minItems = arrayField.minItems ?? 0;
2807
+ if (minItems >= 1 && arrayField.itemSchema?.fields?.length) {
2808
+ const arr = getNestedValue(values, field.name);
2809
+ if (Array.isArray(arr) && arr.length < minItems) {
2810
+ const padded = [...arr];
2811
+ for (let i = arr.length; i < minItems; i++) {
2812
+ const item = {};
2813
+ for (const subField of arrayField.itemSchema.fields) {
2814
+ this.initializeFieldValue(subField, item);
2815
+ }
2816
+ padded.push(item);
2817
+ }
2818
+ setNestedValue(values, field.name, padded);
2819
+ }
2820
+ }
2821
+ }
2822
+ }
2787
2823
  this.state.values = values;
2788
2824
  }
2789
2825
  /**
@@ -2805,9 +2841,25 @@ var StateManager = class {
2805
2841
  values[field.name] = null;
2806
2842
  }
2807
2843
  break;
2808
- case "array":
2809
- values[field.name] = [];
2844
+ case "array": {
2845
+ const arrayField = field;
2846
+ const minItems = arrayField.minItems ?? 0;
2847
+ const itemFields = arrayField.itemSchema?.fields;
2848
+ if (minItems >= 1 && itemFields?.length) {
2849
+ const arr = [];
2850
+ for (let i = 0; i < minItems; i++) {
2851
+ const item = {};
2852
+ for (const subField of itemFields) {
2853
+ this.initializeFieldValue(subField, item);
2854
+ }
2855
+ arr.push(item);
2856
+ }
2857
+ values[field.name] = arr;
2858
+ } else {
2859
+ values[field.name] = [];
2860
+ }
2810
2861
  break;
2862
+ }
2811
2863
  case "group":
2812
2864
  values[field.name] = {};
2813
2865
  if ("fields" in field) {
@@ -2856,6 +2908,19 @@ var StateManager = class {
2856
2908
  getValue(fieldName) {
2857
2909
  return getNestedValue(this.state.values, fieldName);
2858
2910
  }
2911
+ /**
2912
+ * Crea un ítem nuevo con valores por defecto para un campo array
2913
+ */
2914
+ createDefaultArrayItem(arrayField) {
2915
+ const itemSchema = arrayField.itemSchema;
2916
+ const itemFields = itemSchema?.fields;
2917
+ if (!itemFields?.length) return {};
2918
+ const item = {};
2919
+ for (const subField of itemFields) {
2920
+ this.initializeFieldValue(subField, item);
2921
+ }
2922
+ return item;
2923
+ }
2859
2924
  /**
2860
2925
  * Establece un valor
2861
2926
  */
@@ -6476,7 +6541,9 @@ var EasyForm = class extends BrowserHTMLElement {
6476
6541
  content.className = "easy-form-group-content";
6477
6542
  if ("fields" in field && field.fields) {
6478
6543
  for (const subField of field.fields) {
6479
- const fieldElement = this.renderField(subField);
6544
+ const fullName = subField.name.startsWith(field.name + ".") ? subField.name : `${field.name}.${subField.name}`;
6545
+ const subFieldWithPath = { ...subField, name: fullName };
6546
+ const fieldElement = this.renderField(subFieldWithPath);
6480
6547
  if (fieldElement) {
6481
6548
  content.appendChild(fieldElement);
6482
6549
  }
@@ -6519,7 +6586,9 @@ var EasyForm = class extends BrowserHTMLElement {
6519
6586
  }
6520
6587
  if ("fields" in field && field.fields) {
6521
6588
  for (const subField of field.fields) {
6522
- const fieldElement = this.renderField(subField);
6589
+ const fullName = subField.name.startsWith(field.name + ".") ? subField.name : `${field.name}.${subField.name}`;
6590
+ const subFieldWithPath = { ...subField, name: fullName };
6591
+ const fieldElement = this.renderField(subFieldWithPath);
6523
6592
  if (fieldElement) {
6524
6593
  groupContainer.appendChild(fieldElement);
6525
6594
  }
@@ -6572,6 +6641,8 @@ var EasyForm = class extends BrowserHTMLElement {
6572
6641
  }
6573
6642
  const values = this.stateManager.getValue(field.name) || [];
6574
6643
  const arrayField = field;
6644
+ const minItems = arrayField.minItems ?? 0;
6645
+ const maxItems = arrayField.maxItems ?? Infinity;
6575
6646
  const itemsContainer = document.createElement("div");
6576
6647
  itemsContainer.className = "easy-form-array-items";
6577
6648
  for (let i = 0; i < values.length; i++) {
@@ -6593,7 +6664,8 @@ var EasyForm = class extends BrowserHTMLElement {
6593
6664
  removeButton.type = "button";
6594
6665
  removeButton.textContent = "Eliminar";
6595
6666
  removeButton.className = "easy-form-array-remove";
6596
- if (this.disabled || this.loading) {
6667
+ const atMinItems = values.length <= minItems;
6668
+ if (this.disabled || this.loading || atMinItems) {
6597
6669
  removeButton.disabled = true;
6598
6670
  } else {
6599
6671
  removeButton.addEventListener("click", () => {
@@ -6610,11 +6682,13 @@ var EasyForm = class extends BrowserHTMLElement {
6610
6682
  addButton.type = "button";
6611
6683
  addButton.textContent = "Agregar";
6612
6684
  addButton.className = "easy-form-array-add";
6613
- if (this.disabled || this.loading) {
6685
+ const atMaxItems = values.length >= maxItems;
6686
+ if (this.disabled || this.loading || atMaxItems) {
6614
6687
  addButton.disabled = true;
6615
6688
  } else {
6616
6689
  addButton.addEventListener("click", () => {
6617
- const newValues = [...values, {}];
6690
+ const newItem = this.stateManager.createDefaultArrayItem(field);
6691
+ const newValues = [...values, newItem];
6618
6692
  this.handleFieldChange(field.name, newValues);
6619
6693
  });
6620
6694
  }
@@ -6912,10 +6986,22 @@ var EasyForm = class extends BrowserHTMLElement {
6912
6986
  }
6913
6987
  }
6914
6988
  /**
6915
- * Busca un campo por nombre en el schema
6989
+ * Busca un campo por nombre en el schema (soporta rutas como 'group.subfield')
6916
6990
  */
6917
6991
  findFieldInSchema(schema, name) {
6918
6992
  const fields = schema.fields || [];
6993
+ const dot = name.indexOf(".");
6994
+ if (dot > 0) {
6995
+ const parentName = name.slice(0, dot);
6996
+ const childName = name.slice(dot + 1);
6997
+ const parent = fields.find((f) => f.name === parentName);
6998
+ if (!parent) return null;
6999
+ if ((parent.type === "group" || parent.type === "row") && "fields" in parent && parent.fields) {
7000
+ const found = this.findFieldInSchema({ fields: parent.fields }, childName);
7001
+ return found ? { ...found, name } : null;
7002
+ }
7003
+ return null;
7004
+ }
6919
7005
  for (const field of fields) {
6920
7006
  if (field.name === name) {
6921
7007
  return field;