-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.tsx
199 lines (189 loc) · 5.02 KB
/
main.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
import {RouteMatch, Router} from 'boring-router';
import {observer} from 'mobx-react';
import React, {Component, ReactNode} from 'react';
import ReactDOM from 'react-dom';
import {BrowserHistory, Link, Route} from '../../bld/library';
const history = new BrowserHistory();
const router = new Router<'popup' | 'sidebar'>(history);
const primaryRoute = router.$route({
default: {
$match: '',
},
news: true,
about: {
$exact: true,
$children: {
test: true,
},
},
contact: true,
notFound: {
$match: RouteMatch.rest,
},
});
const popupRoute = router.$route('popup', {
account: {
$exact: true,
$children: {
login: true,
register: true,
},
},
profile: true,
});
const sidebarRoute = router.$route('sidebar', {
cart: true,
});
primaryRoute.about.$parallel({matches: [sidebarRoute.cart]});
primaryRoute.contact.$parallel({groups: ['popup']});
primaryRoute.about.$beforeEnter(match => {
if (match.$exact) {
match.test.$push();
}
});
@observer
export class App extends Component {
render(): ReactNode {
return (
<>
<h1>Boring Router</h1>
<Route match={primaryRoute.default}>
<p>Home page</p>
<div>
<Link to={popupRoute.account} toggle>
Account
</Link>
{' | '}
<Link to={popupRoute.profile} toggle>
Profile
</Link>
</div>
<div>
<Link to={sidebarRoute.cart}>Cart</Link>
</div>
<div>
<Link to={primaryRoute.news}>News</Link>
</div>
<div>
<Link to={primaryRoute.about}>About (With Cart)</Link>
</div>
<div>
<Link to={primaryRoute.contact}>Contact (With 'popup' Group)</Link>
</div>
<div>
<Link to={primaryRoute.notFound} params={{notFound: 'boring'}}>
Boring
</Link>
</div>
</Route>
<Route match={popupRoute.account}>
<div
style={{
position: 'fixed',
right: 0,
top: 0,
width: 300,
height: 200,
}}
>
<p>
Account popup{' '}
<a
href="javascript:void(0);"
onClick={() => {
primaryRoute.$replace({}, {leaves: ['popup']});
}}
>
x
</a>
</p>
<Route match={popupRoute.account} exact={true}>
<p>
<Link to={popupRoute.account.login}>Login</Link>
<br />
<Link to={popupRoute.account.register}>Register</Link>
</p>
<p>
<Link to={popupRoute.profile}>Profile</Link>
</p>
</Route>
<Route match={popupRoute.account.login}>
<p>- Login</p>
<Link to={popupRoute.account}>Back</Link>
</Route>
<Route match={popupRoute.account.register}>
<p>- Register</p>
<Link to={popupRoute.account}>Back</Link>
</Route>
</div>
</Route>
<Route match={popupRoute.profile}>
<div
style={{
position: 'fixed',
right: 0,
top: 0,
width: 300,
height: 200,
}}
>
<p>
Profile popup{' '}
<a
href="javascript:void(0);"
onClick={() => {
primaryRoute.$replace({}, {leaves: ['popup']});
}}
>
x
</a>
</p>
<p>
<Link to={popupRoute.account}>Account</Link>
</p>
</div>
</Route>
<Route match={sidebarRoute.cart}>
<div
style={{
position: 'fixed',
right: 0,
top: 200,
width: 300,
height: 200,
}}
>
<p>
Cart sidebar{' '}
<a
href="javascript:;"
onClick={() => {
primaryRoute.$replace({}, {leaves: ['sidebar']});
}}
>
x
</a>
</p>
</div>
</Route>
<Route match={primaryRoute.news}>
<p>News page</p>
<Link to={primaryRoute.default}>Home</Link>
</Route>
<Route match={primaryRoute.about}>
<p>About page</p>
<Link to={primaryRoute.default}>Home</Link>
</Route>
<Route match={primaryRoute.contact}>
<p>Contact page</p>
<Link to={primaryRoute.default}>Home</Link>
</Route>
<Route match={primaryRoute.notFound}>
<p>Not found</p>
<Link to={primaryRoute.default}>Home</Link>
</Route>
</>
);
}
}
ReactDOM.render(<App />, document.getElementById('app'));