Skip to content

Commit 8c1908e

Browse files
committed
fix bug to hidden a selector if other selector is open
1 parent dfbfe09 commit 8c1908e

3 files changed

Lines changed: 43 additions & 4 deletions

File tree

‎projects/some-angular-utils/filter/src/lib/components/custom-select/custom-select.component.ts‎

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
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';
22
import { AbstractControl, FormControl, ReactiveFormsModule } from '@angular/forms';
33
import { 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];
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { Injectable } from '@angular/core';
2+
import { Subject } from 'rxjs';
3+
4+
/**
5+
* Coordina la apertura de los distintos `custom-select` de un mismo `<sau-filter>`.
6+
* openDropdown() hace stopPropagation(), así que el click nunca llega a document y
7+
* el clickOut() de los demás selects nunca se dispara; sin este servicio, dos
8+
* selects abiertos a la vez se quedan ambos abiertos.
9+
*/
10+
@Injectable()
11+
export class SelectCoordinatorService {
12+
private readonly openedSource = new Subject<unknown>();
13+
readonly opened$ = this.openedSource.asObservable();
14+
15+
notifyOpened(source: unknown): void {
16+
this.openedSource.next(source);
17+
}
18+
}

‎projects/some-angular-utils/filter/src/lib/filter.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { CommonModule, DatePipe } from '@angular/common';
44

55
import { CustomInputComponent } from './components/custom-input/custom-input.component';
66
import { CustomSelectComponent } from './components/custom-select/custom-select.component';
7+
import { SelectCoordinatorService } from './components/custom-select/select-coordinator.service';
78
import { SAUDateRangePickerModule } from '@some-angular-utils/date-range-picker';
89
import { FilterButtonComponent } from './components/filter-button/filter-button.component';
910

@@ -12,7 +13,7 @@ import { FilterButtonComponent } from './components/filter-button/filter-button.
1213
templateUrl: './filter.html',
1314
styleUrls: ['./filter.scss'],
1415
encapsulation: ViewEncapsulation.None,
15-
providers: [DatePipe],
16+
providers: [DatePipe, SelectCoordinatorService],
1617
imports: [
1718
CommonModule,
1819
ReactiveFormsModule,

0 commit comments

Comments
 (0)