Skip to content

Components not being removed if they're extending fragment #29

@doorgan

Description

@doorgan

I've noticed that when you have a list of components extending fragment, if you remove items from the list and re-render it, the list won't get updated.

I've made a reproduction in this codesandbox

import { define, ref, html } from "heresy";

const Item = {
  name: "Item",
  extends: "fragment",

  onclick() {
    this.dispatchEvent(new CustomEvent("remove", { detail: this.item }));
  },

  render() {
    this.html`
    <p>
      This is item <strong>${this.item.id}</strong>
      <button onclick=${this}>Remove me!</button>
    </p>
    `;
  }
};

const ItemList = {
  name: "ItemList",
  extends: "fragment",

  includes: { Item },

  mappedAttributes: ["items"],

  oninit() {
    this.items = [];
  },

  add_item() {
    this.items.push({ id: Math.random() });
    this.items = this.items;
  },

  onitems() {
    this.render();
  },

  onremove({ detail: item }) {
    this.items = this.items.filter(x => x != item);
  },

  render() {
    this.html`
    <div class="number-list">
      ${this.items.map(
        item => html.for(this, item.id)`
          <Item .item=${item} onremove=${this} />
        `
      )}
    </div>
    `;
  }
};

const MainComponent = {
  name: "MainComponent",
  extends: "element",

  includes: { ItemList },

  oninit() {
    this.item_list = ref();
  },

  onclick() {
    this.item_list.current.add_item();
  },

  render() {
    this.html`
      <ItemList ref=${this.item_list}/>
      <button onclick=${this}>Add item!</button>
    `;
  }
};

define(MainComponent);

Adding Items works fine, but they don't get removed from the dom. This doesn't happen if the component extends some element, like div.

Maybe it has to do with the fact that fragments are not dom nodes, and the dom diffing breaks?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions