-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement grid-routes component
- Loading branch information
Showing
12 changed files
with
192 additions
and
46 deletions.
There are no files selected for viewing
41 changes: 41 additions & 0 deletions
41
apps/web/src/app/features/demo/demo-feature-grid-routes.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { SimpleGrid } from '@mantine/core' | ||
import { UiCard, UiGridRoutes } from '@pubkey-ui/core' | ||
|
||
export function DemoFeatureGridRoutes() { | ||
return ( | ||
<UiCard title="Grid Routes"> | ||
<UiGridRoutes | ||
basePath="/demo/grid-routes" | ||
routes={[ | ||
{ | ||
path: 'overview', | ||
label: 'Overview', | ||
element: ( | ||
<SimpleGrid cols={2} spacing="md"> | ||
<UiCard title="Overview">Overview</UiCard> | ||
</SimpleGrid> | ||
), | ||
}, | ||
{ | ||
path: 'content', | ||
label: 'Content', | ||
element: ( | ||
<SimpleGrid cols={2} spacing="md"> | ||
<UiCard title="Content">Content</UiCard> | ||
</SimpleGrid> | ||
), | ||
}, | ||
{ | ||
path: 'settings', | ||
label: 'Settings', | ||
element: ( | ||
<SimpleGrid cols={2} spacing="md"> | ||
<UiCard title="Settings">Settings</UiCard> | ||
</SimpleGrid> | ||
), | ||
}, | ||
]} | ||
/> | ||
</UiCard> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './ui-grid-routes' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { Grid, GridColProps, GridProps, NavLink } from '@mantine/core' | ||
import { ReactNode, useMemo } from 'react' | ||
import { Link, Navigate, useLocation, useRoutes } from 'react-router-dom' | ||
import { UiNotFound } from '../ui-not-found' | ||
|
||
export interface UiGridRoute { | ||
path: string | ||
label?: string | ||
element: ReactNode | ||
} | ||
export interface UiGridRoutesProps extends GridProps { | ||
basePath: string | ||
routes: UiGridRoute[] | ||
leftColProps?: GridColProps | ||
rightColProps?: GridColProps | ||
} | ||
|
||
export function UiGridRoutes({ basePath, routes, leftColProps, rightColProps, ...props }: UiGridRoutesProps) { | ||
const { pathname } = useLocation() | ||
|
||
const links = useMemo( | ||
() => | ||
routes | ||
.filter((app) => app.label) | ||
.map((app) => { | ||
const to = `${basePath}/${app.path}` | ||
return <NavLink active={pathname.startsWith(to)} component={Link} key={app.path} label={app.label} to={to} /> | ||
}), | ||
[basePath, pathname, routes], | ||
) | ||
|
||
const router = useRoutes([ | ||
{ index: true, element: <Navigate to={routes[0].path} replace /> }, | ||
...routes.map((item) => ({ path: `${item.path}/*`, element: item.element })), | ||
{ path: '*', element: <UiNotFound to={basePath} /> }, | ||
]) | ||
|
||
return ( | ||
<Grid {...props}> | ||
<Grid.Col span={{ base: 12, sm: 2 }} {...rightColProps}> | ||
{links} | ||
</Grid.Col> | ||
<Grid.Col span={{ base: 12, sm: 10 }} {...leftColProps}> | ||
{router} | ||
</Grid.Col> | ||
</Grid> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,6 +25,7 @@ | |
"dashboard-grid", | ||
"debug", | ||
"form", | ||
"grid-routes", | ||
"group", | ||
"header", | ||
"layout", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
...rs/src/generators/component/files/grid-routes/__prefixFileName__-grid-routes.tsx.template
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { Grid, GridColProps, GridProps, NavLink } from '@mantine/core' | ||
import { ReactNode, useMemo } from 'react' | ||
import { Link, Navigate, useLocation, useRoutes } from 'react-router-dom' | ||
import { <%= prefix.className %>NotFound } from '../<%= prefix.fileName %>-not-found' | ||
|
||
export interface <%= prefix.className %>GridRoute { | ||
path: string | ||
label?: string | ||
element: ReactNode | ||
} | ||
export interface <%= prefix.className %>GridRoutesProps extends GridProps { | ||
basePath: string | ||
routes: <%= prefix.className %>GridRoute[] | ||
leftColProps?: GridColProps | ||
rightColProps?: GridColProps | ||
} | ||
|
||
export function <%= prefix.className %>GridRoutes({ basePath, routes, leftColProps, rightColProps, ...props }: <%= prefix.className %>GridRoutesProps) { | ||
const { pathname } = useLocation() | ||
|
||
const links = useMemo( | ||
() => | ||
routes | ||
.filter((app) => app.label) | ||
.map((app) => { | ||
const to = `${basePath}/${app.path}` | ||
return <NavLink active={pathname.startsWith(to)} component={Link} key={app.path} label={app.label} to={to} /> | ||
}), | ||
[basePath, pathname, routes], | ||
) | ||
|
||
const router = useRoutes([ | ||
{ index: true, element: <Navigate to={routes[0].path} replace /> }, | ||
...routes.map((item) => ({ path: `${item.path}/*`, element: item.element })), | ||
{ path: '*', element: <<%= prefix.className %>NotFound to={basePath} /> }, | ||
]) | ||
|
||
return ( | ||
<Grid {...props}> | ||
<Grid.Col span={{ base: 12, sm: 2 }} {...rightColProps}> | ||
{links} | ||
</Grid.Col> | ||
<Grid.Col span={{ base: 12, sm: 10 }} {...leftColProps}> | ||
{router} | ||
</Grid.Col> | ||
</Grid> | ||
) | ||
} |
1 change: 1 addition & 0 deletions
1
packages/generators/src/generators/component/files/grid-routes/index.ts.template
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './<%= prefixFileName %>-grid-routes' |
41 changes: 41 additions & 0 deletions
41
packages/generators/src/generators/feature/files/demo/demo-feature-grid-routes.tsx.template
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { SimpleGrid } from '@mantine/core' | ||
import { <%= prefix.className %>Card, <%= prefix.className %>GridRoutes } from '<%= uiImport %>' | ||
|
||
export function DemoFeatureGridRoutes() { | ||
return ( | ||
<<%= prefix.className %>Card title="Grid Routes"> | ||
<<%= prefix.className %>GridRoutes | ||
basePath="/demo/grid-routes" | ||
routes={[ | ||
{ | ||
path: 'overview', | ||
label: 'Overview', | ||
element: ( | ||
<SimpleGrid cols={2} spacing="md"> | ||
<<%= prefix.className %>Card title="Overview">Overview</<%= prefix.className %>Card> | ||
</SimpleGrid> | ||
), | ||
}, | ||
{ | ||
path: 'content', | ||
label: 'Content', | ||
element: ( | ||
<SimpleGrid cols={2} spacing="md"> | ||
<<%= prefix.className %>Card title="Content">Content</<%= prefix.className %>Card> | ||
</SimpleGrid> | ||
), | ||
}, | ||
{ | ||
path: 'settings', | ||
label: 'Settings', | ||
element: ( | ||
<SimpleGrid cols={2} spacing="md"> | ||
<<%= prefix.className %>Card title="Settings">Settings</<%= prefix.className %>Card> | ||
</SimpleGrid> | ||
), | ||
}, | ||
]} | ||
/> | ||
</<%= prefix.className %>Card> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters