Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add WithListContext component #8917

Merged
merged 5 commits into from
May 17, 2023
Merged

Add WithListContext component #8917

merged 5 commits into from
May 17, 2023

Conversation

fzaninotto
Copy link
Member

@fzaninotto fzaninotto commented May 16, 2023

Problem

Reading data from a ListContext requires creating another component, as it's only available via a hook.

Solution

A render prop version of useListContext called WithListContext

import { List, Datagrid, TextField, ReferenceArrayField, WithListContext } from 'react-admin';
import { Chip, Stack } from '@mui/material';

const BookList = () => (
    <List>
        <Datagrid>
            <TextField source="id" />
            <TextField source="title" />
            <ReferenceArrayField label="Tags" reference="tags" source="tag_ids">
                <WithListContext render={({ isLoading, data }) => (
                    !isLoading && (
                        <Stack direction="row" spacing={1}>
                            {data.map(tag => (
                                <Chip key={tag.id} label={tag.name} />
                            ))}
                        </Stack>
                    )
                )} />
            </ReferenceArrayField>
        </Datagrid>
    </List>
);

This also facilitates the use of charts in react-admin:

import { ListBase, WithListContext } from 'react-admin';
import { LineChart, Line, XAxis, YAxis, CartesianGrid } from 'recharts';

const FruitChart = () => (
    <ListBase resource="fruits" disableSyncWithLocation perPage={100}>
        <WithListContext
            render={({ data }) => (
                <LineChart width={700} height={300} data={data}>
                    <XAxis dataKey="date" />
                    <YAxis />
                    <CartesianGrid stroke="#eee" strokeDasharray="5 5" />
                    <Line dataKey="apples" type="monotone" stroke="#8884d8" />
                    <Line dataKey="blueberries" type="monotone" stroke="#82ca9d" />
                    <Line dataKey="carrots" type="monotone" stroke="#ffc658" />
                </LineChart>
            )}
        />
    </ListBase>
);

WithListContext-chart

  • Create the component
  • Create a storybook
  • Document it
  • Add mentions of it in other parts of the documentation

docs/WithListContext.md Outdated Show resolved Hide resolved

## `render`

A function which will be called with the current [`ListContext`](./useListContext.md) as argument. It should return a React element.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
A function which will be called with the current [`ListContext`](./useListContext.md) as argument. It should return a React element.
A function which will be called with the current [`ListContext`](./useListContext.md) as argument. It should return a React node.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope, it's a React Element.


Whenever you use a react-admin component fetching a list of records, react-admin stores it in a [`ListContext`](./useListContext.md). This means that `<WithListContext>` works out of the box:

- in descendants of the [`<List>`](./ListBase.md), [`<InfiniteList>`](./InfiniteList.md), and [`<ListBase>`](./ListBase.md) components
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- in descendants of the [`<List>`](./ListBase.md), [`<InfiniteList>`](./InfiniteList.md), and [`<ListBase>`](./ListBase.md) components
- as a child of the [`<List>`](./ListBase.md), [`<InfiniteList>`](./InfiniteList.md), and [`<ListBase>`](./ListBase.md) components

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I find the existing sentence confusing

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your suggestion leads to another confusion: that the component would only work when used as child and not as grandchild.

I think that "descendants" is the right term.

Co-authored-by: Gildas Garcia <1122076+djhi@users.noreply.github.com>
@djhi djhi merged commit fd141cd into next May 17, 2023
8 checks passed
@djhi djhi deleted the WithListContext branch May 17, 2023 11:34
@djhi djhi added this to the 4.11.0 milestone May 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
RFR Ready For Review
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants