Skip to content

Commit efadf0c

Browse files
fix: <Outlet/> and index path support
* add: example with Outlet and index routes
1 parent 5c17f82 commit efadf0c

6 files changed

Lines changed: 86 additions & 13 deletions

File tree

example/routes.tsx

Lines changed: 43 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { IRoutesConfig, useCheckRole } from '../.';
2-
import { Navigate, useParams } from 'react-router-dom';
2+
import { Navigate, useParams, Outlet } from 'react-router-dom';
33

44
export const roles = {
55
ADMIN: 'ADMIN',
@@ -41,27 +41,63 @@ export const routes: IRoutesConfig = {
4141
},
4242
],
4343
private: [
44+
{
45+
path: '/outlet',
46+
component: (
47+
<>
48+
<h1>outlet path (this must be visible)</h1>
49+
<Outlet />
50+
</>
51+
),
52+
children: [
53+
{
54+
index: true,
55+
component: <h1>index path</h1>,
56+
},
57+
{
58+
path: 'more',
59+
component: <h1>more</h1>,
60+
},
61+
{
62+
path: 'less',
63+
component: <h1>less</h1>,
64+
},
65+
],
66+
},
4467
{
4568
path: '/home',
4669
component: <h1>Home Page</h1>,
4770
},
4871
{
4972
path: '/private',
50-
component: <h1>Private Page</h1>,
73+
component: (
74+
<>
75+
<span>some private layout</span>
76+
<Outlet />
77+
</>
78+
),
5179
children: [
80+
{
81+
index: true,
82+
component: <h1>Private Page</h1>,
83+
},
5284
{
5385
path: 'create',
54-
component: <h1>nested private /create</h1>
86+
component: <h1>nested private /create</h1>,
5587
},
5688
{
5789
path: ':id',
58-
component: <NestedPrivatePage />,
90+
component: <Outlet/>,
5991
children: [
92+
{
93+
index: true,
94+
component: <NestedPrivatePage />
95+
},
6096
{
6197
path: 'update',
62-
component: <h1>nested private /:id/update</h1>
63-
}
64-
]
98+
component: <h1>nested private /:id/update</h1>,
99+
},
100+
],
65101
},
66102
],
67103
},

example/src/App.tsx

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,19 @@ const App = () => {
8282
</Link>
8383
</li>
8484
</ul>
85+
<br/>
86+
with outlet:
87+
<ul>
88+
<li>
89+
<Link to={'/outlet'}>/outlet</Link>
90+
</li>
91+
<li>
92+
<Link to={'/outlet/more'}>/outlet/more</Link>
93+
</li>
94+
<li>
95+
<Link to={'/outlet/less'}>/outlet/less</Link>
96+
</li>
97+
</ul>
8598
<fieldset style={{minHeight: 100}}>
8699
<legend>pages wrapper</legend>
87100
<Routes />

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"version": "0.4.2",
2+
"version": "0.4.3",
33
"license": "MIT",
44
"main": "dist/index.js",
55
"typings": "dist/index.d.ts",

readme.md

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,16 +68,30 @@ export const routes: IRoutesConfig = {
6868
// nested routes example
6969
{
7070
path: '/posts',
71-
component: <h1>Posts Lists</h1>,
71+
component: <>
72+
<h1>Posts Lists Layout</h1>
73+
<Outlet/> {/* render outlet (the matched paths) */}
74+
</>,
7275
children: [
76+
{
77+
index: true,
78+
component: <h1>a list of posts here...</h1>
79+
},
7380
{
7481
path: 'create',
7582
component: <h1>create new post</h1>
7683
},
7784
{
7885
path: ':id',
79-
component: <h1>Single post</h1>,
86+
component: <>
87+
Single post page layout
88+
<Outlet/>
89+
</>,
8090
children: [
91+
{
92+
index: true,
93+
component: <h1>post details</h1>
94+
},
8195
{
8296
path: 'update',
8397
component: <h1>update post with dynamic :id</h1>
@@ -243,6 +257,9 @@ export interface IRoute {
243257

244258
/** the component to be rendered under the path */
245259
component: React.ReactElement;
260+
261+
/** if this is a route definition for index path */
262+
index?: boolean;
246263

247264
/** used for nested (aka children) routes definition */
248265
children?: IRoute[];

src/Routes.tsx

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,18 @@ const createNestedRoutes = (
2121
}
2222
if (route.children) {
2323
return (
24-
<Route key={i} path={route.path}>
25-
<Route index={true} element={<RouteType {...route} />} />
24+
<Route key={i} path={route.path} element={<RouteType {...route} />}>
2625
{route.children && createNestedRoutes(route.children, RouteType)}
2726
</Route>
2827
);
2928
}
3029
return (
31-
<Route key={i} path={route.path} element={<RouteType {...route} />} />
30+
<Route
31+
index={route.index}
32+
key={i}
33+
path={route.path}
34+
element={<RouteType {...route} />}
35+
/>
3236
);
3337
})}
3438
</>

src/types.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ export interface IRoute {
77
/** the component to be rendered under the path */
88
component: React.ReactElement;
99

10+
/** if this is a route definition for index path */
11+
index?: boolean;
12+
1013
/** used for nested routes */
1114
children?: IRoute[];
1215

0 commit comments

Comments
 (0)