Skip to content

Commit 4c8d242

Browse files
authored
fix: keep previously added custom slotted elements (#5878)
1 parent 2631384 commit 4c8d242

2 files changed

Lines changed: 47 additions & 9 deletions

File tree

‎packages/component-base/src/slot-controller.js‎

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -211,19 +211,20 @@ export class SlotController extends EventTarget {
211211
}
212212

213213
if (newNodes && newNodes.length > 0) {
214-
// Custom node is added, remove the current one.
215-
current.forEach((node) => {
216-
if (node && node.isConnected) {
217-
node.parentNode.removeChild(node);
218-
}
219-
});
220-
221214
if (this.multiple) {
222-
this.nodes = newNodes;
215+
// Remove default node if exists
216+
if (this.defaultNode) {
217+
this.defaultNode.remove();
218+
}
219+
this.nodes = [...current, ...newNodes].filter((node) => node !== this.defaultNode);
223220
newNodes.forEach((node) => {
224221
this.initAddedNode(node);
225222
});
226223
} else {
224+
// Remove previous node if exists
225+
if (this.node) {
226+
this.node.remove();
227+
}
227228
this.node = newNodes[0];
228229
this.initAddedNode(this.node);
229230
}

‎packages/component-base/test/slot-controller.test.js‎

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,7 @@ describe('slot-controller', () => {
359359
expect(children[0]).to.be.instanceOf(HTMLButtonElement);
360360
});
361361

362-
it('should replace an element when new elements added to the slot', async () => {
362+
it('should remove default node when new elements added to the slot', async () => {
363363
const defaultNode = children[0];
364364

365365
const foo = document.createElement('div');
@@ -375,6 +375,20 @@ describe('slot-controller', () => {
375375
expect(slotChildren[1]).to.equal(bar);
376376
expect(defaultNode.isConnected).to.be.false;
377377
});
378+
379+
it('should store a reference to the default node in nodes array', () => {
380+
const nodes = controller.nodes;
381+
expect(nodes).to.be.instanceOf(Array);
382+
expect(nodes.length).to.equal(1);
383+
expect(nodes[0]).to.equal(controller.defaultNode);
384+
});
385+
386+
it('should remove default node from nodes array when adding new element', async () => {
387+
const nodes = controller.nodes;
388+
expect(nodes).to.be.instanceOf(Array);
389+
expect(nodes.length).to.equal(1);
390+
expect(nodes[0]).to.equal(controller.defaultNode);
391+
});
378392
});
379393

380394
describe('custom', () => {
@@ -394,10 +408,25 @@ describe('slot-controller', () => {
394408
it('should store a reference to custom slotted elements in nodes array', () => {
395409
const nodes = controller.nodes;
396410
expect(nodes).to.be.instanceOf(Array);
411+
expect(nodes.length).to.equal(2);
397412
expect(nodes[0]).to.equal(children[0]);
398413
expect(nodes[1]).to.equal(children[1]);
399414
});
400415

416+
it('should update references to custom slotted elements in nodes array', async () => {
417+
const newChild = document.createElement('div');
418+
element.append(newChild);
419+
420+
await nextFrame();
421+
422+
const nodes = controller.nodes;
423+
expect(nodes).to.be.instanceOf(Array);
424+
expect(nodes.length).to.equal(3);
425+
expect(nodes[0]).to.equal(children[0]);
426+
expect(nodes[1]).to.equal(children[1]);
427+
expect(nodes[2]).to.equal(newChild);
428+
});
429+
401430
it('should get a reference to list of elements passed to un-named slot', () => {
402431
const slotChildren = controller.getSlotChildren();
403432
expect(slotChildren[0]).to.equal(children[0]);
@@ -409,6 +438,14 @@ describe('slot-controller', () => {
409438
expect(initializeSpy.firstCall.args[0]).to.equal(children[0]);
410439
expect(initializeSpy.secondCall.args[0]).to.equal(children[1]);
411440
});
441+
442+
it('should not remove custom elements when adding another custom element', async () => {
443+
const newChild = document.createElement('div');
444+
element.append(newChild);
445+
446+
await nextFrame();
447+
expect(element.childElementCount).to.equal(3);
448+
});
412449
});
413450
});
414451
});

0 commit comments

Comments
 (0)