I've a dropdown component which is inside a custom parent component timeselector. I've tried taking help from:
- How to avoid dropdown menu to close menu items on clicking inside
- Avoid dropdown menu close on click inside
and rest of the solutions are using some library or the other. But here I'm creating this component from scratch without bootstrap, primeng or material (* because my team lead doesn't want to use them for some reasons, I didn't argue). Here is the screenshot:
The widget pops up on click event. As you can see I've a dropdown with four options, but the moment I click on any option, the entire widget simply collapses. However that option gets selected anyway. But the widget collapses back. See the screenshot:
Here is my code. Please correct me where I am wrong.
timeselector.component.html
<div class="time-selector">
  <p-overlayPanel class="my-overlay" #op>
    <div class="inner-panel">
      <h3>Time selection</h3>
      <br>
      <select [(ngModel)]="mode" name="source" placeholder={{selectedSource}}>
        <option *ngFor="let source of sources" [value]="source">
          <h4>{{source}}</h4>
        </option>
      </select>
    </div>
  </p-overlayPanel>
</div>
<input class="input-field" (click)="op.toggle($event)">
timeselector.component.css
.inner-panel {
  width: 360px;
  padding: 10px 20px 20px 20px;
}
.my-overlay {
  position: fixed;
  background-color: white;
  box-shadow: 1px 1px 15px #888888;
  margin-top: 35px;
  right: 0.5%;
}
select {
    margin-top: 10px;
    width: 100% !important;
    z-index: 1;
}
timeselector.component.ts
import { Component, HostListener } from '@angular/core';
@Component({
    ...
})
export class TimeselectorComponent {
  public selectedSource = 'Calendar year';
  public sources  = ['Calendar year', 'Year-to-date', 'Rolling 12 months', 'Current quarter'];
   @HostListener('document:click') 
    ClickedAway() {
        console.log("inside hostlistener");
    }
}
I also read ElementRef in Angular. I'm using HostListener but I'm nor sure how to use it to serve my purpose. Please help me.


