@pinnacle0/react-stack-router
Advanced tools
Comparing version
@@ -42,3 +42,3 @@ import { Route } from "../route"; | ||
private getTopScreen; | ||
private matchRoute; | ||
private matchedRoute; | ||
private createScreen; | ||
@@ -45,0 +45,0 @@ private pushScreen; |
@@ -29,3 +29,4 @@ import { Action } from "history"; | ||
const defaultState = this.stackHistory.location.state; | ||
const matched = this.matchRoute(pathname); | ||
const matched = this.matchedRoute(pathname); | ||
invariant(matched, `StackRouter.initialize(): None of the route match current pathname:${pathname}. Please make sure you have defined fallback route using "**"`); | ||
const stack = [{ pathname, hash, search }]; | ||
@@ -69,4 +70,4 @@ const segments = ["/", ...matched.matchedSegments]; | ||
} | ||
if (!this.matchRoute(to)) | ||
return; | ||
const pathname = typeof to === "string" ? to : to.pathname; | ||
invariant(this.matchedRoute(to), `StackRouter.push(): None of the route match current pathname:${pathname}. Please make sure you have defined fallback route using "**"`); | ||
this.pushOption.value = option ?? null; | ||
@@ -98,4 +99,4 @@ const wait = new Promise(resolve => (this.resolve.value = resolve)); | ||
} | ||
if (!this.matchRoute(to)) | ||
return; | ||
const pathname = typeof to === "string" ? to : to.pathname; | ||
invariant(this.matchedRoute(to), `StackRouter.replace(): None of the route match current pathname:${pathname}. Please make sure you have defined fallback route using "**"`); | ||
this.stackHistory.replace(to, { $key: this.stackHistory.location.state?.$key ?? createKey(), ...(option?.state ?? {}) }); | ||
@@ -128,10 +129,10 @@ } | ||
} | ||
matchRoute(to) { | ||
matchedRoute(to) { | ||
const pathname = typeof to === "string" ? to : to.pathname; | ||
const matched = this.route.lookup(pathname ?? window.location.pathname); | ||
invariant(matched, `None of the route match current pathname:${pathname}. Please make sure you have defined fallback route using "**"`); | ||
return matched; | ||
} | ||
createScreen(location, transitionOption) { | ||
const matched = this.matchRoute(location.pathname); | ||
const matched = this.matchedRoute(location.pathname); | ||
invariant(matched, `StackRouter.createScreen(): None of the route match current pathname:${location.pathname}. Please make sure you have defined fallback route using "**"`); | ||
return new Screen({ | ||
@@ -138,0 +139,0 @@ content: matched.payload.component, |
{ | ||
"name": "@pinnacle0/react-stack-router", | ||
"version": "0.2.0", | ||
"version": "0.2.1", | ||
"author": "Pinnacle", | ||
@@ -29,8 +29,8 @@ "license": "MIT", | ||
"dependencies": { | ||
"classnames": "2.3.2", | ||
"classnames": "2.5.1", | ||
"history": "5.3.0" | ||
}, | ||
"devDependencies": { | ||
"@types/react": "18.2.39", | ||
"@types/react-dom": "18.2.14", | ||
"@types/react": "18.2.48", | ||
"@types/react-dom": "18.2.18", | ||
"copyfiles": "2.4.1", | ||
@@ -37,0 +37,0 @@ "react": "18.2.0", |
@@ -63,3 +63,4 @@ import {Action} from "history"; | ||
const defaultState = this.stackHistory.location.state; | ||
const matched = this.matchRoute(pathname); | ||
const matched = this.matchedRoute(pathname); | ||
invariant(matched, `StackRouter.initialize(): None of the route match current pathname:${pathname}. Please make sure you have defined fallback route using "**"`); | ||
@@ -113,3 +114,4 @@ const stack: To[] = [{pathname, hash, search}]; | ||
if (!this.matchRoute(to)) return; | ||
const pathname = typeof to === "string" ? to : to.pathname; | ||
invariant(this.matchedRoute(to), `StackRouter.push(): None of the route match current pathname:${pathname}. Please make sure you have defined fallback route using "**"`); | ||
@@ -147,3 +149,4 @@ this.pushOption.value = option ?? null; | ||
} | ||
if (!this.matchRoute(to)) return; | ||
const pathname = typeof to === "string" ? to : to.pathname; | ||
invariant(this.matchedRoute(to), `StackRouter.replace(): None of the route match current pathname:${pathname}. Please make sure you have defined fallback route using "**"`); | ||
this.stackHistory.replace(to, {$key: (this.stackHistory.location.state as any)?.$key ?? createKey(), ...(option?.state ?? {})}); | ||
@@ -183,7 +186,5 @@ } | ||
private matchRoute(to: To): Match<StackRoutePayload> { | ||
private matchedRoute(to: To): Match<StackRoutePayload> | null { | ||
const pathname = typeof to === "string" ? to : to.pathname; | ||
const matched = this.route.lookup(pathname ?? window.location.pathname); | ||
invariant(matched, `None of the route match current pathname:${pathname}. Please make sure you have defined fallback route using "**"`); | ||
return matched; | ||
@@ -193,3 +194,4 @@ } | ||
private createScreen(location: Location, transitionOption: Required<TransitionOption>): Screen { | ||
const matched = this.matchRoute(location.pathname); | ||
const matched = this.matchedRoute(location.pathname); | ||
invariant(matched, `StackRouter.createScreen(): None of the route match current pathname:${location.pathname}. Please make sure you have defined fallback route using "**"`); | ||
return new Screen({ | ||
@@ -196,0 +198,0 @@ content: matched.payload.component, |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
166963
0.93%2563
0.16%2
Infinity%+ Added
- Removed
Updated