Skip to content

Commit b8efea0

Browse files
authored
fix: order slots in state as in Light DOM (#874)
FIXES: #873 Order slotted children in components state properly, keeping their order in the Light DOM, not the order the children are defined
1 parent 344340c commit b8efea0

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

packages/base/src/UI5Element.js

+15-3
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,9 @@ class UI5Element extends HTMLElement {
152152
}
153153

154154
const autoIncrementMap = new Map();
155-
const allChildrenUpgraded = domChildren.map(async child => {
155+
const slottedChildrenMap = new Map();
156+
157+
const allChildrenUpgraded = domChildren.map(async (child, idx) => {
156158
// Determine the type of the child (mainly by the slot attribute)
157159
const slotName = this.constructor._getSlotName(child);
158160
const slotData = slotsMap[slotName];
@@ -192,12 +194,22 @@ class UI5Element extends HTMLElement {
192194
this._attachChildPropertyUpdated(child, slotData);
193195
}
194196

195-
// Distribute the child in the _state object
196197
const propertyName = slotData.propertyName || slotName;
197-
this._state[propertyName].push(child);
198+
199+
if (slottedChildrenMap.has(propertyName)) {
200+
slottedChildrenMap.get(propertyName).push({ child, idx });
201+
} else {
202+
slottedChildrenMap.set(propertyName, [{ child, idx }]);
203+
}
198204
});
199205

200206
await Promise.all(allChildrenUpgraded);
207+
208+
// Distribute the child in the _state object, keeping the Light DOM order,
209+
// not the order elements are defined.
210+
slottedChildrenMap.forEach((children, slot) => {
211+
this._state[slot] = children.sort((a, b) => a.idx - b.idx).map(_ => _.child);
212+
});
201213
this._invalidate();
202214
}
203215

0 commit comments

Comments
 (0)