easy-forms-core 1.1.17 → 1.1.19

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) {
@@ -1158,7 +1174,7 @@ function getPlanoStyles(_colors) {
1158
1174
  }
1159
1175
  .easy-form-array-item {
1160
1176
  border: none;
1161
- border-bottom: 1px solid var(--easy-form-border);
1177
+ border-bottom: 1px solid var(--easy-form-group-border);
1162
1178
  border-radius: 0;
1163
1179
  }
1164
1180
  `;
@@ -1195,7 +1211,7 @@ function getTradicionalStyles(_colors) {
1195
1211
  border-color: var(--easy-form-primary);
1196
1212
  }
1197
1213
  .easy-form-array-item {
1198
- border: 2px solid var(--easy-form-border);
1214
+ border: 2px solid var(--easy-form-group-border);
1199
1215
  border-radius: 4px;
1200
1216
  background: #f8f9fa;
1201
1217
  }
@@ -1245,7 +1261,7 @@ function getMaterialStyles(_colors) {
1245
1261
  .easy-form-array-item {
1246
1262
  border: none;
1247
1263
  border-radius: 4px;
1248
- background: #f5f5f5;
1264
+ background: var(--easy-form-group-background);
1249
1265
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.12);
1250
1266
  }
1251
1267
  `;
@@ -1290,10 +1306,10 @@ function getRoundedShadowStyles(_colors) {
1290
1306
  box-shadow: 0 4px 16px rgba(0, 123, 255, 0.3);
1291
1307
  }
1292
1308
  .easy-form-array-item {
1293
- border: 1px solid var(--easy-form-border);
1309
+ border: 1px solid var(--easy-form-group-border);
1294
1310
  border-radius: 12px;
1295
1311
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
1296
- background: var(--easy-form-background);
1312
+ background: var(--easy-form-group-background);
1297
1313
  }
1298
1314
  `;
1299
1315
  }
@@ -1353,7 +1369,7 @@ function getLinesStyles(_colors) {
1353
1369
  }
1354
1370
  .easy-form-array-item {
1355
1371
  border: none;
1356
- border-bottom: 1px solid var(--easy-form-border);
1372
+ border-bottom: 1px solid var(--easy-form-group-border);
1357
1373
  border-radius: 0;
1358
1374
  }
1359
1375
  `;
@@ -1435,7 +1451,7 @@ function getShadcnStyles(_colors) {
1435
1451
  .easy-form-array-item {
1436
1452
  border: 1.5px solid #e4e4e7;
1437
1453
  border-radius: 8px;
1438
- background: linear-gradient(to bottom, var(--easy-form-background), #fafafa);
1454
+ background: var(--easy-form-group-background);
1439
1455
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.06);
1440
1456
  }
1441
1457
  `;
@@ -1534,7 +1550,7 @@ function getChakraStyles(_colors) {
1534
1550
  transition: all 0.25s ease;
1535
1551
  }
1536
1552
  .easy-form-array-item:hover {
1537
- border-color: var(--easy-form-primary);
1553
+ border-color: var(--easy-form-group-border);
1538
1554
  box-shadow: 0 2px 8px rgba(0, 123, 255, 0.15);
1539
1555
  }
1540
1556
  `;
@@ -1612,9 +1628,9 @@ function getMantineStyles(_colors) {
1612
1628
  transform: translateY(-3px) scale(1.05);
1613
1629
  }
1614
1630
  .easy-form-array-item {
1615
- border: 1px solid #dee2e6;
1631
+ border: 1px solid var(--easy-form-group-border);
1616
1632
  border-radius: 12px;
1617
- background: var(--easy-form-background);
1633
+ background: var(--easy-form-group-background);
1618
1634
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.06), inset 0 1px 0 rgba(255, 255, 255, 0.8);
1619
1635
  }
1620
1636
  `;
@@ -1798,9 +1814,9 @@ function getBorderedStyles(_colors) {
1798
1814
  transform: translate(-4px, -4px);
1799
1815
  }
1800
1816
  .easy-form-array-item {
1801
- border: 3px solid #1a1a1a;
1817
+ border: 3px solid var(--easy-form-group-border);
1802
1818
  border-radius: 0;
1803
- background: var(--easy-form-background);
1819
+ background: var(--easy-form-group-background);
1804
1820
  box-shadow: 4px 4px 0 rgba(0, 0, 0, 0.1);
1805
1821
  }
1806
1822
  `;
@@ -1928,7 +1944,7 @@ function getMinimalStyles(_colors) {
1928
1944
  }
1929
1945
  .easy-form-array-item {
1930
1946
  border: none;
1931
- border-bottom: 1px solid #e5e7eb;
1947
+ border-bottom: 1px solid var(--easy-form-group-border);
1932
1948
  border-radius: 0;
1933
1949
  background: transparent;
1934
1950
  padding-bottom: 1.5rem;
@@ -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
  }