Skip to content

Commit fb73b7c

Browse files
committed
update show
1 parent 4573871 commit fb73b7c

File tree

4 files changed

+97
-573
lines changed

4 files changed

+97
-573
lines changed

docs_headless/src/content/docs/show/ShowBase.md

Lines changed: 62 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
11
---
2-
layout: default
3-
title: "The ShowBase Component"
4-
storybook_path: ra-core-controller-showbase--default-title
2+
title: "<ShowBase>"
53
---
64

7-
# `<ShowBase>`
5+
`<ShowBase>` fetches the record from the data provider via `dataProvider.getOne()`, puts it in a [`ShowContext`](./useShowContext.md), and renders its child. Use it to build a custom show page layout.
86

9-
`<ShowBase>` is a headless variant of [`<Show>`](./Show.md). It fetches the record from the data provider via `dataProvider.getOne()`, puts it in a [`ShowContext`](./useShowContext.md), and renders its child. Use it to build a custom show page layout.
10-
11-
Contrary to [`<Show>`](./Show.md), it does not render the page layout, so no title, no actions, and no `<Card>`.
7+
As a headless component, it does not render any layout by default.
128

139
`<ShowBase>` relies on the [`useShowController`](./useShowController.md) hook.
1410

@@ -19,20 +15,20 @@ Use `<ShowBase>` instead of `<Show>` when you want a completely custom page layo
1915
```jsx
2016
// in src/posts.jsx
2117
import * as React from "react";
22-
import { ShowBase } from 'react-admin';
18+
import { ShowBase } from 'ra-core';
2319

2420
const PostShow = () => (
2521
<ShowBase resource="posts">
26-
<Grid container>
27-
<Grid item xs={8}>
28-
<SimpleShowLayout>
22+
<div style={{ display: 'flex' }}>
23+
<div style={{ flex: 2 }}>
24+
<div>
2925
...
30-
</SimpleShowLayout>
31-
</Grid>
32-
<Grid item xs={4}>
26+
</div>
27+
</div>
28+
<div style={{ flex: 1 }}>
3329
Show instructions...
34-
</Grid>
35-
</Grid>
30+
</div>
31+
</div>
3632
<div>
3733
Post related links...
3834
</div>
@@ -44,15 +40,17 @@ Components using `<ShowBase>` can be used as the `show` prop of a `<Resource>` c
4440

4541
```jsx
4642
// in src/App.jsx
47-
import { Admin, Resource } from 'react-admin';
43+
import { CoreAdminContext, CoreAdminUI, Resource } from 'ra-core';
4844

4945
import { dataProvider } from './dataProvider';
5046
import { PostShow } from './posts';
5147

5248
const App = () => (
53-
<Admin dataProvider={dataProvider}>
54-
<Resource name="posts" show={PostShow} />
55-
</Admin>
49+
<CoreAdminContext dataProvider={dataProvider}>
50+
<CoreAdminUI>
51+
<Resource name="posts" show={PostShow} />
52+
</CoreAdminUI>
53+
</CoreAdminContext>
5654
);
5755
```
5856

@@ -73,47 +71,51 @@ const App = () => (
7371

7472
## `children`
7573

76-
`<ShowBase>` renders its children wrapped by a `RecordContext`, so you can use any component that depends on such a context to be defined - including all [Field components](./Fields.md).
74+
`<ShowBase>` renders its children wrapped by a `RecordContext`, so you can use any component that depends on such a context to be defined.
7775

78-
For instance, to display several fields in a single line, you can use Material UI’s `<Grid>` component:
76+
For instance, to display several fields in a grid layout:
7977

80-
{% raw %}
8178
```jsx
82-
import { ShowBase, TextField, DateField, ReferenceField, WithRecord } from 'react-admin';
83-
import { Grid } from '@mui/material';
84-
import StarIcon from '@mui/icons-material/Star';
79+
import { ShowBase, ReferenceFieldBase, WithRecord } from 'ra-core';
80+
import { TextField } from './TextField';
81+
import { DateField } from './DateField';
8582

