-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.tsx
199 lines (184 loc) · 4.36 KB
/
index.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 createSolidRouter, {
useRoute,
ReadRoutes,
RouteActive,
RSM,
} from "solid-typefu-router5";
import browserPluginFactory from "router5-plugin-browser";
import { render } from "solid-js/web";
import createRouter from "router5";
import { createEffect, createSignal, For } from "solid-js";
const routes = [
{ name: "home", path: "/" },
{
name: "users",
path: "/users?page",
children: [
{ name: "profile", path: "/:id<\\d+>" },
{ name: "edit", path: "/edit" },
],
},
{ name: "about", path: "/about" },
] as const;
type Routes = ReadRoutes<typeof routes>;
type RenderRoutes = RSM<Routes>;
type UserRoutes = RenderRoutes["users"];
const { Link, Router, Provider } = createSolidRouter({
routes,
defaultLinkProps: {
navActiveClassList: (s) => ({
"is-active": s > 0,
}),
},
back: () => window.history.back(),
forward: () => window.history.forward(),
createRouter5: (routes) => {
const router = createRouter(routes, { allowNotFound: true });
router.usePlugin(browserPluginFactory({ useHash: false }));
return router;
},
});
const About = () => (
<>
<h1>About</h1>
<p>
This is the about page of the <code>solid-typefu-router5</code> example
project.
</p>
</>
);
const Home = () => (
<>
<h1>Home</h1>
<p>
This is the <code>solid-typefu-router5</code> example project.
</p>
</>
);
function userList(page: number) {
return [5 * page, 5 * page + 1, 5 * page + 2, 5 * page + 3, 5 * page + 4];
}
const Users = (props: { page: number }) => (
<>
<h1>Users</h1>
<p>Page: {props.page}</p>
<ul>
<For each={userList(props.page)}>
{(user) => (
<li>
<Link to="users.profile" params={{ id: "" + user }}>
To user {user}
</Link>
</li>
)}
</For>
</ul>
<Link
to="users"
params={{ page: "" + (props.page - 1) }}
display="button"
disabled={props.page <= 0}
>
Previous page
</Link>
<br />
<Link to="users" params={{ page: "" + (props.page + 1) }} display="button">
Next page
</Link>
<hr />
</>
);
const UsersEdit = () => (
<>
<h1>Edit users</h1>
<p>If you could edit the users here, here is where that would be. </p>{" "}
</>
);
const UserProfile = (props: { id: number }) => (
<>
<h1>User {props.id} profile</h1>
<p>hello world!</p>
</>
);
const App = () => {
const route = useRoute();
const [something, setSomething] = createSignal(0);
createEffect(() => {
const r = route();
console.log("route changed!", r.name, { ...r.params });
});
return (
<div>
<b>SolidJS test: </b> {something()}{" "}
<button onClick={() => setSomething((s) => s + 1)}>Increment</button>
<hr />
<b>Route: </b>
<code>{route().name}</code>
<code>{JSON.stringify(route())}</code>
<hr />
<nav class="nav">
<ul>
<li>
<Link to="@@back">Back</Link> / <Link to="@@forward">Forward</Link>
</li>
<li>
<Link nav to="home">
Home
</Link>
</li>
<li>
<Link nav navIgnoreParams to="users" params={{ page: "0" }}>
Users
</Link>
<ul>
<li>
<Link nav navIgnoreParams to="users.edit">
Edit
</Link>
</li>
</ul>
</li>
<li>
<Link nav to="about">
About
</Link>
</li>
</ul>
</nav>
<hr />
<Router>
{{
about: { render: About },
home: { render: Home },
users: {
render: (p) => (
<>
<h1>USERS</h1>
<hr />
<div>{p.children}</div>
</>
),
fallback: (p) => <Users page={Number(p.params.page ?? 0)} />,
edit: {
render: () => <UsersEdit />,
},
profile: {
render: (p: { params: { id: string } }) => (
<UserProfile id={Number(p.params.id)} />
),
},
},
}}
</Router>
</div>
);
};
// end interesting parts
render(
() => (
<Provider>
<App />
</Provider>
),
document.getElementById("app") as any
);