easy-forms-core 1.1.17 → 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/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
  */
@@ -6576,6 +6641,8 @@ var EasyForm = class extends BrowserHTMLElement {
6576
6641
  }
6577
6642
  const values = this.stateManager.getValue(field.name) || [];
6578
6643
  const arrayField = field;
6644
+ const minItems = arrayField.minItems ?? 0;
6645
+ const maxItems = arrayField.maxItems ?? Infinity;
6579
6646
  const itemsContainer = document.createElement("div");
6580
6647
  itemsContainer.className = "easy-form-array-items";
6581
6648
  for (let i = 0; i < values.length; i++) {
@@ -6597,7 +6664,8 @@ var EasyForm = class extends BrowserHTMLElement {
6597
6664
  removeButton.type = "button";
6598
6665
  removeButton.textContent = "Eliminar";
6599
6666
  removeButton.className = "easy-form-array-remove";
6600
- if (this.disabled || this.loading) {
6667
+ const atMinItems = values.length <= minItems;
6668
+ if (this.disabled || this.loading || atMinItems) {
6601
6669
  removeButton.disabled = true;
6602
6670
  } else {
6603
6671
  removeButton.addEventListener("click", () => {
@@ -6614,11 +6682,13 @@ var EasyForm = class extends BrowserHTMLElement {
6614
6682
  addButton.type = "button";
6615
6683
  addButton.textContent = "Agregar";
6616
6684
  addButton.className = "easy-form-array-add";
6617
- if (this.disabled || this.loading) {
6685
+ const atMaxItems = values.length >= maxItems;
6686
+ if (this.disabled || this.loading || atMaxItems) {
6618
6687
  addButton.disabled = true;
6619
6688
  } else {
6620
6689
  addButton.addEventListener("click", () => {
6621
- const newValues = [...values, {}];
6690
+ const newItem = this.stateManager.createDefaultArrayItem(field);
6691
+ const newValues = [...values, newItem];
6622
6692
  this.handleFieldChange(field.name, newValues);
6623
6693
  });
6624
6694
  }