Skip to content

Commit 3b1e3fb

Browse files
committed
update list (end)
1 parent 5685546 commit 3b1e3fb

File tree

6 files changed

+100
-132
lines changed

6 files changed

+100
-132
lines changed

docs_headless/src/content/docs/list/WithListContext.md

Lines changed: 30 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
---
2-
layout: default
3-
title: "WithListContext"
2+
title: "<WithListContext>"
43
storybook_path: ra-core-controller-list-withlistcontext--basic
54
---
65

7-
# `<WithListContext>`
8-
96
`<WithListContext>` executes its `render` function using the current `ListContext` as parameter. It's the render prop version of [the `useListContext` hook](./useListContext.md).
107

118
Use it to render a list of records already fetched.
@@ -14,32 +11,34 @@ Use it to render a list of records already fetched.
1411

1512
The most common use case for `<WithListContext>` is to build a custom list view on-the-fly, without creating a new component, in a place where records are available inside a `ListContext`.
1613

17-
For instance, a list of book tags fetched via [`<ReferenceArrayField>`](./ReferenceArrayField.md):
14+
For instance, a list of book tags fetched via [`<ReferenceArrayFieldBase>`](../fields/ReferenceArrayFieldBase.md):
1815

1916
```jsx
20-
import { List, DataTable, ReferenceArrayField, WithListContext } from 'react-admin';
21-
import { Chip, Stack } from '@mui/material';
17+
import { ListBase, WithListContext, ReferenceArrayFieldBase } from 'ra-core';
18+
import { DataTable } from './components';
2219

2320
const BookList = () => (
24-
<List>
21+
<ListBase>
2522
<DataTable>
2623
<DataTable.Col source="id" />
2724
<DataTable.Col source="title" />
2825
<DataTable.Col source="tag_ids" label="Tags">
29-
<ReferenceArrayField reference="tags" source="tag_ids">
26+
<ReferenceArrayFieldBase reference="tags" source="tag_ids">
3027
<WithListContext render={({ isPending, data }) => (
3128
!isPending && (
32-
<Stack direction="row" spacing={1}>
29+
<div className="stack">
3330
{data.map(tag => (
34-
<Chip key={tag.id} label={tag.name} />
31+
<span key={tag.id} className="chip">
32+
{tag.name}
33+
</span>
3534
))}
36-
</Stack>
35+
</div>
3736
)
3837
)} />
39-
</ReferenceArrayField>
38+
</ReferenceArrayFieldBase>
4039
</DataTable.Col>
4140
</DataTable>
42-
</List>
41+
</ListBase>
4342
);
4443
```
4544