8683
const BookShow = () => (
8784
<ShowBase>
88-
<Grid container spacing={2} sx={{ margin: 2 }}>
89-
<Grid item xs={12} sm={6}>
85+
<div style={{ display: 'grid', gridTemplateColumns: 'repeat(2, 1fr)', gap: '1rem', margin: '1rem' }}>
86+
<div>
9087
<TextField label="Title" source="title" />
91-
</Grid>
92-
<Grid item xs={12} sm={6}>
93-
<ReferenceField label="Author" source="author_id" reference="authors">
88+
</div>
89+
<div>
90+
<ReferenceFieldBase label="Author" source="author_id" reference="authors">
9491
<TextField source="name" />
95-
</ReferenceField>
96-
</Grid>
97-
<Grid item xs={12} sm={6}>
92+
</ReferenceFieldBase>
93+
</div>
94+
<div>
9895
<DateField label="Publication Date" source="published_at" />
99-
</Grid>
100-
<Grid item xs={12} sm={6}>
101-
<WithRecord label="Rating" render={record => <>
102-
{[...Array(record.rating)].map((_, index) => <StarIcon key={index} />)}
103-
</>} />
104-
</Grid>
105-
</Grid>
96+
</div>
97+
<div>
98+
<WithRecord render={record => (
99+
<span>
100+
{record.rating >= 1 ? '' : ''}
101+
{record.rating >= 2 ? '' : ''}
102+
{record.rating >= 3 ? '' : ''}
103+
{record.rating >= 4 ? '' : ''}
104+
{record.rating >= 5 ? '' : ''}
105+
</span>
106+
)} />
107+
</div>
108+
</div>
106109
</ShowBase>
107110
);
108111
```
109-
{% endraw %}
110112

111113
## `disableAuthentication`
112114

113115
By default, the `<ShowBase>` component will automatically redirect the user to the login page if the user is not authenticated. If you want to disable this behavior and allow anonymous access to a show page, set the `disableAuthentication` prop to `true`.
114116

115117
```jsx
116-
import { ShowBase } from 'react-admin';
118+
import { ShowBase } from 'ra-core';
117119

