Skip to content

Commit 564a8d5

Browse files
mvdluitalxhub
authored andcommitted
docs(docs-infra): Add query as an url query param to the api reference (angular#57062)
add an url query param to perform a search on api references directly via the url PR Close angular#57062
1 parent bfda774 commit 564a8d5

File tree

4 files changed

+61
-4
lines changed

4 files changed

+61
-4
lines changed

adev/src/app/app-scroller.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,13 @@ export class AppScroller {
4141
this._lastScrollEvent = e;
4242
}),
4343
filter(() => !this.disableScrolling),
44+
filter(() => {
45+
const info = this.router.lastSuccessfulNavigation?.extras.info as Record<
46+
'disableScrolling',
47+
boolean
48+
>;
49+
return !info?.['disableScrolling'];
50+
}),
4451
switchMap((e) => {
4552
return firstValueFrom(
4653
this.appRef.isStable.pipe(

adev/src/app/features/references/api-items-section/api-items-section.component.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ <h3 [attr.id]="group.id">
33
@if (group.isFeatured) {
44
<docs-icon aria-hidden>star</docs-icon>
55
}
6-
<a routerLink="/api" [fragment]="group.id" class="adev-api-anchor" tabindex="-1">{{ group.title }}</a>
6+
<a routerLink="/api" [fragment]="group.id" queryParamsHandling="preserve" class="adev-api-anchor" tabindex="-1">{{ group.title }}</a>
77
</h3>
88
</header>
99

adev/src/app/features/references/api-reference-list/api-reference-list.component.spec.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {signal} from '@angular/core';
1414
import {ApiItemType} from '../interfaces/api-item-type';
1515
import {RouterTestingHarness} from '@angular/router/testing';
1616
import {provideRouter} from '@angular/router';
17+
import {Location} from '@angular/common';
1718

1819
describe('ApiReferenceList', () => {
1920
let component: ApiReferenceList;
@@ -117,4 +118,30 @@ describe('ApiReferenceList', () => {
117118
harness.navigateByUrl(`/api`);
118119
expect(component.type()).toBe(ALL_STATUSES_KEY);
119120
});
121+
122+
it('should set the value of the queryParam equal to the query value', async () => {
123+
const location = TestBed.inject(Location);
124+
component.query.set('item1');
125+
await fixture.whenStable();
126+
expect(location.path()).toBe(`?query=item1&type=All`);
127+
});
128+
129+
it('should keep the values of existing queryParams and set new queryParam equal to the type', async () => {
130+
const location = TestBed.inject(Location);
131+
132+
component.query.set('item1');
133+
await fixture.whenStable();
134+
expect(location.path()).toBe(`?query=item1&type=All`);
135+
136+
component.filterByItemType(ApiItemType.BLOCK);
137+
await fixture.whenStable();
138+
expect(location.path()).toBe(`?query=item1&type=${ApiItemType.BLOCK}`);
139+
});
140+
141+
it('should display all items when query and type are undefined', async () => {
142+
component.query.set(undefined);
143+
component.type.set(undefined);
144+
await fixture.whenStable();
145+
expect(component.filteredGroups()![0].items).toEqual([fakeItem1, fakeItem2]);
146+
});
120147
});

adev/src/app/features/references/api-reference-list/api-reference-list.component.ts

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import ApiItemsSection from '../api-items-section/api-items-section.component';
2323
import {FormsModule} from '@angular/forms';
2424
import {SlideToggle, TextField} from '@angular/docs';
2525
import {NgFor, NgIf} from '@angular/common';
26+
import {Params, Router} from '@angular/router';
2627
import {ApiItemType} from '../interfaces/api-item-type';
2728
import {ApiReferenceManager} from './api-reference-manager.service';
2829
import ApiItemLabel from '../api-item-label/api-item-label.component';
@@ -50,6 +51,7 @@ export const ALL_STATUSES_KEY = 'All';
5051
})
5152
export default class ApiReferenceList {
5253
private readonly apiReferenceManager = inject(ApiReferenceManager);
54+
private readonly router = inject(Router);
5355
filterInput = viewChild.required(TextField, {read: ElementRef});
5456
private readonly injector = inject(EnvironmentInjector);
5557

@@ -71,9 +73,28 @@ export default class ApiReferenceList {
7173
{injector: this.injector},
7274
);
7375
});
76+
77+
effect(
78+
() => {
79+
const params: Params = {
80+
'query': this.query() ? this.query() : null,
81+
'type': this.type() ? this.type() : null,
82+
};
83+
84+
this.router.navigate([], {
85+
queryParams: params,
86+
replaceUrl: true,
87+
preserveFragment: true,
88+
info: {
89+
disableScrolling: true,
90+
},
91+
});
92+
},
93+
{allowSignalWrites: true},
94+
);
7495
}
7596

76-
query = signal('');
97+
query = model<string | undefined>('');
7798
includeDeprecated = signal(false);
7899

79100
type = model<string | undefined>(ALL_STATUSES_KEY);
@@ -87,8 +108,10 @@ export default class ApiReferenceList {
87108
id: group.id,
88109
items: group.items.filter((apiItem) => {
89110
return (
90-
(this.query()
91-
? apiItem.title.toLocaleLowerCase().includes(this.query().toLocaleLowerCase())
111+
(this.query() !== undefined
112+
? apiItem.title
113+
.toLocaleLowerCase()
114+
.includes((this.query() as string).toLocaleLowerCase())
92115
: true) &&
93116
(this.includeDeprecated() ? true : apiItem.isDeprecated === this.includeDeprecated()) &&
94117
(this.type() === undefined ||

0 commit comments

Comments
 (0)