-
- Notifications
You must be signed in to change notification settings - Fork 412
[docs] Add CRUD to themed example #4785
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
apedroferreira merged 14 commits into mui:master from bharatkashyap:examples/crud-themed Apr 2, 2025
Merged
Changes from all commits
Commits
Show all changes
14 commits Select commit Hold shift + click to select a range
8c5698c fix: Add CRUD to themed example
bharatkashyap 7601a83 fix: Pedro feedback
bharatkashyap 50391d4 fix: CI
bharatkashyap 4d3fd84 Merge branch 'master' into examples/crud-themed
bharatkashyap 08e2dde Some unrelated adjustments
apedroferreira 4b80eb8 Fix my own suggestions
apedroferreira c138222 Refix .env
apedroferreira 0cb002e Merge remote-tracking branch 'upstream/master' into examples/crud-themed
apedroferreira 064f4f7 prettier
apedroferreira 997cc59 Merge remote-tracking branch 'upstream/master' into examples/crud-themed
apedroferreira c3ee714 Fix CRUD list page size + revert example deps to previous versions
apedroferreira c52835c Merge branch 'master' into examples/crud-themed
apedroferreira d8e7812 Add TODO comment
apedroferreira 083d967 Nevermind it's an example do better no todo comments
apedroferreira File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
84 changes: 84 additions & 0 deletions 84 examples/core/auth-nextjs-themed/app/(dashboard)/employees/[[...segments]]/page.tsx
This file contains hidden or 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,84 @@ | ||
| 'use client'; | ||
| import * as React from 'react'; | ||
| import { usePathname, useRouter } from 'next/navigation'; | ||
| import { CrudProvider, List, Create, Edit, Show } from '@toolpad/core/Crud'; | ||
| import { employeesDataSource, Employee, employeesCache } from '../../../mocks/employees'; | ||
| import CustomDataGrid from '../../../components/CustomDataGrid'; | ||
| | ||
| function matchPath(pattern: string, pathname: string): string | null { | ||
| const regex = new RegExp(`^${pattern.replace(/:[^/]+/g, '([^/]+)')}$`); | ||
| const match = pathname.match(regex); | ||
| return match ? match[1] : null; | ||
| } | ||
| | ||
| export default function EmployeesCrudPage() { | ||
| const pathname = usePathname(); | ||
| const router = useRouter(); | ||
| | ||
| const rootPath = '/employees'; | ||
| const listPath = rootPath; | ||
| const showPath = `${rootPath}/:employeeId`; | ||
| const createPath = `${rootPath}/new`; | ||
| const editPath = `${rootPath}/:employeeId/edit`; | ||
| | ||
| const showEmployeeId = matchPath(showPath, pathname); | ||
| const editEmployeeId = matchPath(editPath, pathname); | ||
| | ||
| const handleRowClick = React.useCallback( | ||
| (employeeId: string | number) => { | ||
| console.log('Clicked on row with ID', employeeId); | ||
| router.push(`${rootPath}/${String(employeeId)}`); | ||
| }, | ||
| [router], | ||
| ); | ||
| | ||
| const handleCreateClick = React.useCallback(() => { | ||
| router.push(createPath); | ||
| }, [createPath, router]); | ||
| | ||
| const handleEditClick = React.useCallback( | ||
| (employeeId: string | number) => { | ||
| router.push(`${rootPath}/${String(employeeId)}/edit`); | ||
| }, | ||
| [router], | ||
| ); | ||
| | ||
| const handleCreate = React.useCallback(() => { | ||
| router.push(listPath); | ||
| }, [listPath, router]); | ||
| | ||
| const handleEdit = React.useCallback(() => { | ||
| router.push(listPath); | ||
| }, [listPath, router]); | ||
| | ||
| const handleDelete = React.useCallback(() => { | ||
| router.push(listPath); | ||
| }, [listPath, router]); | ||
| | ||
| return ( | ||
| <CrudProvider<Employee> dataSource={employeesDataSource} dataSourceCache={employeesCache}> | ||
| {pathname === listPath ? ( | ||
| <List<Employee> | ||
| initialPageSize={20} | ||
| onRowClick={handleRowClick} | ||
| onCreateClick={handleCreateClick} | ||
| onEditClick={handleEditClick} | ||
| slots={{ | ||
| dataGrid: CustomDataGrid, | ||
| }} | ||
| /> | ||
| ) : null} | ||
| {pathname === createPath ? ( | ||
| <Create<Employee> | ||
| initialValues={{ title: 'New Employee' }} | ||
| onSubmitSuccess={handleCreate} | ||
| resetOnSubmit={false} | ||
| /> | ||
| ) : null} | ||
| {pathname !== createPath && showEmployeeId ? ( | ||
| <Show<Employee> id={showEmployeeId} onEditClick={handleEditClick} onDelete={handleDelete} /> | ||
| ) : null} | ||
| {editEmployeeId ? <Edit<Employee> id={editEmployeeId} onSubmitSuccess={handleEdit} /> : null} | ||
| </CrudProvider> | ||
| ); | ||
| } | ||
21 changes: 20 additions & 1 deletion 21 examples/core/auth-nextjs-themed/app/(dashboard)/layout.tsx
This file contains hidden or 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
3 changes: 2 additions & 1 deletion 3 examples/core/auth-nextjs-themed/app/(dashboard)/orders/page.tsx
This file contains hidden or 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 |
|---|---|---|
| @@ -1,7 +1,8 @@ | ||
| 'use client'; | ||
| import * as React from 'react'; | ||
| import CustomDataGrid from '../../components/CustomDataGrid'; | ||
| import { rows, columns } from '../../mocks/gridOrdersData'; | ||
| | ||
| export default function OrdersPage() { | ||
| return <CustomDataGrid />; | ||
| return <CustomDataGrid rows={rows} columns={columns} />; | ||
| } |
24 changes: 14 additions & 10 deletions 24 examples/core/auth-nextjs-themed/app/components/CustomDataGrid.tsx
This file contains hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll expose the
dataGridslot to theCrudcomponent so we can just use that component here.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Feel free to just add a TODO comment here for this for now as i guess we will only be able to adjust here after the next release once we merge #4786