118120
const PostShow = () => (
119121
<ShowBase disableAuthentication>
@@ -127,7 +129,7 @@ const PostShow = () => (
127129
By default, `<ShowBase>` deduces the identifier of the record to show from the URL path. So under the `/posts/123/show` path, the `id` prop will be `123`. You may want to force a different identifier. In this case, pass a custom `id` prop.
128130

129131
```jsx
130-
import { ShowBase } from 'react-admin';
132+
import { ShowBase } from 'ra-core';
131133

132134
export const PostShow = () => (
133135
<ShowBase id="123">
@@ -143,7 +145,7 @@ export const PostShow = () => (
143145
By default, `<ShowBase>` renders nothing while checking for authentication and permissions. You can provide your own component via the `loading` prop:
144146

145147
```jsx
146-
import { ShowBase } from 'react-admin';
148+
import { ShowBase } from 'ra-core';
147149

148150
export const PostShow = () => (
149151
<ShowBase loading={<p>Checking for permissions...</p>}>
@@ -157,7 +159,7 @@ export const PostShow = () => (
157159
By default, `<ShowBase>` renders nothing when there is no connectivity and the record hasn't been cached yet. You can provide your own component via the `offline` prop:
158160

159161
```jsx
160-
import { ShowBase } from 'react-admin';
162+
import { ShowBase } from 'ra-core';
161163

162164
export const PostShow = () => (
163165
<ShowBase offline={<p>No network. Could not load the post.</p>}>
@@ -169,7 +171,7 @@ export const PostShow = () => (
169171
**Tip**: If the record is in the Tanstack Query cache but you want to warn the user that they may see an outdated version, you can use the `<IsOffline>` component:
170172

171173
```jsx
172-
import { ShowBase, IsOffline } from 'react-admin';
174+
import { ShowBase, IsOffline } from 'ra-core';
173175

174176
export const PostShow = () => (
175177
<ShowBase offline={<p>No network. Could not load the post.</p>}>
@@ -191,7 +193,7 @@ You can override this behavior and pass custom side effects by providing a custo
191193

192194
```jsx
193195
import * as React from 'react';
194-
import { useNotify, useRefresh, useRedirect, ShowBase, SimpleShowLayout } from 'react-admin';
196+
import { useNotify, useRefresh, useRedirect, ShowBase } from 'ra-core';
195197

196198
const PostShow = () => {
197199
const notify = useNotify();
@@ -206,15 +208,13 @@ const PostShow = () => {
206208

207209
return (
208210
<ShowBase queryOptions={{ onError }}>
209-
<SimpleShowLayout>
210211
...
211-
</SimpleShowLayout>
212212
</ShowBase>
213213
);
214214
}
215215
```
216216

217-
The `onError` function receives the error from the dataProvider call (`dataProvider.getOne()`), which is a JavaScript Error object (see [the dataProvider documentation for details](./DataProviderWriting.md#error-format)).
217+
The `onError` function receives the error from the dataProvider call (`dataProvider.getOne()`), which is a JavaScript Error object (see [the dataProvider documentation for details](../data-fetching/DataProviderWriting.md#error-format)).
218218

219219
The default `onError` function is:
220220

@@ -230,9 +230,8 @@ The default `onError` function is:
230230

231231
Alternatively, you can pass a `render` function prop instead of children. This function will receive the `ShowContext` as argument.
232232

233-
{% raw %}
234233
```jsx
235-
import { ShowBase, TextField, DateField, ReferenceField, WithRecord } from 'react-admin';
234+
import { ShowBase } from 'ra-core';
236235

237236
const BookShow = () => (
238237
<ShowBase render={({ isPending, error, record }) => {
@@ -251,14 +250,13 @@ const BookShow = () => (
251250
}}/>
252251
);
253252
```
254-
{% endraw %}
255253

256254
## `resource`
257255

258256
By default, `<ShowBase>` operates on the current `ResourceContext` (defined at the routing level), so under the `/posts/1/show` path, the `resource` prop will be `posts`. You may want to force a different resource. In this case, pass a custom `resource` prop, and it will override the `ResourceContext` value.
259257

260258
```jsx
261-
import { ShowBase } from 'react-admin';
259+
import { ShowBase } from 'ra-core';
262260

263261
export const UsersShow = () => (
264262
<ShowBase resource="users">
@@ -271,23 +269,22 @@ export const UsersShow = () => (
271269

272270
## Security
273271

274-
The `<ShowBase>` component requires authentication and will redirect anonymous users to the login page. If you want to allow anonymous access, use the [`disableAuthentication`](./Show.md#disableauthentication) prop.
272+
The `<ShowBase>` component requires authentication and will redirect anonymous users to the login page. If you want to allow anonymous access, use the [`disableAuthentication`](#disableauthentication) prop.
275273

276-
If your `authProvider` implements [Access Control](./Permissions.md#access-control), `<ShowBase>` will only render if the user has the "show" access to the related resource.
274+
If your `authProvider` implements [Access Control](../security/Permissions.md#access-control), `<ShowBase>` will only render if the user has the "show" access to the related resource.
277275

278276
For instance, for the `<PostShow>`page below:
279277

280278
```tsx
281-
import { ShowBase, SimpleShowLayout, TextField } from 'react-admin';
279+
import { ShowBase } from 'ra-core';
280+
import { TextField } from './TextField';
282281

283282
// Resource name is "posts"
284283
const PostShow = () => (
285284
<ShowBase>
286-
<SimpleShowLayout>
287-
<TextField source="title" />
288-
<TextField source="author" />
289-
<TextField source="published_at" />
290-
</SimpleShowLayout>
285+
<TextField source="title" />
286+
<TextField source="author" />
287+
<TextField source="published_at" />
291288
</ShowBase>
292289
);
293290
```
@@ -298,6 +295,6 @@ const PostShow = () => (
298295
{ action: "show", resource: "posts" }
299296
```
300297

301-
Users without access will be redirected to the [Access Denied page](./Admin.md#accessdenied).
298+
Users without access will be redirected to the [Access Denied page](../app-configuration/CoreAdminUI.md#accessdenied).
302299

303-
**Note**: Access control is disabled when you use [the `disableAuthentication` prop](./Show.md#disableauthentication).
300+
**Note**: Access control is disabled when you use [the `disableAuthentication` prop](#disableauthentication).

0 commit comments

Comments
 (0)