Skip to content

Commit 3dc9e9e

Browse files
committed
add render prop on Show
1 parent d49a8e9 commit 3dc9e9e

File tree

5 files changed

+18
-5
lines changed

5 files changed

+18
-5
lines changed

docs/Show.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@ That's enough to display the post show view above.
6161

6262
| Prop | Required | Type | Default | Description
6363
|------------------|----------|-------------------|---------|--------------------------------------------------------
64-
| `children` | Required | `ReactNode` | | The components rendering the record fields
64+
| `children` | Required if no render | `ReactNode` | | The components rendering the record fields
65+
| `render` | Required if no children | `(showContext) => ReactNode` | | A function rendering the record fields, receive the show context as its argument
6566
| `actions` | Optional | `ReactElement` | | The actions to display in the toolbar
6667
| `aside` | Optional | `ReactElement` | | The component to display on the side of the list
6768
| `className` | Optional | `string` | | passed to the root component

packages/ra-ui-materialui/src/detail/Edit.spec.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@ describe('<Edit />', () => {
341341
});
342342
});
343343

344-
it('should allow tu use render prop instead of children', async () => {
344+
it('should allow to use render prop instead of children', async () => {
345345
render(<WithRenderProp />);
346346
await waitFor(() => {
347347
expect(screen.queryAllByText('War and Peace')).toHaveLength(1);

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import {
2525
TitleFalse,
2626
TitleElement,
2727
Themed,
28+
WithRenderProp,
2829
} from './Show.stories';
2930
import { Show } from './Show';
3031

@@ -132,11 +133,18 @@ describe('<Show />', () => {
132133

133134
it('should be customized by a theme', async () => {
134135
render(<Themed />);
135-
expect(screen.queryByTestId('themed-view').classList).toContain(
136+
expect(screen.queryByTestId('themed-view')?.classList).toContain(
136137
'custom-class'
137138
);
138139
});
139140

141+
it('should allow to use render prop instead of children', async () => {
142+
render(<WithRenderProp />);
143+
await waitFor(() => {
144+
expect(screen.queryByText('War and Peace')).not.toBeNull();
145+
});
146+
});
147+
140148
describe('title', () => {
141149
it('should display by default the title of the resource', async () => {
142150
render(<Basic />);

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,9 @@ import { Loading } from '../layout';
4545
* );
4646
* export default App;
4747
*
48+
* @typedef {(showContext: Object) => ReactNode} RenderProp
4849
* @param {ShowProps} inProps
50+
* @param {RenderProp} inProps.render A function rendering the page content, receive the show context as its argument.
4951
* @param {ReactElement|false} inProps.actions An element to display above the page content, or false to disable actions.
5052
* @param {string} inProps.className A className to apply to the page content.
5153
* @param {ElementType} inProps.component The component to use as root component (div by default).

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ export const ShowView = (props: ShowViewProps) => {
3939
const finalActions =
4040
typeof actions === 'undefined' && hasEdit ? defaultActions : actions;
4141

42-
if (!children || (!record && emptyWhileLoading)) {
42+
if (!record && emptyWhileLoading) {
4343
return null;
4444
}
4545
return (
@@ -57,7 +57,9 @@ export const ShowView = (props: ShowViewProps) => {
5757
[ShowClasses.noActions]: !finalActions,
5858
})}
5959
>
60-
<Content className={ShowClasses.card}>{children}</Content>
60+
<Content className={ShowClasses.card}>
61+
{render ? render(showContext) : children}
62+
</Content>
6163
{aside}
6264
</div>
6365
</Root>

0 commit comments

Comments
 (0)