@dile/ui 2.14.2 → 2.15.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.
@@ -31,20 +31,46 @@ export const DileForm = (superclass) => class extends superclass {
31
31
  getData() {
32
32
  let data = {};
33
33
  this.allNodeElements.forEach(node => {
34
- data[node.getAttribute('name')] = node.value;
34
+ const name = node.getAttribute('name');
35
+ const isArray = name.endsWith('[]');
36
+ const fieldName = isArray ? name.slice(0, -2) : name;
37
+
38
+ if (isArray) {
39
+ if (!data[fieldName]) {
40
+ data[fieldName] = [];
41
+ }
42
+ data[fieldName].push(node.value);
43
+ } else {
44
+ data[fieldName] = node.value;
45
+ }
35
46
  });
36
47
  return data;
37
48
  }
38
49
 
39
50
  setData(data) {
40
51
  this.allNodeElements.forEach(node => {
41
- if(data[node.getAttribute('name')] !== undefined && data[node.getAttribute('name')] !== null) {
52
+ const name = node.getAttribute('name');
53
+ const isArray = name.endsWith('[]');
54
+ const fieldName = isArray ? name.slice(0, -2) : name;
55
+
56
+ if (isArray && Array.isArray(data[fieldName])) {
57
+ // with arrays, the index is the element order in the DOM
58
+ const arrayElements = Array.from(this.allNodeElements).filter(el => el.getAttribute('name') === name);
59
+ const elementIndex = arrayElements.indexOf(node);
60
+ if (elementIndex >= 0 && elementIndex < data[fieldName].length) {
61
+ const value = data[fieldName][elementIndex];
62
+ if (typeof node.set === "function") {
63
+ node.set(value);
64
+ } else {
65
+ node.value = value;
66
+ }
67
+ }
68
+ } else if (!isArray && data[fieldName] !== undefined && data[fieldName] !== null) {
42
69
  if (typeof node.set === "function") {
43
- node.set(data[node.getAttribute('name')]);
70
+ node.set(data[fieldName]);
44
71
  } else {
45
- node.value = data[node.getAttribute('name')];
72
+ node.value = data[fieldName];
46
73
  }
47
-
48
74
  }
49
75
  });
50
76
  }
@@ -64,6 +90,15 @@ export const DileForm = (superclass) => class extends superclass {
64
90
  }
65
91
 
66
92
  getNodeElement(name) {
93
+ // Check if it's an array field reference like "items.0"
94
+ const arrayMatch = name.match(/^(.+)\.(\d+)$/);
95
+ if (arrayMatch) {
96
+ const fieldName = arrayMatch[1];
97
+ const index = parseInt(arrayMatch[2]);
98
+ const arrayElements = this.shadowRoot.querySelectorAll(`[name="${fieldName}[]"]`);
99
+ return arrayElements[index] || null;
100
+ }
101
+ // Regular field
67
102
  return this.shadowRoot.querySelector('[name="' + name + '"]');
68
103
  }
69
104
 
@@ -72,17 +107,51 @@ export const DileForm = (superclass) => class extends superclass {
72
107
  }
73
108
 
74
109
  resetField(name) {
75
- if(this.firstValue.hasOwnProperty(name)) {
110
+ // Check if it's an array field reference like "items.0"
111
+ const arrayMatch = name.match(/^(.+)\.(\d+)$/);
112
+ if (arrayMatch) {
113
+ const fieldName = arrayMatch[1];
114
+ const index = parseInt(arrayMatch[2]);
115
+ if (this.firstValue.hasOwnProperty(fieldName) && Array.isArray(this.firstValue[fieldName])) {
116
+ const value = this.firstValue[fieldName][index];
117
+ const node = this.getNodeElement(name);
118
+ if (node) {
119
+ node.value = value;
120
+ }
121
+ }
122
+ } else if (this.firstValue.hasOwnProperty(name)) {
76
123
  let value = this.firstValue[name];
77
124
  let node = this.getNodeElement(name);
78
- node.value = value;
125
+ if (node) {
126
+ node.value = value;
127
+ }
79
128
  }
80
129
  }
81
130
 
82
131
  showError(name, error) {
83
- let node = this.getNodeElement(name);
84
- node.errored = true;
85
- node.message = error;
132
+ const parts = name.split('.');
133
+
134
+ // Check if it's a nested field reference like "product_lines.0.name"
135
+ if (parts.length > 2) {
136
+ const fieldName = parts[0];
137
+ const nestedPath = parts.slice(1).join('.');
138
+ const node = this.shadowRoot.querySelector('[name="' + fieldName + '"]');
139
+ if (node) {
140
+ node.errored = true;
141
+ node.message = nestedPath + ':' + error;
142
+ } else {
143
+ console.warn(`Error to ${name} not shown because this element is not found`);
144
+ }
145
+ } else {
146
+ // Regular field or array element reference (handled by getNodeElement)
147
+ let node = this.getNodeElement(name);
148
+ if(node) {
149
+ node.errored = true;
150
+ node.message = error;
151
+ } else {
152
+ console.warn(`Error to ${name} not shown because this element is not found`);
153
+ }
154
+ }
86
155
  }
87
156
 
88
157
  showErrors(errors) {
@@ -106,8 +175,22 @@ export const DileForm = (superclass) => class extends superclass {
106
175
  return true;
107
176
  }
108
177
  for (let prop of originProperties) {
109
- if (this.firstValue[prop] != currentData[prop]) {
110
- return true; // Si alguna propiedad no coincide, retorna false
178
+ const isArray = Array.isArray(this.firstValue[prop]);
179
+ if (isArray) {
180
+ const originalArray = this.firstValue[prop];
181
+ const currentArray = currentData[prop];
182
+ if (!Array.isArray(currentArray) || originalArray.length !== currentArray.length) {
183
+ return true;
184
+ }
185
+ for (let i = 0; i < originalArray.length; i++) {
186
+ if (originalArray[i] != currentArray[i]) {
187
+ return true;
188
+ }
189
+ }
190
+ } else {
191
+ if (this.firstValue[prop] != currentData[prop]) {
192
+ return true;
193
+ }
111
194
  }
112
195
  }
113
196
  return false;
@@ -150,9 +150,6 @@ export const DileSelectable = (SuperClass) =>
150
150
 
151
151
  updated(changedProperties) {
152
152
  this.setSelectedItem();
153
- if(changedProperties.has('selected')) {
154
- this.dispatchSelectedChanged();
155
- }
156
153
  }
157
154
 
158
155
  getItems() {
@@ -166,4 +163,9 @@ export const DileSelectable = (SuperClass) =>
166
163
  }
167
164
  }
168
165
  }
166
+
167
+ changeSelectedTo(elem) {
168
+ this.selected = elem;
169
+ this.dispatchSelectedChanged();
170
+ }
169
171
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dile/ui",
3
- "version": "2.14.2",
3
+ "version": "2.15.0",
4
4
  "description": "UI Core components from dile-components.",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -26,5 +26,5 @@
26
26
  "publishConfig": {
27
27
  "access": "public"
28
28
  },
29
- "gitHead": "7043cb3237acf07b8c4bae726742ed390883d21a"
29
+ "gitHead": "f90d37caf2088efed29d1e0aa4934b6ac3547d95"
30
30
  }