Skip to content

Commit eac23af

Browse files
committed
fix(router): read focusReset from the navigation extras
With the Navigation API integration, the browser resets focus after each navigation, so a field that writes its value to the URL as the user types loses focus after the first key. The `focusReset` option that fixes this is new public API on main (angular#71595), so it can't come to the patch branch. Read `focusReset` from the navigation extras without adding it to `NavigationBehaviorOptions`, so angular.dev can pass it in the API reference filter.
1 parent 8087f76 commit eac23af

2 files changed

Lines changed: 63 additions & 0 deletions

File tree

‎packages/router/src/statemanager/navigation_state_manager.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -458,6 +458,12 @@ export class NavigationStateManager extends StateManager {
458458
: (this.currentNavigation.routerTransition?.extras.scroll ?? 'after-transition');
459459
const interceptOptions: NavigationInterceptOptions = {
460460
scroll,
461+
// `focusReset` isn't part of the public `NavigationBehaviorOptions` on this branch. It's read
462+
// here so angular.dev can keep focus in the API reference filter.
463+
focusReset: (
464+
this.currentNavigation.routerTransition?.extras as
465+
{focusReset?: NavigationFocusReset} | undefined
466+
)?.focusReset,
461467
};
462468

463469
const {

‎packages/router/test/with_platform_navigation.spec.ts‎

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
DefaultUrlSerializer,
1717
UrlTree,
1818
Params,
19+
NavigationExtras,
1920
} from '../src';
2021
import {withExperimentalPlatformNavigation, withRouterConfig} from '../src/provide_router';
2122
import {withBody, useAutoTick, timeout} from '@angular/private/testing';
@@ -510,4 +511,60 @@ if (typeof window !== 'undefined' && 'navigation' in window && !isFirefox()) {
510511
}),
511512
);
512513
});
514+
515+
describe('focusReset with real platform navigation', () => {
516+
const navigation = window.navigation as Navigation;
517+
518+
async function setup() {
519+
TestBed.configureTestingModule({
520+
providers: [
521+
provideRouter([{path: '**', children: []}], withExperimentalPlatformNavigation()),
522+
{provide: PlatformLocation, useClass: BrowserPlatformLocation},
523+
{provide: PlatformNavigation, useFactory: () => navigation},
524+
],
525+
});
526+
const router = TestBed.inject(Router);
527+
router.initialNavigation();
528+
await timeout();
529+
return router;
530+
}
531+
532+
it(
533+
'resets focus by default',
534+
withBody('<input>', async () => {
535+
const router = await setup();
536+
const input = document.querySelector('input')!;
537+
input.focus();
538+
expect(document.activeElement).toBe(input);
539+
540+
// Each test uses its own URL: these tests share the page URL, and navigating to the
541+
// current URL again would be skipped by the router.
542+
await router.navigate(['/search'], {queryParams: {q: 'default'}});
543+
// The browser resets focus when it finishes the navigate event, after the next render.
544+
await navigation.transition?.finished;
545+
546+
expect(router.url).toBe('/search?q=default');
547+
expect(document.activeElement).toBe(document.body);
548+
}),
549+
);
550+
551+
it(
552+
"keeps focus with focusReset: 'manual' in the extras",
553+
withBody('<input>', async () => {
554+
const router = await setup();
555+
const input = document.querySelector('input')!;
556+
input.focus();
557+
expect(document.activeElement).toBe(input);
558+
559+
await router.navigate(['/search'], {
560+
queryParams: {q: 'manual'},
561+
focusReset: 'manual',
562+
} as NavigationExtras);
563+
await navigation.transition?.finished;
564+
565+
expect(router.url).toBe('/search?q=manual');
566+
expect(document.activeElement).toBe(input);
567+
}),
568+
);
569+
});
513570
}

0 commit comments

Comments
 (0)