Skip to content
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

Integrate Toolpad Core in Toolpad Studio runtime #4119

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions packages/toolpad-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
"@toolpad/utils": "workspace:*",
"client-only": "^0.0.1",
"invariant": "2.2.4",
"path-to-regexp": "6.2.2",
"path-to-regexp": "7.1.0",
"prop-types": "15.8.1"
},
"devDependencies": {
Expand All @@ -76,18 +76,23 @@
"next": "^14.2.11",
"next-router-mock": "^0.9.13",
"playwright": "^1.46.1",
"react-router-dom": "6.26.2",
"sinon": "^18.0.1",
"vitest": "2.1.1"
},
"peerDependencies": {
"@mui/icons-material": "5 - 6",
"@mui/material": "5 - 6",
"next": "^14",
"react": "^18"
"react": "^18",
"react-router-dom": "^6"
},
"peerDependenciesMeta": {
"next": {
"optional": true
},
"react-router-dom": {
"optional": true
}
},
"sideEffects": false,
Expand Down
10 changes: 7 additions & 3 deletions packages/toolpad-core/src/DashboardLayout/DashboardLayout.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use client';
import * as React from 'react';
import PropTypes from 'prop-types';
import { styled, useTheme, type Theme } from '@mui/material';
import { styled, useTheme, type Theme, SxProps } from '@mui/material';
import MuiAppBar from '@mui/material/AppBar';
import Avatar from '@mui/material/Avatar';
import Box from '@mui/material/Box';
Expand Down Expand Up @@ -357,6 +357,10 @@ export interface DashboardLayoutProps {
toolbarActions?: {};
toolbarAccount?: AccountProps;
};
/**
* The system prop that allows defining system overrides as well as additional CSS styles.
*/
sx?: SxProps<Theme>;
}

/**
Expand All @@ -370,7 +374,7 @@ export interface DashboardLayoutProps {
* - [DashboardLayout API](https://mui.com/toolpad/core/api/dashboard-layout)
*/
function DashboardLayout(props: DashboardLayoutProps) {
const { children, disableCollapsibleSidebar = false, slots, slotProps } = props;
const { children, disableCollapsibleSidebar = false, slots, slotProps, sx } = props;

const theme = useTheme();

Expand Down Expand Up @@ -517,7 +521,7 @@ function DashboardLayout(props: DashboardLayoutProps) {
const ToolbarAccountSlot = slots?.toolbarAccount ?? Account;

return (
<Box sx={{ display: 'flex' }}>
<Box sx={{ ...sx, display: 'flex' }}>
<AppBar color="inherit" position="fixed">
{
// TODO: (minWidth: 100vw) Temporary fix to issue reported in https://github.com/mui/material-ui/issues/43244
Expand Down
36 changes: 36 additions & 0 deletions packages/toolpad-core/src/react-router-dom/AppProvider.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* @vitest-environment jsdom
*/

import * as React from 'react';
import { describe, test, expect } from 'vitest';
import { render, screen } from '@testing-library/react';
import { AppProvider } from './AppProvider';
import { Router } from '../AppProvider';

interface RouterTestProps {
children: React.ReactNode;
}

function RouterTest({ children }: RouterTestProps) {
const [pathname, setPathname] = React.useState('/page');

const router = React.useMemo<Router>(() => {
return {
pathname,
searchParams: new URLSearchParams(),
navigate: (path) => setPathname(String(path)),
};
}, [pathname]);

return <AppProvider router={router}>{children}</AppProvider>;
}

describe('React Router DOM AppProvider', () => {
test('renders content correctly', async () => {
// placeholder test
render(<RouterTest>Hello</RouterTest>);

expect(screen.getByText('Hello')).toBeTruthy();
});
});
39 changes: 39 additions & 0 deletions packages/toolpad-core/src/react-router-dom/AppProvider.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
'use client';
import * as React from 'react';
import { useSearchParams, useLocation, useNavigate } from 'react-router-dom';
import { AppProvider as AppProviderComponent, type AppProviderProps, Navigate, Router } from '../AppProvider';

/**
* @ignore - internal component.
*/
function AppProvider(props: AppProviderProps) {
const { pathname } = useLocation();
const [searchParams] = useSearchParams();
const navigate = useNavigate();

const navigateImpl = React.useCallback<Navigate>(
(url, { history = 'auto' } = {}) => {
if (history === 'auto' || history === 'push') {
return navigate(url);
}
if (history === 'replace') {
return navigate(url, { replace: true });
}
throw new Error(`Invalid history option: ${history}`);
},
[navigate],
);

const routerImpl = React.useMemo<Router>(
() => ({
pathname,
searchParams,
navigate: navigateImpl,
}),
[pathname, searchParams, navigateImpl],
);

return <AppProviderComponent router={routerImpl} {...props} />;
}

export { AppProvider };
1 change: 1 addition & 0 deletions packages/toolpad-core/src/react-router-dom/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './AppProvider';
1 change: 1 addition & 0 deletions packages/toolpad-studio/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
"@mui/x-tree-view": "7.17.0",
"@tanstack/react-query": "5.56.2",
"@tanstack/react-query-devtools": "5.56.2",
"@toolpad/core": "workspace:*",
"@toolpad/studio-components": "workspace:*",
"@toolpad/studio-runtime": "workspace:*",
"@toolpad/utils": "workspace:*",
Expand Down
Loading