Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add callback to be triggered right before transition #35

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@bhoos/react-kit",
"version": "2.0.12",
"version": "2.0.14",
"description": "Bhoos React Library",
"main": "dist/index.js",
"module": "es6/index.js",
Expand Down
9 changes: 9 additions & 0 deletions src/router/Router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export class Router {
private childListeners: Array<Router> = [];
private ConfirmTransitions: Array<ConfirmTransition> = [];
private currentTransition: Array<ConfirmTransition> | null = null;
private onRouteTransition: (route: string) => Promise<void>;
/**
* Create a router for handling route changes, with a
* custom url mapper that is used by `setUrl`, for mapping
Expand All @@ -48,6 +49,10 @@ export class Router {
if (initialUrl) this.setUrl(initialUrl);
};

setOnRouteTransition = (transitionHandler: (route: string) => Promise<void>) => {
this.onRouteTransition = transitionHandler;
}

private getRecentUrl(): string | null {
if (this.recentUrl) return this.recentUrl;
if (this.parentRouter) return this.parentRouter.getRecentUrl();
Expand Down Expand Up @@ -210,6 +215,10 @@ export class Router {
};
private async updateUrl(url: string, op: (route: Route) => void): Promise<Route> {
let newRoute: Route;
if (this.onRouteTransition) {
await this.onRouteTransition(url);
}

if (this.mapUrl) {
newRoute = this.mapUrl(url || '', this.setChildUrl);
op(newRoute);
Expand Down