@@ -48,32 +47,35 @@ const BookList = () => (
4847
The equivalent with `useListContext` would require an intermediate component:
4948

5049
```jsx
51-
import { List, DataTable, ReferenceArrayField, WithListContext } from 'react-admin';
50+
import { ListBase, useListContext, ReferenceArrayFieldBase } from 'ra-core';
51+
import { DataTable } from './components';
5252

5353
const BookList = () => (
54-
<List>
54+
<ListBase>
5555
<DataTable>
5656
<DataTable.Col source="id" />
5757
<DataTable.Col source="title" />
5858
<DataTable.Col label="Tags" source="tag_ids">
59-
<ReferenceArrayField reference="tags" source="tag_ids">
59+
<ReferenceArrayFieldBase reference="tags" source="tag_ids">
6060
<TagList />
61-
</ReferenceArrayField>
61+
</ReferenceArrayFieldBase>
6262
</DataTable.Col>
6363
</DataTable>
64-
</List>
64+
</ListBase>
6565
);
6666

6767
const TagList = () => {
6868
const { isPending, data } = useListContext();
6969
return isPending
7070
? null
7171
: (
72-
<Stack direction="row" spacing={1}>
72+
<div className="stack">
7373
{data.map(tag => (
74-
<Chip key={tag.id} label={tag.name} />
74+
<span key={tag.id} className="chip">
75+
{tag.name}
76+
</span>
7577
))}
76-
</Stack>
78+
</div>
7779
);
7880
};
7981
```
@@ -141,22 +143,19 @@ As a reminder, the [`ListContext`](./useListContext.md) is an object with the fo
141143

142144
## Availability
143145

144-
Whenever you use a react-admin component to fetch a list of records, react-admin stores the data in a [`ListContext`](./useListContext.md). Consequently, `<WithListContext>` works in any component that is a descendant of:
146+
Whenever you use a ra-core component to fetch a list of records, ra-core stores the data in a [`ListContext`](./useListContext.md). Consequently, `<WithListContext>` works in any component that is a descendant of:
145147

146-
- the [`<List>`](./ListBase.md), [`<InfiniteList>`](./InfiniteList.md), and [`<ListBase>`](./ListBase.md) components
147-
- the [`<ArrayField>`](./ArrayField.md) component
148-
- the [`<ReferenceManyField>`](./ReferenceManyField.md) component
149-
- the [`<ReferenceArrayField>`](./ReferenceArrayField.md) component
148+
- the [`<ListBase>`](./ListBase.md) component
149+
- the [`<ReferenceArrayFieldBase>`](../fields/ReferenceArrayFieldBase.md) component
150150

151151
## Building a Chart
152152

153153
A common use case is to build a chart based on the list data. For instance, the following component fetches a list of fruit prices (using `<ListBase>`), and draws a line chart with the data using [Echarts](https://echarts.apache.org/en/index.html):
154154

155155
![Chart based on ListContext](../img/WithListContext-chart.png)
156156

157-
{% raw %}
158157
```jsx
159-
import { ListBase, WithListContext } from 'react-admin';
158+
import { ListBase, WithListContext } from 'ra-core';
160159
import * as echarts from 'echarts';
161160

162161
const FruitChart = () => (
@@ -216,18 +215,17 @@ const LineChart = ({ data }) => {
216215
return <div ref={chartRef} style={{ height: 300, width: 700 }} />;
217216
};
218217
```
219-
{% endraw %}
220218

221219
## Building a Refresh Button
222220

223221
Another use case is to create a button that refreshes the current list. As the [`ListContext`](./useListContext.md) exposes the `refetch` function, it's as simple as:
224222

225223
```jsx
226-
import { WithListContext } from 'react-admin';
224+
import { WithListContext } from 'ra-core';
227225

228226
const RefreshListButton = () => (
229227
<WithListContext render={({ refetch }) => (
230228
<button onClick={refetch}>Refresh</button>
231229
)} />
232230
);
233-
```
231+
```

docs_headless/src/content/docs/list/useList.md

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
---
2-
layout: default
32
title: "useList"
43
---
54

6-
# `useList`
7-
85
The `useList` hook allows to create a `ListContext` based on local data. `useList` creates callbacks for sorting, paginating, filtering, and selecting records from an array.
96

10-
Thanks to it, you can display your data inside a [`<DataTable>`](./DataTable.md), a [`<SimpleList>`](./SimpleList.md) or an [`<EditableDatagrid>`](./EditableDatagrid.md).
7+
Thanks to it, you can display your data inside your own list components.
118

129
## Usage
1310

@@ -17,8 +14,8 @@ Thanks to it, you can display your data inside a [`<DataTable>`](./DataTable.md)
1714
import {
1815
useList,
1916
ListContextProvider,
20-
DataTable,
21-
} from 'react-admin';
17+
} from 'ra-core';
18+
import { DataTable } from './DataTable';
2219

2320
const data = [
2421
{ id: 1, name: 'Arnold' },
@@ -42,7 +39,8 @@ const MyComponent = () => {
4239
If you use it with data coming from the `dataProvider`, don't forget to pass the `isPending` prop so that it only manipulates the data once it's available:
4340

4441
```jsx
45-
import { useGetList, useList } from 'react-admin';
42+
import { useGetList, useList, ListContextProvider } from 'ra-core';
43+
import { DataTable } from './DataTable';
4644

4745
const MyComponent = () => {
4846
const { data, isPending } = useGetList(
@@ -140,10 +138,11 @@ const { data } = useList({
140138

141139
## `isFetching`
142140

143-
This value ends up in the return value. It is used by list iterators (like `<DataTable>`) to know when to display a loading indicator.
141+
This value ends up in the return value. It is used by list iterators to know when to display a loading indicator.
144142

145143
```jsx
146-
import { useGetList, useList } from 'react-admin';
144+
import { useGetList, useList, ListContextProvider } from 'ra-core';
145+
import { DataTable } from './DataTable';
147146

148147
const MyComponent = () => {
149148
const { data, isFetching } = useGetList(
@@ -164,15 +163,15 @@ const MyComponent = () => {
164163

165164
## `isPending`
166165

167-
This value ends up in the return value. It is used by list iterators (like `<DataTable>`) to know when to display a loading indicator.
166+
This value ends up in the return value. It is used by list iterators to know when to display a loading indicator.
168167

169168
```jsx
170169
import {
171170
useGetList,
172171
useList,
173172
ListContextProvider,
174-
DataTable,
175-
} from 'react-admin';
173+
} from 'ra-core';
174+
import { DataTable } from './DataTable';
176175

177176
const MyComponent = () => {
178177
const { data, isPending } = useGetList(

docs_headless/src/content/docs/list/useListContext.md

Lines changed: 29 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,47 @@
11
---
2-
layout: default
32
title: "useListContext"
43
---
54

6-
# `useListContext`
7-
8-
Whenever react-admin displays a List, it creates a `ListContext` to store the list data, as well as filters, pagination, sort state, and callbacks to update them.
5+
Whenever ra-core displays a List, it creates a `ListContext` to store the list data, as well as filters, pagination, sort state, and callbacks to update them.
96

107
The `ListContext` is available to descendants of:
118

12-
- `<List>`,
13-
- `<ListGuesser>`,
149
- `<ListBase>`,
15-
- `<ReferenceArrayField>`,
16-
- `<ReferenceManyField>`
10+
- `<ReferenceArrayFieldBase>`,
11+
- `<ReferenceManyFieldBase>`
1712

18-
All descendant components can therefore access the list context, using the `useListContext` hook. As a matter of fact, react-admin's `<DataTable>`, `<FilterForm>`, and `<Pagination>` components all use the `useListContext` hook.
13+
All descendant components can therefore access the list context, using the `useListContext` hook.
1914

2015
## Usage
2116

22-
Call `useListContext` in a component, then use this component as a descendant of a `List` component.
17+
Call `useListContext` in a component, then use this component as a descendant of a list component.
2318

2419
```jsx
2520
// in src/posts/Aside.js
26-
import { Typography } from '@mui/material';
27-
import { useListContext } from 'react-admin';
21+
import { useListContext } from 'ra-core';
2822

2923
export const Aside = () => {
3024
const { data, isPending } = useListContext();
3125
if (isPending) return null;
3226
return (
3327
<div>
34-
<Typography variant="h6">Posts stats</Typography>
35-
<Typography variant="body2">
28+
<h6>Posts stats</h6>
29+
<p>
3630
Total views: {data.reduce((sum, post) => sum + post.views, 0)}
37-
</Typography>
31+
</p>
3832
</div>
3933
);
4034
};
4135

4236
// in src/posts/PostList.js
43-
import { List, DataTable } from 'react-admin';
37+
import { ListBase } from 'ra-core';
4438
import Aside from './Aside';
4539

4640
export const PostList = () => (
47-
<List aside={<Aside />}>
48-
<DataTable>
49-
<DataTable.Col source="id" />
50-
<DataTable.Col source="title" />
51-
<DataTable.Col source="views" />
52-
</DataTable>
53-
</List>
41+
<ListBase>
42+
<Aside />
43+
{/* My list content */}
44+
</ListBase>
5445
);
5546
```
5647

@@ -100,17 +91,16 @@ const {
10091
`useListContext` often forces you to create a new component just to access the list context. If you prefer a declarative approach based on render props, you can use [the `<WithListContext>` component](./WithListContext.md) instead:
10192

10293
```jsx
103-
import { WithListContext } from 'react-admin';
104-
import { Typography } from '@mui/material';
94+
import { WithListContext } from 'ra-core';
10595

10696
export const Aside = () => (
10797
<WithListContext render={({ data, isPending }) =>
10898
!isPending && (
10999
<div>
110-
<Typography variant="h6">Posts stats</Typography>
111-
<Typography variant="body2">
100+
<h6>Posts stats</h6>
101+
<p>
112102
Total views: {data.reduce((sum, post) => sum + post.views, 0)}
113-
</Typography>
103+
</p>
114104
</div>
115105
)} />
116106
);
@@ -128,7 +118,7 @@ You can use it to update the filters in a custom filter component:
128118

129119
```jsx
130120
import { useState } from 'react';
131-
import { useListContext } from 'react-admin';
121+
import { useListContext } from 'ra-core';
132122

133123
const CustomFilter = () => {
134124
const { filterValues, setFilters } = useListContext();
@@ -162,8 +152,7 @@ const CustomFilter = () => {
162152
The `useListContext` hook accepts a generic parameter for the record type:
163153

164154
```tsx
165-
import { Typography } from '@mui/material';
166-
import { List, DataTable, useListContext } from 'react-admin';
155+
import { ListBase, useListContext } from 'ra-core';
167156

168157
type Post = {
169158
id: number;
@@ -176,36 +165,30 @@ export const Aside = () => {
176165
if (isPending) return null;
177166
return (
178167
<div>
179-
<Typography variant="h6">Posts stats</Typography>
180-
<Typography variant="body2">
168+
<h6>Posts stats</h6>
169+
<p>
181170
{/* TypeScript knows that posts is of type Post[] */}
182171
Total views: {posts.reduce((sum, post) => sum + post.views, 0)}
183-
</Typography>
172+
</p>
184173
</div>
185174
);
186175
};
187176

188177
export const PostList = () => (
189-
<List aside={<Aside />}>
190-
<DataTable>
191-
<DataTable.Col source="id" />
192-
<DataTable.Col source="title" />
193-
<DataTable.Col source="views" />
194-
</DataTable>
195-
</List>
178+
<ListBase>
179+
<Aside />
180+
{/* My list content */}
181+
</ListBase>
196182
);
197183
```
198184

199185
## Recipes
200186

201187
You can find many usage examples of `useListContext` in the documentation, including:
202188

203-
- [Adding a Side Component with `<List actions>`](./List.md#aside)
204-
- [Building a Custom Actions Bar via `<List actions>`](./List.md#actions)
205-
- [Building a Custom Empty Page via `<List empty>`](./List.md#empty)
206189
- [Building a Custom Filter](./FilteringTutorial.md#building-a-custom-filter)
207190
- [Building a Custom Sort Control](./ListTutorial.md#building-a-custom-sort-control)
208191
- [Building a Custom Pagination Control](./ListTutorial.md#building-a-custom-pagination)
209-
- [Building a Custom Iterator](./ListTutorial.md#building-a-custom-iterator)
192+
- [Building a Custom List Layout](./ListTutorial.md#building-a-custom-list-layout)
210193

211-
**Tip**: [`<ReferenceManyField>`](./ReferenceManyField.md), as well as other relationship-related components, also implement a `ListContext`. That means you can use a `<DataTable>` of a `<Pagination>` inside these components!
194+
**Tip**: [`<ReferenceManyFieldBase>`](../fields/ReferenceManyFieldBase.md), as well as other relationship-related components, also implement a `ListContext`. That means you can use custom list components inside these components!

0 commit comments

Comments
 (0)