Skip to content

Commit d55276a

Browse files
crisbetoalxhub
authored andcommitted
refactor(router): use transform to coerce input values (#50589)
Uses the new `transform` option for inputs instead of getters and setters to coerce the incoming values. PR Close #50589
1 parent 70c2e39 commit d55276a

File tree

4 files changed

+55
-51
lines changed

4 files changed

+55
-51
lines changed

goldens/public-api/router/index.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -793,24 +793,24 @@ class RouterLink implements OnChanges, OnDestroy {
793793
fragment?: string;
794794
href: string | null;
795795
// (undocumented)
796+
static ngAcceptInputType_preserveFragment: unknown;
797+
// (undocumented)
798+
static ngAcceptInputType_replaceUrl: unknown;
799+
// (undocumented)
800+
static ngAcceptInputType_skipLocationChange: unknown;
801+
// (undocumented)
796802
ngOnChanges(changes: SimpleChanges): void;
797803
// (undocumented)
798804
ngOnDestroy(): any;
799805
// (undocumented)
800806
onClick(button: number, ctrlKey: boolean, shiftKey: boolean, altKey: boolean, metaKey: boolean): boolean;
801-
set preserveFragment(preserveFragment: boolean | string | null | undefined);
802-
// (undocumented)
803-
get preserveFragment(): boolean;
807+
preserveFragment: boolean;
804808
queryParams?: Params | null;
805809
queryParamsHandling?: QueryParamsHandling | null;
806810
relativeTo?: ActivatedRoute | null;
807-
set replaceUrl(replaceUrl: boolean | string | null | undefined);
808-
// (undocumented)
809-
get replaceUrl(): boolean;
811+
replaceUrl: boolean;
810812
set routerLink(commands: any[] | string | null | undefined);
811-
set skipLocationChange(skipLocationChange: boolean | string | null | undefined);
812-
// (undocumented)
813-
get skipLocationChange(): boolean;
813+
skipLocationChange: boolean;
814814
state?: {
815815
[k: string]: any;
816816
};

packages/core/test/bundling/router/bundle.golden_symbols.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2030,6 +2030,9 @@
20302030
{
20312031
"name": "ɵEmptyOutletComponent"
20322032
},
2033+
{
2034+
"name": "ɵɵInputTransformsFeature"
2035+
},
20332036
{
20342037
"name": "ɵɵNgOnChangesFeature"
20352038
},

packages/router/src/directives/router_link.ts

Lines changed: 3 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -120,10 +120,6 @@ import {UrlTree} from '../url_tree';
120120
standalone: true,
121121
})
122122
export class RouterLink implements OnChanges, OnDestroy {
123-
private _preserveFragment = false;
124-
private _skipLocationChange = false;
125-
private _replaceUrl = false;
126-
127123
/**
128124
* Represents an `href` attribute value applied to a host element,
129125
* when a host element is `<a>`. For other tags, the value is `null`.
@@ -210,44 +206,23 @@ export class RouterLink implements OnChanges, OnDestroy {
210206
* @see {@link UrlCreationOptions#preserveFragment UrlCreationOptions#preserveFragment}
211207
* @see {@link Router#createUrlTree Router#createUrlTree}
212208
*/
213-
@Input()
214-
set preserveFragment(preserveFragment: boolean|string|null|undefined) {
215-
this._preserveFragment = booleanAttribute(preserveFragment);
216-
}
217-
218-
get preserveFragment(): boolean {
219-
return this._preserveFragment;
220-
}
209+
@Input({transform: booleanAttribute}) preserveFragment: boolean = false;
221210

222211
/**
223212
* Passed to {@link Router#navigateByUrl Router#navigateByUrl} as part of the
224213
* `NavigationBehaviorOptions`.
225214
* @see {@link NavigationBehaviorOptions#skipLocationChange NavigationBehaviorOptions#skipLocationChange}
226215
* @see {@link Router#navigateByUrl Router#navigateByUrl}
227216
*/
228-
@Input()
229-
set skipLocationChange(skipLocationChange: boolean|string|null|undefined) {
230-
this._skipLocationChange = booleanAttribute(skipLocationChange);
231-
}
232-
233-
get skipLocationChange(): boolean {
234-
return this._skipLocationChange;
235-
}
217+
@Input({transform: booleanAttribute}) skipLocationChange: boolean = false;
236218

237219
/**
238220
* Passed to {@link Router#navigateByUrl Router#navigateByUrl} as part of the
239221
* `NavigationBehaviorOptions`.
240222
* @see {@link NavigationBehaviorOptions#replaceUrl NavigationBehaviorOptions#replaceUrl}
241223
* @see {@link Router#navigateByUrl Router#navigateByUrl}
242224
*/
243-
@Input()
244-
set replaceUrl(replaceUrl: boolean|string|null|undefined) {
245-
this._replaceUrl = booleanAttribute(replaceUrl);
246-
}
247-
248-
get replaceUrl(): boolean {
249-
return this._replaceUrl;
250-
}
225+
@Input({transform: booleanAttribute}) replaceUrl: boolean = false;
251226

252227
/**
253228
* Modifies the tab index if there was not a tabindex attribute on the element during

packages/router/test/router_link_spec.ts

Lines changed: 40 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,20 @@ describe('RouterLink', () => {
3030
});
3131

3232
describe('on a non-anchor', () => {
33-
@Component({template: `<div [routerLink]="link"></div>`})
33+
@Component({
34+
template: `
35+
<div
36+
[routerLink]="link"
37+
[preserveFragment]="preserveFragment"
38+
[skipLocationChange]="skipLocationChange"
39+
[replaceUrl]="replaceUrl"></div>
40+
`
41+
})
3442
class LinkComponent {
3543
link: string|null|undefined = '/';
44+
preserveFragment: unknown;
45+
skipLocationChange: unknown;
46+
replaceUrl: unknown;
3647
}
3748
let fixture: ComponentFixture<LinkComponent>;
3849
let link: HTMLDivElement;
@@ -76,18 +87,20 @@ describe('RouterLink', () => {
7687
const dir = fixture.debugElement.query(By.directive(RouterLink)).injector.get(RouterLink);
7788

7889
for (const truthy of [true, '', 'true', 'anything']) {
79-
dir.preserveFragment = truthy;
80-
dir.skipLocationChange = truthy;
81-
dir.replaceUrl = truthy;
90+
fixture.componentInstance.preserveFragment = truthy;
91+
fixture.componentInstance.skipLocationChange = truthy;
92+
fixture.componentInstance.replaceUrl = truthy;
93+
fixture.detectChanges();
8294
expect(dir.preserveFragment).toBeTrue();
8395
expect(dir.skipLocationChange).toBeTrue();
8496
expect(dir.replaceUrl).toBeTrue();
8597
}
8698

8799
for (const falsy of [false, null, undefined, 'false']) {
88-
dir.preserveFragment = falsy;
89-
dir.skipLocationChange = falsy;
90-
dir.replaceUrl = falsy;
100+
fixture.componentInstance.preserveFragment = falsy;
101+
fixture.componentInstance.skipLocationChange = falsy;
102+
fixture.componentInstance.replaceUrl = falsy;
103+
fixture.detectChanges();
91104
expect(dir.preserveFragment).toBeFalse();
92105
expect(dir.skipLocationChange).toBeFalse();
93106
expect(dir.replaceUrl).toBeFalse();
@@ -97,9 +110,20 @@ describe('RouterLink', () => {
97110

98111
describe('on an anchor', () => {
99112
describe('RouterLink for elements with `href` attributes', () => {
100-
@Component({template: `<a [routerLink]="link"></a>`})
113+
@Component({
114+
template: `
115+
<a
116+
[routerLink]="link"
117+
[preserveFragment]="preserveFragment"
118+
[skipLocationChange]="skipLocationChange"
119+
[replaceUrl]="replaceUrl"></a>
120+
`
121+
})
101122
class LinkComponent {
102123
link: string|null|undefined = '/';
124+
preserveFragment: unknown;
125+
skipLocationChange: unknown;
126+
replaceUrl: unknown;
103127
}
104128
let fixture: ComponentFixture<LinkComponent>;
105129
let link: HTMLAnchorElement;
@@ -132,18 +156,20 @@ describe('RouterLink', () => {
132156
const dir = fixture.debugElement.query(By.directive(RouterLink)).injector.get(RouterLink);
133157

134158
for (const truthy of [true, '', 'true', 'anything']) {
135-
dir.preserveFragment = truthy;
136-
dir.skipLocationChange = truthy;
137-
dir.replaceUrl = truthy;
159+
fixture.componentInstance.preserveFragment = truthy;
160+
fixture.componentInstance.skipLocationChange = truthy;
161+
fixture.componentInstance.replaceUrl = truthy;
162+
fixture.detectChanges();
138163
expect(dir.preserveFragment).toBeTrue();
139164
expect(dir.skipLocationChange).toBeTrue();
140165
expect(dir.replaceUrl).toBeTrue();
141166
}
142167

143168
for (const falsy of [false, null, undefined, 'false']) {
144-
dir.preserveFragment = falsy;
145-
dir.skipLocationChange = falsy;
146-
dir.replaceUrl = falsy;
169+
fixture.componentInstance.preserveFragment = falsy;
170+
fixture.componentInstance.skipLocationChange = falsy;
171+
fixture.componentInstance.replaceUrl = falsy;
172+
fixture.detectChanges();
147173
expect(dir.preserveFragment).toBeFalse();
148174
expect(dir.skipLocationChange).toBeFalse();
149175
expect(dir.replaceUrl).toBeFalse();

0 commit comments

Comments
 (0)