Skip to content

Multi-select component not compatible with Modal Component #35

@Souptik2001

Description

@Souptik2001

In the multi-select component we are attaching a click event handler to the tp-multi-select-field component in the connectedCallback function -

connectedCallback(): void {
this.addEventListener( 'click', this.toggleOpen.bind( this ) );
}

But here is a edge case for that. Suppose we use this component inside a modal where we take the modal content and append it at the end of the body something like this -

this.body.appendChild( this );

In this case the connectedCallback of the multi-select component runs twice, and because of that the event listener gets attached twice to the element. And because of this the options dropdown never opens, because one of the event listener opens it and another closes it instantly.
Here I have given example of modal, but this is true with any wrapper which removes and attaches the component again and again.

image

So, to solve this issue, I see two possibilities -

  • Use constructor instead of connectedCallback, which will run only once -
constructor() {
  super();
  this.addEventListener("click", this.toggleOpen.bind(this));
}
  • Or remove the event listener before adding it -
connectedCallback() {
	this.removeEventListener("click", this.toggleOpen.bind(this));
	this.addEventListener("click", this.toggleOpen.bind(this));
}

@junaidbhura @i-am-chitti Please let me know your thoughts on that. 🙏

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions