core-api/routing: switch ref to prop and rename to routeReference

This commit is contained in:
Patrik Oldsberg
2020-09-20 13:21:55 +02:00
parent 0f3e9b29d4
commit 735c1277d1
3 changed files with 23 additions and 13 deletions
+12 -7
View File
@@ -14,28 +14,33 @@
* limitations under the License.
*/
import { ConcreteRoute, ref, resolveRoute, RouteRefConfig } from './types';
import {
ConcreteRoute,
routeReference,
ReferencedRoute,
resolveRoute,
RouteRefConfig,
} from './types';
import { generatePath } from 'react-router-dom';
type SubRouteConfig = {
path: string;
};
export class SubRouteRef<T extends { [name in string]: string }> {
export class SubRouteRef<T extends { [name in string]: string }>
implements ReferencedRoute {
constructor(
private readonly parent: ConcreteRoute,
private readonly config: SubRouteConfig,
) {}
[ref]() {
get [routeReference]() {
return this;
}
link(params: T): ConcreteRoute {
const selfRef = this as unknown;
return {
[ref]: () => selfRef,
[routeReference]: this,
[resolveRoute]: (path: string) => {
const ownPart = generatePath(this.config.path, params);
const parentPart = this.parent[resolveRoute](path);
@@ -67,7 +72,7 @@ export class AbsoluteRouteRef implements ConcreteRoute {
return new SubRouteRef<T>(this, config);
}
[ref]() {
get [routeReference]() {
return this;
}
@@ -14,10 +14,15 @@
* limitations under the License.
*/
import { ConcreteRoute, ref, resolveRoute, ReferencedRoute } from './types';
import {
ConcreteRoute,
routeReference,
resolveRoute,
ReferencedRoute,
} from './types';
const rootRoute: ConcreteRoute = {
[ref]() {
get [routeReference]() {
return this;
},
[resolveRoute]: () => '',
@@ -39,7 +44,7 @@ class Node {
let node = this as Node | undefined;
for (let i = 0; i < routes.length; i++) {
node = node?.children.get(routes[i][ref]());
node = node?.children.get(routes[i][routeReference]);
}
return node;
@@ -62,7 +67,7 @@ class Node {
}
const lastRoute = routes[routes.length - 1];
const lastRouteRef = lastRoute[ref]();
const lastRouteRef = lastRoute[routeReference];
if (parentNode.children.has(lastRouteRef)) {
return false;
}
+2 -2
View File
@@ -17,10 +17,10 @@
import { IconComponent } from '../icons';
export const resolveRoute = Symbol('resolve-route');
export const ref = Symbol('route-ref');
export const routeReference = Symbol('route-ref');
export type ReferencedRoute = {
[ref](): unknown;
[routeReference]: unknown;
};
export type ConcreteRoute = ReferencedRoute & {