-
Notifications
You must be signed in to change notification settings - Fork 1
Description
In the multi-select component we are attaching a click event handler to the tp-multi-select-field component in the connectedCallback function -
web-components/src/multi-select/tp-multi-select-field.ts
Lines 13 to 15 in 0282694
| 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.
So, to solve this issue, I see two possibilities -
- Use
constructorinstead ofconnectedCallback, 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. 🙏
