Skip to content

Commit 78842a9

Browse files
Fix: Close advanced filter slideout when selecting an item (#1846)
1 parent f408b61 commit 78842a9

File tree

3 files changed

+132
-22
lines changed

3 files changed

+132
-22
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
import { Component, DebugElement, Injector, forwardRef } from "@angular/core";
2+
import { ComponentFixture, TestBed } from "@angular/core/testing";
3+
import { FormsModule, ReactiveFormsModule } from "@angular/forms";
4+
import { By } from "@angular/platform-browser";
5+
import { RouterTestingModule } from "@angular/router/testing";
6+
import { I18nTestingModule } from "@batch-flask/core/testing";
7+
import { click } from "test/utils/helpers";
8+
import { ListBaseComponent } from "../abstract-list";
9+
import { ButtonsModule } from "../buttons";
10+
import { ScrollableModule } from "../scrollable";
11+
import { SplitPaneModule } from "../split-pane";
12+
import { BrowseLayoutAdvancedFilterDirective } from "./browse-layout-advanced-filter";
13+
import { BrowseLayoutListDirective } from "./browse-layout-list";
14+
import { BrowseLayoutComponent } from "./browse-layout.component";
15+
import { ToggleFilterButtonComponent } from "./toggle-filter-button";
16+
17+
@Component({
18+
selector: "bl-fake-list",
19+
template: `
20+
`,
21+
providers: [{
22+
provide: ListBaseComponent,
23+
useExisting: forwardRef(() => FakeListComponent),
24+
}],
25+
})
26+
class FakeListComponent extends ListBaseComponent {
27+
constructor(injector: Injector) {
28+
super(injector);
29+
}
30+
}
31+
32+
@Component({
33+
template: `
34+
<bl-browse-layout>
35+
<div blBrowseLayoutTitle>
36+
Jobs
37+
</div>
38+
<div blBrowseLayoutButtons>
39+
</div>
40+
<bl-fake-list blBrowseLayoutList></bl-fake-list>
41+
<div blBrowseLayoutAdvancedFilter >
42+
Advanced filter contents
43+
</div>
44+
</bl-browse-layout>
45+
`,
46+
})
47+
class TestComponent {
48+
}
49+
50+
describe("BrowseLayoutComponent", () => {
51+
let fixture: ComponentFixture<TestComponent>;
52+
let fakeList: FakeListComponent;
53+
let de: DebugElement;
54+
55+
let advFilterEl: DebugElement;
56+
57+
beforeEach(() => {
58+
TestBed.configureTestingModule({
59+
imports: [
60+
ScrollableModule,
61+
ButtonsModule,
62+
SplitPaneModule,
63+
FormsModule,
64+
ReactiveFormsModule,
65+
RouterTestingModule,
66+
I18nTestingModule,
67+
],
68+
declarations: [
69+
BrowseLayoutComponent,
70+
TestComponent,
71+
FakeListComponent,
72+
BrowseLayoutComponent,
73+
BrowseLayoutListDirective,
74+
ToggleFilterButtonComponent,
75+
BrowseLayoutAdvancedFilterDirective,
76+
],
77+
});
78+
fixture = TestBed.createComponent(TestComponent);
79+
de = fixture.debugElement.query(By.css("bl-browse-layout"));
80+
fixture.detectChanges();
81+
82+
fakeList = de.query(By.directive(FakeListComponent)).componentInstance;
83+
advFilterEl = de.query(By.css(".advanced-filter-content"));
84+
});
85+
86+
describe("When the advanced filter is opened", () => {
87+
beforeEach(async () => {
88+
click(de.query(By.css("bl-toggle-filter-button bl-clickable")));
89+
fixture.detectChanges();
90+
await fixture.whenStable();
91+
});
92+
93+
it("has the advanced filter open", () => {
94+
expect(advFilterEl).toBeVisible();
95+
});
96+
97+
it("close the advanced filters when selecting an item", () => {
98+
fakeList.activeItemChange.emit("foo");
99+
fixture.detectChanges();
100+
expect(advFilterEl).not.toBeVisible();
101+
});
102+
});
103+
});

src/@batch-flask/ui/browse-layout/browse-layout.component.ts

+27-21
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
1+
import { LiveAnnouncer } from "@angular/cdk/a11y";
12
import {
2-
AfterContentInit, ChangeDetectionStrategy,
3-
ChangeDetectorRef, Component, ContentChild,
4-
ElementRef, Input, OnChanges, OnInit, ViewChild,
3+
AfterContentInit, AfterViewInit,
4+
ChangeDetectionStrategy, ChangeDetectorRef, Component,
5+
ContentChild, ElementRef, Input, OnChanges, OnDestroy, OnInit, ViewChild,
56
} from "@angular/core";
67
import { FormControl } from "@angular/forms";
78
import { ActivatedRoute } from "@angular/router";
89
import { Filter, FilterBuilder, I18nService, autobind } from "@batch-flask/core";
910
import { KeyCode } from "@batch-flask/core/keys";
1011
import { ListSelection } from "@batch-flask/core/list";
1112
import { SanitizedError } from "@batch-flask/utils";
12-
import { Subscription } from "rxjs";
13-
import { debounceTime, distinctUntilChanged } from "rxjs/operators";
13+
import { Subject } from "rxjs";
14+
import { debounceTime, distinctUntilChanged, takeUntil } from "rxjs/operators";
1415
import { SplitPaneComponent, SplitPaneConfig } from "../split-pane";
1516
import { BrowseLayoutAdvancedFilterDirective } from "./browse-layout-advanced-filter";
1617
import { BrowseLayoutListDirective } from "./browse-layout-list";
1718

18-
import { LiveAnnouncer } from "@angular/cdk/a11y";
1919
import "./browse-layout.scss";
2020

2121
export interface BrowseLayoutConfig {
@@ -46,7 +46,7 @@ let idCounter = 0;
4646
templateUrl: "browse-layout.html",
4747
changeDetection: ChangeDetectionStrategy.OnPush,
4848
})
49-
export class BrowseLayoutComponent implements OnInit, AfterContentInit, OnChanges {
49+
export class BrowseLayoutComponent implements OnInit, AfterViewInit, AfterContentInit, OnChanges, OnDestroy {
5050
@Input() public id = `bl-browse-layout-${idCounter++}`;
5151
/**
5252
* Field for the quicksearch.
@@ -94,20 +94,14 @@ export class BrowseLayoutComponent implements OnInit, AfterContentInit, OnChange
9494

9595
private _activeItemKey: string = null;
9696
private _config: BrowseLayoutConfig = defaultConfig;
97-
private _selectionChangeSub: Subscription;
97+
private _destroy = new Subject();
9898

9999
constructor(
100-
activeRoute: ActivatedRoute,
100+
private activeRoute: ActivatedRoute,
101101
private i18n: I18nService,
102102
private liveAnouncer: LiveAnnouncer,
103103
private changeDetector: ChangeDetectorRef) {
104104

105-
activeRoute.queryParams.subscribe((params: any) => {
106-
if (params.filter) {
107-
this.toggleFilter(true);
108-
}
109-
});
110-
111105
activeRoute.url.subscribe((url) => {
112106
const child = activeRoute.snapshot.firstChild;
113107
if (child) {
@@ -136,6 +130,13 @@ export class BrowseLayoutComponent implements OnInit, AfterContentInit, OnChange
136130
});
137131
}
138132

133+
public ngAfterViewInit() {
134+
this.activeRoute.queryParams.pipe(takeUntil(this._destroy)).subscribe((params: any) => {
135+
if (params.filter) {
136+
this.toggleFilter(true);
137+
}
138+
});
139+
}
139140
public ngOnChanges(changes) {
140141
if (changes.config) {
141142
this._updateFilter();
@@ -153,16 +154,21 @@ export class BrowseLayoutComponent implements OnInit, AfterContentInit, OnChange
153154
this.deleteSelectionIsEnabled = Boolean(component.deleteSelection);
154155
this.refreshEnabled = Boolean(component.refresh);
155156
this.changeDetector.markForCheck();
156-
this._selectionChangeSub = this.listDirective.component.selectionChange.subscribe((x) => {
157+
this.listDirective.component.selectionChange.pipe(takeUntil(this._destroy)).subscribe((x) => {
157158
this.selection = x;
158159
this.changeDetector.markForCheck();
159160
});
161+
162+
this.listDirective.component.activeItemChange.pipe(takeUntil(this._destroy)).subscribe((x) => {
163+
if (this.showAdvancedFilter) {
164+
this.toggleFilter(false);
165+
}
166+
});
160167
}
161168

162-
public _ngOnDestroy() {
163-
if (this._selectionChangeSub) {
164-
this._selectionChangeSub.unsubscribe();
165-
}
169+
public ngOnDestroy() {
170+
this._destroy.next();
171+
this._destroy.complete();
166172
}
167173

168174
/**
@@ -181,7 +187,7 @@ export class BrowseLayoutComponent implements OnInit, AfterContentInit, OnChange
181187

182188
public toggleFilter(value?: boolean) {
183189
const newValue = (value === undefined ? !this.showAdvancedFilter : value);
184-
if (newValue === this.showAdvancedFilter) {return; }
190+
if (newValue === this.showAdvancedFilter) { return; }
185191
this.showAdvancedFilter = newValue;
186192

187193
if (this.listDirective) {

src/@batch-flask/ui/browse-layout/browse-layout.module.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ const publicComponents = [
1717
BrowseLayoutComponent,
1818
BrowseLayoutListDirective,
1919
ToggleFilterButtonComponent,
20-
BrowseLayoutAdvancedFilterDirective];
20+
BrowseLayoutAdvancedFilterDirective,
21+
];
2122

2223
@NgModule({
2324
imports: [

0 commit comments

Comments
 (0)