From 640f36874e3413a08981ab679de443afe811f454 Mon Sep 17 00:00:00 2001 From: Gildas Garcia <1122076+djhi@users.noreply.github.com> Date: Fri, 20 Jun 2025 16:18:06 +0200 Subject: [PATCH] Revert "Fix limitation preventing to declare create and show routes from the list view" --- docs/Resource.md | 1 - packages/ra-core/src/core/Resource.spec.tsx | 82 +------- .../ra-core/src/core/Resource.stories.tsx | 187 +----------------- packages/ra-core/src/core/Resource.tsx | 21 +- packages/react-admin/src/Resource.stories.tsx | 39 +--- 5 files changed, 13 insertions(+), 317 deletions(-) diff --git a/docs/Resource.md b/docs/Resource.md index 2e2a6f24cbe..83330adef82 100644 --- a/docs/Resource.md +++ b/docs/Resource.md @@ -359,7 +359,6 @@ In order to display a list of songs for the selected artist, `` should import { List, Datagrid, TextField, useRecordContext } from 'react-admin'; import { useParams } from 'react-router-dom'; import { Button } from '@mui/material'; -import EditIcon from '@mui/icons-material/Edit'; export const SongList = () => { const { id } = useParams(); diff --git a/packages/ra-core/src/core/Resource.spec.tsx b/packages/ra-core/src/core/Resource.spec.tsx index ca55b1a5781..c5581f67ac2 100644 --- a/packages/ra-core/src/core/Resource.spec.tsx +++ b/packages/ra-core/src/core/Resource.spec.tsx @@ -1,12 +1,6 @@ import * as React from 'react'; import { render, screen } from '@testing-library/react'; -import { - Basic, - OnlyList, - WithAllDialogs, - WithCreateDialog, - WithShowDialog, -} from './Resource.stories'; +import { Basic } from './Resource.stories'; describe('', () => { it('renders resource routes by default', async () => { @@ -29,78 +23,4 @@ describe('', () => { navigate('/posts/customroute'); await screen.findByText('PostCustomRoute'); }); - - it('always renders the list if only a list view is present', async () => { - let navigate; - render( - { - navigate = n; - }} - /> - ); - navigate('/posts'); - await screen.findByText('PostList'); - navigate('/posts/123'); - await screen.findByText('PostList'); - navigate('/posts/123/show'); - await screen.findByText('PostList'); - navigate('/posts/create'); - await screen.findByText('PostList'); - navigate('/posts/customroute'); - await screen.findByText('PostList'); - }); - - it('allows to render all dialogs views declared in the list view', async () => { - let navigate; - render( - { - navigate = n; - }} - /> - ); - navigate('/posts'); - await screen.findByText('PostList'); - navigate('/posts/123'); - await screen.findByText('PostEdit'); - navigate('/posts/123/show'); - await screen.findByText('PostShow'); - navigate('/posts/create'); - await screen.findByText('PostCreate'); - }); - - it('allows to render a create dialog declared in the list even if there is an edit view', async () => { - let navigate; - render( - { - navigate = n; - }} - /> - ); - navigate('/posts'); - await screen.findByText('PostList'); - navigate('/posts/123'); - await screen.findByText('PostEdit'); - navigate('/posts/create'); - await screen.findByText('PostCreate'); - }); - - it('allows to render a show dialog declared in the list even if there is an edit view', async () => { - let navigate; - render( - { - navigate = n; - }} - /> - ); - navigate('/posts'); - await screen.findByText('PostList'); - navigate('/posts/123'); - await screen.findByText('PostEdit'); - navigate('/posts/123/show'); - await screen.findByText('PostShow'); - }); }); diff --git a/packages/ra-core/src/core/Resource.stories.tsx b/packages/ra-core/src/core/Resource.stories.tsx index c877177f282..0a826bb2ade 100644 --- a/packages/ra-core/src/core/Resource.stories.tsx +++ b/packages/ra-core/src/core/Resource.stories.tsx @@ -1,10 +1,9 @@ import * as React from 'react'; -import { NavigateFunction, Route, Routes } from 'react-router'; -import { Link, useParams, useLocation } from 'react-router-dom'; +import { NavigateFunction, Route } from 'react-router'; +import { Link } from 'react-router-dom'; import { TestMemoryRouter } from '../routing'; import { Resource } from './Resource'; import { CoreAdmin } from './CoreAdmin'; -import { Browser } from '../storybook/FakeBrowser'; export default { title: 'ra-core/core/Resource', @@ -62,186 +61,10 @@ export const Basic = ({ navigateCallback?: (n: NavigateFunction) => void; }) => ( - - - - - + + + ); const Loading = () =>
Loading...
; - -export const OnlyList = ({ - navigateCallback, -}: { - navigateCallback?: (n: NavigateFunction) => void; -}) => ( - - - - - - - -); - -export const WithAllDialogs = ({ - navigateCallback, -}: { - navigateCallback?: (n: NavigateFunction) => void; -}) => ( - - - - - - - -); - -const PostListWithAllDialogs = () => ( -
-
PostList
- create edit{' '} - show - - - -
-); - -const PostCreateDialog = () => ( - - -
- close -
-
PostCreate
- - } - /> -
-); - -const PostEditDialog = () => { - return ( - - } /> - - ); -}; - -const PostEditDialogView = () => { - const params = useParams<'id'>(); - const location = useLocation(); - const isMatch = - params.id && - params.id !== 'create' && - location.pathname.indexOf('/show') === -1; - return isMatch ? ( -
-
- close -
-
PostEdit
-
- ) : null; -}; - -const PostShowDialog = () => { - return ( - - } /> - - ); -}; - -const PostShowDialogView = () => { - const params = useParams<'id'>(); - const isMatch = params.id && params.id !== 'create'; - return isMatch ? ( -
-
- close -
-
PostShow
-
- ) : null; -}; - -export const WithCreateDialog = ({ - navigateCallback, -}: { - navigateCallback?: (n: NavigateFunction) => void; -}) => ( - - - - - - - -); - -const PostListWithCreateDialog = () => ( -
-
PostList
- create edit{' '} - -
-); - -export const WithShowDialog = ({ - navigateCallback, -}: { - navigateCallback?: (n: NavigateFunction) => void; -}) => ( - - - - - - - -); - -const PostListWithShowDialog = () => ( -
-
PostList
- edit show - -
-); diff --git a/packages/ra-core/src/core/Resource.tsx b/packages/ra-core/src/core/Resource.tsx index 8f9f40eb926..7a62c0372b5 100644 --- a/packages/ra-core/src/core/Resource.tsx +++ b/packages/ra-core/src/core/Resource.tsx @@ -1,25 +1,14 @@ import * as React from 'react'; import { ComponentType, ReactElement, isValidElement } from 'react'; -import { Route, Routes, useLocation, matchPath } from 'react-router-dom'; +import { Route, Routes } from 'react-router-dom'; import { isValidElementType } from 'react-is'; import { ResourceProps } from '../types'; import { ResourceContextProvider } from './ResourceContextProvider'; import { RestoreScrollPosition } from '../routing/RestoreScrollPosition'; -import { useSplatPathBase } from '../routing'; export const Resource = (props: ResourceProps) => { const { create, edit, list, name, show } = props; - const location = useLocation(); - const splatPathBase = useSplatPathBase(); - const matchCreate = matchPath( - `${splatPathBase}/create/*`, - location.pathname - ); - const matchShow = matchPath( - `${splatPathBase}/:id/show/*`, - location.pathname - ); return ( @@ -27,12 +16,8 @@ export const Resource = (props: ResourceProps) => { {create && ( )} - {!matchCreate && show && ( - - )} - {!matchCreate && !matchShow && edit && ( - - )} + {show && } + {edit && } {list && ( ( ); const BookList = () => { - const { id } = useParams(); + const { authorId } = useParams(); return ( - - + + - ); }; -const EditBookButton = () => { - const book = useRecordContext(); - return ( - - ); -}; - -const BookEdit = () => { - const { id, bookId } = useParams(); - return ( - - - - - - - ); -}; - export const Nested = () => ( - } /> - } /> + } /> );