boring-router
Advanced tools
Comparing version 0.1.2 to 0.1.3
@@ -5,2 +5,4 @@ export * from './schema'; | ||
export * from './route'; | ||
export * from './history'; | ||
export * from './link'; | ||
export * from './redirect'; |
@@ -8,3 +8,5 @@ "use strict"; | ||
tslib_1.__exportStar(require("./route"), exports); | ||
tslib_1.__exportStar(require("./history"), exports); | ||
tslib_1.__exportStar(require("./link"), exports); | ||
tslib_1.__exportStar(require("./redirect"), exports); | ||
//# sourceMappingURL=index.js.map |
@@ -1,7 +0,5 @@ | ||
import { History } from 'history'; | ||
import React, { Component, ReactNode } from 'react'; | ||
import { Component, ReactNode } from 'react'; | ||
import { EmptyObjectPatch } from 'tslang'; | ||
import { RouteMatch } from './route-match'; | ||
declare const HistoryProvider: React.ComponentType<React.ProviderProps<History>>; | ||
export interface LinkProps<TRouteMatch> { | ||
export interface LinkProps<TRouteMatch extends RouteMatch> { | ||
className?: string; | ||
@@ -15,4 +13,3 @@ to: TRouteMatch | string; | ||
render(): ReactNode; | ||
private onClick; | ||
private navigate; | ||
} | ||
export { HistoryProvider }; |
@@ -6,20 +6,16 @@ "use strict"; | ||
const react_1 = tslib_1.__importStar(require("react")); | ||
const history_1 = require("./history"); | ||
const route_match_1 = require("./route-match"); | ||
const { Provider: HistoryProvider, Consumer: HistoryConsumer } = react_1.createContext(undefined); | ||
exports.HistoryProvider = HistoryProvider; | ||
let Link = class Link extends react_1.Component { | ||
constructor() { | ||
super(...arguments); | ||
this.onClick = (history) => { | ||
let { to, params, preserveQuery } = this.props; | ||
if (to instanceof route_match_1.RouteMatch) { | ||
to = to.$ref(params, preserveQuery); | ||
} | ||
history.push(to); | ||
}; | ||
} | ||
render() { | ||
let { className, children } = this.props; | ||
return (react_1.default.createElement(HistoryConsumer, null, history => (react_1.default.createElement("a", { className: className, onClick: () => this.onClick(history), href: "javascript:;", children: children })))); | ||
return (react_1.default.createElement(history_1.HistoryConsumer, null, history => (react_1.default.createElement("a", { className: className, onClick: () => this.navigate(history), href: "javascript:;", children: children })))); | ||
} | ||
navigate(history) { | ||
let { to, params, preserveQuery } = this.props; | ||
if (to instanceof route_match_1.RouteMatch) { | ||
to = to.$ref(params, preserveQuery); | ||
} | ||
history.push(to); | ||
} | ||
}; | ||
@@ -26,0 +22,0 @@ Link = tslib_1.__decorate([ |
@@ -11,8 +11,7 @@ import { Dict } from 'tslang'; | ||
private _name; | ||
private _matchPattern; | ||
private _queryKeys; | ||
/** @internal */ | ||
private _matched; | ||
/** @internal */ | ||
private _exact; | ||
private _pathFragments; | ||
private _sourceQuery; | ||
/** @internal */ | ||
private _params; | ||
@@ -24,5 +23,4 @@ constructor(_name: string, { match, query }: RouteMatchOptions); | ||
$ref(params?: Partial<TParamDict>, preserveQuery?: boolean): string; | ||
private _match; | ||
static fragment: RegExp; | ||
static rest: RegExp; | ||
} |
@@ -84,2 +84,3 @@ "use strict"; | ||
} | ||
/** @internal */ | ||
_match(skipped, rest) { | ||
@@ -136,10 +137,13 @@ if (skipped || !rest) { | ||
mobx_1.observable | ||
/** @internal */ | ||
], RouteMatch.prototype, "_matched", void 0); | ||
tslib_1.__decorate([ | ||
mobx_1.observable | ||
/** @internal */ | ||
], RouteMatch.prototype, "_exact", void 0); | ||
tslib_1.__decorate([ | ||
mobx_1.observable | ||
/** @internal */ | ||
], RouteMatch.prototype, "_params", void 0); | ||
exports.RouteMatch = RouteMatch; | ||
//# sourceMappingURL=route-match.js.map |
@@ -25,9 +25,5 @@ import { History } from 'history'; | ||
export declare class Router { | ||
private _fragmentMatcher; | ||
private constructor(); | ||
private onLocationChange; | ||
private pushRouteChange; | ||
private buildRouteMatches; | ||
static create<TSchema extends RouteSchemaDict>(schema: TSchema, history: History, options?: RouterOptions): RouterType<TSchema>; | ||
} | ||
export {}; |
@@ -9,3 +9,4 @@ "use strict"; | ||
constructor(schema, history, { fragmentMatcher }) { | ||
this.onLocationChange = ({ pathname, search }) => { | ||
/** @internal */ | ||
this._onLocationChange = ({ pathname, search }) => { | ||
let searchParams = new URLSearchParams(search); | ||
@@ -16,11 +17,12 @@ let queryDict = Array.from(searchParams).reduce((dict, [key, value]) => { | ||
}, {}); | ||
this.pushRouteChange(this, false, pathname, {}, {}, queryDict); | ||
this._pushRouteChange(this, false, pathname, {}, {}, queryDict); | ||
}; | ||
this._fragmentMatcher = | ||
fragmentMatcher || DEFAULT_FRAGMENT_MATCHER_CALLBACK; | ||
this._children = this.buildRouteMatches(this, schema); | ||
history.listen(this.onLocationChange); | ||
this.onLocationChange(history.location); | ||
this._children = this._buildRouteMatches(this, schema); | ||
history.listen(this._onLocationChange); | ||
this._onLocationChange(history.location); | ||
} | ||
pushRouteChange(target, skipped, upperRest, upperPathFragmentDict, upperParamFragmentDict, sourceQueryDict) { | ||
/** @internal */ | ||
_pushRouteChange(target, skipped, upperRest, upperPathFragmentDict, upperParamFragmentDict, sourceQueryDict) { | ||
if (!target._children) { | ||
@@ -34,6 +36,7 @@ return; | ||
} | ||
this.pushRouteChange(routeMatch, !matched, rest, pathFragmentDict, paramFragmentDict, sourceQueryDict); | ||
this._pushRouteChange(routeMatch, !matched, rest, pathFragmentDict, paramFragmentDict, sourceQueryDict); | ||
} | ||
} | ||
buildRouteMatches(target, schemaDict) { | ||
/** @internal */ | ||
_buildRouteMatches(target, schemaDict) { | ||
let routeMatches = []; | ||
@@ -51,3 +54,3 @@ for (let [key, schema] of Object.entries(schemaDict)) { | ||
} | ||
routeMatch._children = this.buildRouteMatches(routeMatch, children); | ||
routeMatch._children = this._buildRouteMatches(routeMatch, children); | ||
} | ||
@@ -54,0 +57,0 @@ return routeMatches; |
{ | ||
"name": "boring-router", | ||
"version": "0.1.2", | ||
"version": "0.1.3", | ||
"description": "A light-weight, type-safe, yet reactive router service using MobX.", | ||
@@ -5,0 +5,0 @@ "repository": { |
@@ -152,2 +152,10 @@ [](https://www.npmjs.com/package/boring-router) | ||
- [Redirect](examples/redirect/main.tsx) | ||
Redirect on match. | ||
```tsx | ||
<Redirect match={router.notFound} to={router.account} params={{id: '123'}} /> | ||
``` | ||
- [Function as Child](examples/function-as-child/main.tsx) | ||
@@ -154,0 +162,0 @@ |
24820
21
440
209