1- import { Component , ElementRef , HostListener , Input , ViewChild , OnInit , OnChanges , SimpleChanges } from '@angular/core' ;
1+ import { Component , ElementRef , HostListener , Input , ViewChild , OnInit , OnChanges , OnDestroy , Optional , SimpleChanges } from '@angular/core' ;
22import { AbstractControl , FormControl , ReactiveFormsModule } from '@angular/forms' ;
33import { CommonModule } from '@angular/common' ;
4+ import { Subscription } from 'rxjs' ;
5+
6+ import { SelectCoordinatorService } from './select-coordinator.service' ;
47
58@Component ( {
69 selector : 'custom-select' ,
710 templateUrl : './custom-select.component.html' ,
811 styleUrl : './custom-select.component.scss' ,
912 imports : [ CommonModule , ReactiveFormsModule ]
1013} )
11- export class CustomSelectComponent implements OnInit , OnChanges {
14+ export class CustomSelectComponent implements OnInit , OnChanges , OnDestroy {
1215 @Input ( ) label = '' ;
1316 @Input ( ) placeholder = 'Selecciona una opción' ;
1417 @Input ( ) isMultiple = false ;
@@ -38,6 +41,8 @@ export class CustomSelectComponent implements OnInit, OnChanges {
3841 private _formControlItem ! : AbstractControl ;
3942 inputControl ! : FormControl ;
4043
44+ private openedSubscription ?: Subscription ;
45+
4146 @Input ( ) set formControlItem ( ctrl : AbstractControl ) {
4247 this . _formControlItem = ctrl ;
4348 this . inputControl = ctrl as FormControl ;
@@ -54,7 +59,7 @@ export class CustomSelectComponent implements OnInit, OnChanges {
5459 }
5560 get formControlItem ( ) { return this . _formControlItem ; }
5661
57- constructor ( private elementRef : ElementRef ) {
62+ constructor ( private elementRef : ElementRef , @ Optional ( ) private coordinator ?: SelectCoordinatorService ) {
5863 this . searchControl . valueChanges . subscribe ( val => {
5964 if ( this . isOpen ) {
6065 this . filterData ( val || '' ) ;
@@ -68,6 +73,17 @@ export class CustomSelectComponent implements OnInit, OnChanges {
6873 if ( this . inputControl ) {
6974 this . syncVisualInput ( this . inputControl . value ) ;
7075 }
76+
77+ // Si otro custom-select del mismo <sau-filter> se abre, cerramos este
78+ this . openedSubscription = this . coordinator ?. opened$ . subscribe ( source => {
79+ if ( source !== this && this . isOpen ) {
80+ this . closeDropdown ( ) ;
81+ }
82+ } ) ;
83+ }
84+
85+ ngOnDestroy ( ) {
86+ this . openedSubscription ?. unsubscribe ( ) ;
7187 }
7288
7389 // Captura cualquier desajuste de ciclo de vida de inputs nativos de Angular
@@ -91,6 +107,10 @@ export class CustomSelectComponent implements OnInit, OnChanges {
91107 return ;
92108 }
93109
110+ // stopPropagation() de arriba impide que el clickOut() de document de otros
111+ // custom-select se dispare, así que avisamos explícitamente para que se cierren
112+ this . coordinator ?. notifyOpened ( this ) ;
113+
94114 this . isOpen = true ;
95115 this . searchControl . setValue ( '' , { emitEvent : false } ) ;
96116 this . filteredOptions = [ ...this . options ] ;
0 commit comments