Skip to content

Commit ed6bb49

Browse files
committed
chore: improve router stories
1 parent 3810a82 commit ed6bb49

File tree

6 files changed

+82
-50
lines changed

6 files changed

+82
-50
lines changed

.npmrc

Lines changed: 0 additions & 8 deletions
This file was deleted.

package.json

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
{
2-
"name": "@mol-fe/mol-fe-react",
3-
"version": "1.5.0",
4-
"description": "MOL React.js utilities",
2+
"name": "libreact",
3+
"version": "0.0.1",
4+
"description": "React standard library",
55
"main": "lib/index.js",
66
"keywords": [
77
"react",
8+
"stdlib",
9+
"libreact",
810
"utilities",
911
"MailOnline"
1012
],
1113
"repository": {
1214
"type": "git",
13-
"url": "https://github.com/MailOnline/mol-fe-react.git"
15+
"url": "https://github.com/MailOnline/libreact.git"
1416
},
1517
"dependencies": {
1618
"react": "15",
@@ -66,7 +68,6 @@
6668
"test:client": "jest",
6769
"test:client:coverage": "jest --coverage",
6870
"test:watch": "jest --watch",
69-
"pub": "npm publish --registry http://sinopia.devhsk.mol.dmgt.net:4873",
7071
"storybook": "start-storybook -p 6007",
7172
"build-storybook": "build-storybook",
7273
"start": "npm run storybook"

src/route/__story__/StoryRouteExample2.tsx

Lines changed: 35 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -32,26 +32,43 @@ const About = () => (
3232
</div>
3333
);
3434

35-
const Topics = ({ match }) => (
36-
<div>
37-
<h2>Topics</h2>
38-
<ul>
39-
<li>
40-
Rendering with React
41-
</li>
42-
<li>
43-
Components
44-
</li>
45-
<li>
46-
Props v. State
47-
</li>
48-
</ul>
49-
</div>
50-
);
35+
const Topics = (props) => {
36+
const {match} = props;
37+
38+
console.log('PROPS', props);
39+
40+
return (
41+
<div>
42+
<h2>Topics</h2>
43+
<ul>
44+
<li>
45+
<Link to={`${match}/rendering`}>
46+
Rendering with React
47+
</Link>
48+
</li>
49+
<li>
50+
<Link to={`${match}/components`}>
51+
Components
52+
</Link>
53+
</li>
54+
<li>
55+
<Link to={`${match}/props-v-state`}>
56+
Props v. State
57+
</Link>
58+
</li>
59+
</ul>
60+
61+
<Route comp={Topic}/>
62+
<Route exact path={match.url} render={() => (
63+
<h3>Please select a topic.</h3>
64+
)}/>
65+
</div>
66+
);
67+
};
5168

52-
const Topic = ({ match }) => (
69+
const Topic = ({match}) => (
5370
<div>
54-
<h3>{match.params.topicId}</h3>
71+
<h3>{match}</h3>
5572
</div>
5673
);
5774

src/route/__story__/StoryRouteTruncateRoute.tsx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,24 @@ const StoryRouteTruncateRoute = () =>
66
<div>
77
<Router route='/api/users/123.json'>
88
<Route match='/api'>
9+
<div>
10+
API
911
<Route match='/users'>
12+
<div>
13+
USERS
1014
<Route match={/.*/}>{(result) =>
1115
<pre style={{fontFamily: 'monospace'}}>{JSON.stringify(result, null, 4)}</pre>
1216
}</Route>
17+
</div>
1318
</Route>
19+
</div>
1420
</Route>
1521
</Router>
1622

1723
<hr />
1824

1925
<pre style={{fontFamily: 'monospace'}}>{`
20-
<Router route='/api/users/132.json'>
26+
<Router route='/api/users/123.json'>
2127
<Route match='/api'>
2228
<Route match='/users'>
2329
<Route match={/.*/}>{(result) =>

src/route/go.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
export interface IGoParams {
2+
replace?: boolean;
3+
title?: string;
4+
state?: any;
5+
}
6+
7+
export type TGo = (url: string, params: IGoParams) => void;
8+
9+
export const go = (url, {replace, title = '', state}: IGoParams = {}) => {
10+
history[replace ? 'replaceState' : 'pushState'](state, title || '', url);
11+
};

src/route/index.ts

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,19 @@ export class Route extends Component<IRouteMatch, any> {
9999
};
100100
}
101101

102+
renderChildren (props) {
103+
const {comp, children} = this.props;
104+
105+
return comp ?
106+
h(comp, props) :
107+
typeof children === 'function' ?
108+
children(props) :
109+
children;
110+
}
111+
102112
render () {
103113
return h(Consumer, {name: ns(`route/${this.props.ns}`)}, ({route, onMatch, getMathces, parent}) => {
104-
const {children, match} = this.props;
114+
const {children, match, preserve} = this.props;
105115

106116
if (getMathces() <= this.props.cnt) {
107117
const matchResult = this.matcher()(route);
@@ -113,14 +123,19 @@ export class Route extends Component<IRouteMatch, any> {
113123
// Notify <RouteProvider> that we matched.
114124
onMatch(this, matchResult);
115125

116-
if (!this.props.preserve && length) {
117-
route = route.substr(length);
126+
let newRoute = route;
127+
128+
if (!preserve && length) {
129+
newRoute = newRoute.substr(length);
118130
}
119131

120-
return h(Router, {route, parent: matchResult},
121-
this.props.comp ?
122-
h(this.props.comp) :
123-
typeof children === 'function' ? children({matches, route, parent}) : children
132+
return h(Router, {route: newRoute, parent: matchResult},
133+
this.renderChildren({
134+
match: route.substr(0, length),
135+
matches,
136+
route: newRoute,
137+
parent
138+
})
124139
);
125140
}
126141
}
@@ -130,14 +145,4 @@ export class Route extends Component<IRouteMatch, any> {
130145
}
131146
}
132147

133-
export interface IGoParams {
134-
replace?: boolean;
135-
title?: string;
136-
state?: any;
137-
}
138-
139-
export type TGo = (url: string, params: IGoParams) => void;
140-
141-
export const go = (url, {replace, title = '', state}: IGoParams = {}) => {
142-
history[replace ? 'replaceState' : 'pushState'](state, title || '', url);
143-
};
148+
export * from './go';

0 commit comments

Comments
 (0)