ngx-chips icon indicating copy to clipboard operation
ngx-chips copied to clipboard

Dropdown not closing inside a bootstrap modal

Open lucas-carneiro opened this issue 7 years ago • 7 comments

I'm submitting a ...

[x] bug report => search github for a similar issue or PR before submitting
[ ] support request/question

Current behavior When I use the tag-input-dropdown component inside a bootstrap (v 4.0.0+) modal, I can't close the dropdown menu clicking inside the modal. If I change the appendToBody property to false, is possible to close it by clicking inside, but its position is changed, appearing outside the modal. Note: I found someone who had the same problem on StackOverflow, but without answers/comments.

Expected behavior There are 2 options here:

  • The dropdown would be successfully closed when clicking inside the modal without changing the value of appendToBody.
  • The dropdown would be successfully closed when clicking inside the modal afterchanging the value of appendToBody, but its position would stay the same as option 1.

Minimal reproduction of the problem with instructions (if applicable) https://stackblitz.com/edit/angular-jmsqe1 Note: The dropdown position problem can be better seen by clicking on "Open in New Window" option in StackBlitz.

What do you use to build your app?. Please specify the version angular-cli

Angular version: 6.0.7

ngx-chips version: 1.9.2

Browser: Tested on Chrome 67

lucas-carneiro avatar Jun 29 '18 14:06 lucas-carneiro

I have same problem here.

lucianojs avatar Jul 12 '18 23:07 lucianojs

Any fix or solution to this?

Tawsif avatar Jul 30 '18 10:07 Tawsif

I think the modal is preventing blur events. That's the issue.

Gbuomprisco avatar Jul 30 '18 10:07 Gbuomprisco

@lucianojs @Gbuomprisco @lucas-carneiro I have a temporary fix for this issue:

add [appendToBody] = "false" to tag-input-dropdown and then add following css in global styles.css

.ng2-dropdown-menu { position: absolute !important; left: 2% !important; top: 100% !important; }

Tawsif avatar Jul 30 '18 12:07 Tawsif

To get the dropdown to hide in a modal/pop-up, [appendToBody]="false" was messing up positioning of the tag-input-dropdown so I set it to:

<tag-input-dropdown
[appendToBody]="true"
[zIndex]="2000"
>

and then did the following in my modal component:

  @ViewChild(TagInputComponent)
  tagInput: TagInputComponent;
  @ViewChild(TagInputDropdown)
  tagInputDropdown: TagInputDropdown;
  

  @HostListener('click')
  onClick()
  {
    if (!this.tagInput.isInputFocused() && this.tagInputDropdown.isVisible) {
      this.tagInputDropdown.hide();
    }
  }

Hacky but it works (I couldn't get a blur event fix sorted).

[Update] Here's a simpler update to the above in case you have multiple ngx-chip controls in the same modal/pop-up:

@ViewChildren(TagInputComponent)
private tagInputs: QueryList<TagInputComponent>;


 @HostListener('click')
  onClick()
  {
    this.tagInputs.forEach(tagInput => {
      if (!tagInput.isInputFocused() && tagInput.dropdown.isVisible) {
        tagInput.dropdown.hide();
      }
    })
  }

cesaric avatar Oct 25 '19 16:10 cesaric

@cesaric it worked here, thank you very much

jonathandsantiago avatar Jul 27 '20 18:07 jonathandsantiago

Same Problem here, but the question on StackOverflow has an answer now, which worked for my purposes, just add

<tag-input-dropdown
...
[keepOpen]="false">
</tag-input-dropdown>

Edit: not all usecases are solved by this

r-sw-eet avatar Oct 14 '20 12:10 r-sw-eet