-
-
Notifications
You must be signed in to change notification settings - Fork 354
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[PageContainer] Make PageContainer customizable for dynamic routes (#…
- Loading branch information
Showing
19 changed files
with
478 additions
and
117 deletions.
There are no files selected for viewing
71 changes: 71 additions & 0 deletions
71
docs/data/toolpad/core/components/page-container/CustomPageContainer.js
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,71 @@ | ||
import * as React from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import { PageContainer } from '@toolpad/core/PageContainer'; | ||
import { AppProvider } from '@toolpad/core/AppProvider'; | ||
import { Link, useDemoRouter } from '@toolpad/core/internals'; | ||
import { useActivePage } from '@toolpad/core/useActivePage'; | ||
import { useTheme } from '@mui/material/styles'; | ||
import Box from '@mui/material/Box'; | ||
import Paper from '@mui/material/Paper'; | ||
import invariant from 'invariant'; | ||
|
||
const NAVIGATION = [ | ||
{ | ||
segment: 'inbox', | ||
title: 'Orders', | ||
pattern: '/inbox/:id', | ||
}, | ||
]; | ||
|
||
function Content({ router }) { | ||
const id = Number(router.pathname.replace('/inbox/', '')); | ||
|
||
const activePage = useActivePage(); | ||
invariant(activePage, 'No navigation match'); | ||
|
||
const title = `Item ${id}`; | ||
const path = `${activePage.path}/${id}`; | ||
|
||
const breadCrumbs = [...activePage.breadCrumbs, { title, path }]; | ||
|
||
return ( | ||
// preview-start | ||
<PageContainer title={title} breadCrumbs={breadCrumbs}> | ||
{/* preview-end */} | ||
<Box sx={{ display: 'flex', justifyContent: 'space-between' }}> | ||
<Link href={`/inbox/${id - 1}`}>previous</Link> | ||
<Link href={`/inbox/${id + 1}`}>next</Link> | ||
</Box> | ||
</PageContainer> | ||
); | ||
} | ||
|
||
Content.propTypes = { | ||
router: PropTypes.shape({ | ||
navigate: PropTypes.func.isRequired, | ||
pathname: PropTypes.string.isRequired, | ||
searchParams: PropTypes.instanceOf(URLSearchParams).isRequired, | ||
}).isRequired, | ||
}; | ||
|
||
export default function CustomPageContainer() { | ||
const router = useDemoRouter('/inbox/123'); | ||
|
||
const theme = useTheme(); | ||
|
||
let content = ( | ||
<PageContainer> | ||
<Link href={`/inbox/123`}>Item 123</Link> | ||
</PageContainer> | ||
); | ||
|
||
if (router.pathname.startsWith('/inbox/')) { | ||
content = <Content router={router} />; | ||
} | ||
|
||
return ( | ||
<AppProvider navigation={NAVIGATION} router={router} theme={theme}> | ||
<Paper sx={{ width: '100%' }}>{content}</Paper> | ||
</AppProvider> | ||
); | ||
} |
66 changes: 66 additions & 0 deletions
66
docs/data/toolpad/core/components/page-container/CustomPageContainer.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,66 @@ | ||
import * as React from 'react'; | ||
import { PageContainer } from '@toolpad/core/PageContainer'; | ||
import { AppProvider, Router } from '@toolpad/core/AppProvider'; | ||
import { Link, useDemoRouter } from '@toolpad/core/internals'; | ||
import { useActivePage } from '@toolpad/core/useActivePage'; | ||
import { useTheme } from '@mui/material/styles'; | ||
import Box from '@mui/material/Box'; | ||
import Paper from '@mui/material/Paper'; | ||
import invariant from 'invariant'; | ||
|
||
const NAVIGATION = [ | ||
{ | ||
segment: 'inbox', | ||
title: 'Orders', | ||
pattern: '/inbox/:id', | ||
}, | ||
]; | ||
|
||
interface ContentProps { | ||
router: Router; | ||
} | ||
|
||
function Content({ router }: ContentProps) { | ||
const id = Number(router.pathname.replace('/inbox/', '')); | ||
|
||
const activePage = useActivePage(); | ||
invariant(activePage, 'No navigation match'); | ||
|
||
const title = `Item ${id}`; | ||
const path = `${activePage.path}/${id}`; | ||
|
||
const breadCrumbs = [...activePage.breadCrumbs, { title, path }]; | ||
|
||
return ( | ||
// preview-start | ||
<PageContainer title={title} breadCrumbs={breadCrumbs}> | ||
{/* preview-end */} | ||
<Box sx={{ display: 'flex', justifyContent: 'space-between' }}> | ||
<Link href={`/inbox/${id - 1}`}>previous</Link> | ||
<Link href={`/inbox/${id + 1}`}>next</Link> | ||
</Box> | ||
</PageContainer> | ||
); | ||
} | ||
|
||
export default function CustomPageContainer() { | ||
const router = useDemoRouter('/inbox/123'); | ||
|
||
const theme = useTheme(); | ||
|
||
let content = ( | ||
<PageContainer> | ||
<Link href={`/inbox/123`}>Item 123</Link> | ||
</PageContainer> | ||
); | ||
|
||
if (router.pathname.startsWith('/inbox/')) { | ||
content = <Content router={router} />; | ||
} | ||
|
||
return ( | ||
<AppProvider navigation={NAVIGATION} router={router} theme={theme}> | ||
<Paper sx={{ width: '100%' }}>{content}</Paper> | ||
</AppProvider> | ||
); | ||
} |
1 change: 1 addition & 0 deletions
1
docs/data/toolpad/core/components/page-container/CustomPageContainer.tsx.preview
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 @@ | ||
<PageContainer title={title} breadCrumbs={breadCrumbs}> |
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
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
11 changes: 9 additions & 2 deletions
11
docs/translations/api-docs/page-container/page-container.json
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
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
Oops, something went wrong.