Skip to content

Commit d49a8e9

Browse files
committed
add render prop on Show
1 parent 487d255 commit d49a8e9

File tree

3 files changed

+36
-4
lines changed

3 files changed

+36
-4
lines changed

packages/ra-ui-materialui/src/detail/Show.stories.tsx

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,3 +280,22 @@ export const Themed = () => (
280280
</Admin>
281281
</TestMemoryRouter>
282282
);
283+
284+
export const WithRenderProp = () => (
285+
<TestMemoryRouter initialEntries={['/books/1/show']}>
286+
<Admin dataProvider={dataProvider}>
287+
<Resource
288+
name="books"
289+
show={() => (
290+
<Show
291+
render={showContext =>
292+
showContext.record ? (
293+
<span>{showContext.record.title}</span>
294+
) : null
295+
}
296+
/>
297+
)}
298+
/>
299+
</Admin>
300+
</TestMemoryRouter>
301+
);

packages/ra-ui-materialui/src/detail/Show.tsx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,12 @@ export const Show = <RecordType extends RaRecord = any>(
7575
...rest
7676
} = props;
7777

78+
if (!props.render && !props.children) {
79+
throw new Error(
80+
'<Show> requires either a `render` prop or `children` prop'
81+
);
82+
}
83+
7884
return (
7985
<ShowBase<RecordType>
8086
id={id}
@@ -90,7 +96,7 @@ export const Show = <RecordType extends RaRecord = any>(
9096

9197
export interface ShowProps<RecordType extends RaRecord = any>
9298
extends ShowBaseProps<RecordType>,
93-
Omit<ShowViewProps, 'children'> {}
99+
Omit<ShowViewProps, 'children' | 'render'> {}
94100

95101
const defaultLoading = <Loading />;
96102

packages/ra-ui-materialui/src/detail/ShowView.tsx

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as React from 'react';
2-
import type { ReactElement, ElementType } from 'react';
2+
import type { ReactElement, ElementType, ReactNode } from 'react';
33
import {
44
Card,
55
type ComponentsOverrides,
@@ -8,7 +8,11 @@ import {
88
type Theme,
99
} from '@mui/material';
1010
import clsx from 'clsx';
11-
import { useShowContext, useResourceDefinition } from 'ra-core';
11+
import {
12+
useShowContext,
13+
useResourceDefinition,
14+
ShowControllerResult,
15+
} from 'ra-core';
1216
import { ShowActions } from './ShowActions';
1317
import { Title } from '../layout';
1418
import { ShowProps } from './Show';
@@ -20,14 +24,16 @@ export const ShowView = (props: ShowViewProps) => {
2024
actions,
2125
aside,
2226
children,
27+
render,
2328
className,
2429
component: Content = Card,
2530
emptyWhileLoading = false,
2631
title,
2732
...rest
2833
} = props;
2934

30-
const { resource, defaultTitle, record } = useShowContext();
35+
const showContext = useShowContext();
36+
const { resource, defaultTitle, record } = showContext;
3137
const { hasEdit } = useResourceDefinition();
3238

3339
const finalActions =
@@ -66,6 +72,7 @@ export interface ShowViewProps
6672
emptyWhileLoading?: boolean;
6773
title?: string | ReactElement | false;
6874
sx?: SxProps<Theme>;
75+
render?: (showContext: ShowControllerResult) => ReactNode;
6976
}
7077

7178
const PREFIX = 'RaShow';

0 commit comments

Comments
 (0)