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

[docs] New page presenting the apiRef #5273

Merged
merged 20 commits into from
Jul 19, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions docs/data/data-grid/api-object/UseGridApiContext.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import * as React from 'react';
import Box from '@mui/material/Box';
import Button from '@mui/material/Button';
import { DataGrid, GridToolbarContainer, useGridApiContext } from '@mui/x-data-grid';
import { useDemoData } from '@mui/x-data-grid-generator';

const CustomToolbar = () => {
const apiRef = useGridApiContext();

const handleGoToPage1 = () => apiRef.current.setPage(1);

return (
<GridToolbarContainer>
<Button onClick={handleGoToPage1}>Go to page 1</Button>
</GridToolbarContainer>
);
};

export default function UseGridApiContext() {
const { data } = useDemoData({
dataSet: 'Commodity',
rowLength: 100,
maxColumns: 6,
});

return (
<Box sx={{ height: 400, width: '100%' }}>
<DataGrid
{...data}
components={{
Toolbar: CustomToolbar,
}}
pageSize={10}
/>
</Box>
);
}
37 changes: 37 additions & 0 deletions docs/data/data-grid/api-object/UseGridApiContext.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import * as React from 'react';
import Box from '@mui/material/Box';
import Button from '@mui/material/Button';
import { DataGrid, GridToolbarContainer, useGridApiContext } from '@mui/x-data-grid';
import { useDemoData } from '@mui/x-data-grid-generator';

const CustomToolbar = () => {
const apiRef = useGridApiContext();

const handleGoToPage1 = () => apiRef.current.setPage(1);

return (
<GridToolbarContainer>
<Button onClick={handleGoToPage1}>Go to page 1</Button>
</GridToolbarContainer>
);
};

export default function UseGridApiContext() {
const { data } = useDemoData({
dataSet: 'Commodity',
rowLength: 100,
maxColumns: 6,
});

return (
<Box sx={{ height: 400, width: '100%' }}>
<DataGrid
{...data}
components={{
Toolbar: CustomToolbar,
}}
pageSize={10}
/>
</Box>
);
}
7 changes: 7 additions & 0 deletions docs/data/data-grid/api-object/UseGridApiContext.tsx.preview
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<DataGrid
{...data}
components={{
Toolbar: CustomToolbar,
}}
pageSize={10}
/>
27 changes: 27 additions & 0 deletions docs/data/data-grid/api-object/UseGridApiRef.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import * as React from 'react';
import Button from '@mui/material/Button';
import Stack from '@mui/material/Stack';
import Box from '@mui/material/Box';
import { DataGridPro, useGridApiRef } from '@mui/x-data-grid-pro';
import { useDemoData } from '@mui/x-data-grid-generator';

export default function UseGridApiRef() {
const { data } = useDemoData({
dataSet: 'Commodity',
rowLength: 100,
maxColumns: 6,
});

const apiRef = useGridApiRef();

const handleGoToPage1 = () => apiRef.current.setPage(1);

return (
<Stack spacing={2} sx={{ width: '100%' }} alignItems="flex-start">
flaviendelangle marked this conversation as resolved.
Show resolved Hide resolved
<Button onClick={handleGoToPage1}>Go to page 1</Button>
<Box sx={{ height: 400, width: '100%' }}>
<DataGridPro {...data} apiRef={apiRef} pagination pageSize={10} />
</Box>
</Stack>
);
}
27 changes: 27 additions & 0 deletions docs/data/data-grid/api-object/UseGridApiRef.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import * as React from 'react';
import Button from '@mui/material/Button';
import Stack from '@mui/material/Stack';
import Box from '@mui/material/Box';
import { DataGridPro, useGridApiRef } from '@mui/x-data-grid-pro';
import { useDemoData } from '@mui/x-data-grid-generator';

export default function UseGridApiRef() {
const { data } = useDemoData({
dataSet: 'Commodity',
rowLength: 100,
maxColumns: 6,
});

const apiRef = useGridApiRef();

const handleGoToPage1 = () => apiRef.current.setPage(1);

return (
<Stack spacing={2} sx={{ width: '100%' }} alignItems="flex-start">
<Button onClick={handleGoToPage1}>Go to page 1</Button>
<Box sx={{ height: 400, width: '100%' }}>
<DataGridPro {...data} apiRef={apiRef} pagination pageSize={10} />
</Box>
</Stack>
);
}
4 changes: 4 additions & 0 deletions docs/data/data-grid/api-object/UseGridApiRef.tsx.preview
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<Button onClick={handleGoToPage1}>Go to page 1</Button>
<Box sx={{ height: 400, width: '100%' }}>
<DataGridPro {...data} apiRef={apiRef} pagination pageSize={10} />
</Box>
81 changes: 81 additions & 0 deletions docs/data/data-grid/api-object/api-object.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
---
title: Data Grid - API object
---

# Data grid - API object

<p class="description">Interact with the grid using its api.</p>
flaviendelangle marked this conversation as resolved.
Show resolved Hide resolved

## What is the API object ?
flaviendelangle marked this conversation as resolved.
Show resolved Hide resolved

The API object is an interface containing the state and all the methods needed to interact imperatively with the grid.
Copy link
Member Author

Choose a reason for hiding this comment

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

This section is probably a little succinct for now

flaviendelangle marked this conversation as resolved.
Show resolved Hide resolved

You can find the list of all the available API methods on the [GridApi page](/x/api/data-grid/grid-api/).
flaviendelangle marked this conversation as resolved.
Show resolved Hide resolved

:::warning
All the methods prefixed by `unstable_` are private.
Copy link
Member

Choose a reason for hiding this comment

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

I thought unstable_ was used more in the direction of an alpha version of an API method.
Would it make sense to consider private_ in these cases?

Copy link
Member Author

Choose a reason for hiding this comment

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

Right now it's the same prefix
And right now no unstable_ is documented in a markdown file, even if theoretically we could indeed be used for features in alpha phase.

I suggest we leave it as is (with rewording if you want).
And we discuss if it's worth splitting in two prefixes in another PR.

They should not be used by third party user and can be removed / renamed / reworked at any time.
flaviendelangle marked this conversation as resolved.
Show resolved Hide resolved
flaviendelangle marked this conversation as resolved.
Show resolved Hide resolved

flaviendelangle marked this conversation as resolved.
Show resolved Hide resolved
If you need to use a private method, please open a GitHub issue describing your use case.
We will help you to achieve the same goal with public methods, or we will discuss making this specific method public.
:::

## How to use the API object ?
flaviendelangle marked this conversation as resolved.
Show resolved Hide resolved
flaviendelangle marked this conversation as resolved.
Show resolved Hide resolved

The API object is accessible through the `apiRef` variable.
Depending on where you are trying to access this variable, you will have to use either `useGridApiContext` or `useGridApiRef`.

### Use it inside the grid

If you need to access the api object inside component slots or inside renders (e.g: `renderCell`, `renderHeader`),
flaviendelangle marked this conversation as resolved.
Show resolved Hide resolved
you can use the `useGridApiContext` hook.

```tsx
const apiRef = useGridApiContext();

<Button onClick={() => apiRef.current.setPage(1)}>Go to page 1</Button>;
```

:::info
You don't have to initialize the API object using `useGridApiRef` to be able to it inside the grid components.
flaviendelangle marked this conversation as resolved.
Show resolved Hide resolved
:::

{{"demo": "UseGridApiContext.js", "bg": "inline", "defaultCodeOpen": false}}

### Use it outside the grid [<span class="plan-pro"></span>](https://mui.com/store/items/mui-x-pro/)

When using the API object outside the grid components, you need to initialize it using the `useGridApiRef` hook.
You can then pass it to the `apiRef` prop of the grid.

```tsx
const apiRef = useGridApiRef();
flaviendelangle marked this conversation as resolved.
Show resolved Hide resolved

<div>
<Button onClick={() => apiRef.current.setPage(1)}>Go to page 1</Button>
<DataGridPro apiRef={apiRef} {...other} />
</div>;
```

:::warning
The API object will be populated by the various plugins of the grid during the 1st render of our component.
flaviendelangle marked this conversation as resolved.
Show resolved Hide resolved
If you try to use it in the 1st render of your component, all the methods will be undefined.
flaviendelangle marked this conversation as resolved.
Show resolved Hide resolved
:::

{{"demo": "UseGridApiRef.js", "bg": "inline", "defaultCodeOpen": false}}

## Common use cases

### Retrieve data from the state

You can find a detailed example in the [State page](/x/react-data-grid/state/#access-the-state).

### Listen to grid events
Copy link
Member

Choose a reason for hiding this comment

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

Another suggestion is #5109

Copy link
Member Author

Choose a reason for hiding this comment

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

What would you like to document exactly here ?

Copy link
Member

Choose a reason for hiding this comment

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

It could be an example of how to save the state before unmounting the grid. Using the cleanup of useEffect doesn't work in this case.

Copy link
Member Author

Choose a reason for hiding this comment

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

Would you agree to add a section on the State page in a follow up PR and link it here ?

Copy link
Member

Choose a reason for hiding this comment

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

Yes.


You can find a detailed example in the [Events page](/x/react-data-grid/events/#subscribing-to-events).

## API

- [GridApi](/x/api/data-grid/grid-api/)
- [DataGrid](/x/api/data-grid/data-grid/)
- [DataGridPro](/x/api/data-grid/data-grid-pro/)
- [DataGridPremium](/x/api/data-grid/data-grid-premium/)
6 changes: 2 additions & 4 deletions docs/data/data-grid/components/components.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,8 @@ The grid exposes two hooks to help you to access the grid data while overriding

They can be used as below:

- `useGridApiContext`: returns the `apiRef`.
- `useGridSelector`: returns the result of a selector on the current state.

More details about the selectors in the [State page](/x/react-data-grid/state/#access-the-state)
- `useGridApiContext`: returns the `apiRef` (more details in the [API object page](/x/react-data-grid/api-object/#use-it-outside-the-grid)).
- `useGridSelector`: returns the result of a selector on the current state (more details in the [State page](/x/react-data-grid/state/#access-the-state)).

```tsx
function CustomPagination() {
Expand Down
16 changes: 14 additions & 2 deletions docs/data/pages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@ const pages: MuiPage[] = [
{ pathname: '/x/react-data-grid/filtering' },
{ pathname: '/x/react-data-grid/pagination' },
{ pathname: '/x/react-data-grid/selection' },
{ pathname: '/x/react-data-grid/events' },
{ pathname: '/x/react-data-grid/state' },
{ pathname: '/x/react-data-grid/export' },
{ pathname: '/x/react-data-grid/components' },
{ pathname: '/x/react-data-grid/style' },
Expand All @@ -64,6 +62,20 @@ const pages: MuiPage[] = [
{ pathname: '/x/react-data-grid/pivoting', title: 'Pivoting 🚧', plan: 'premium' },
],
},
{
title: 'Advanced',
pathname: '/x/react-data-grid/api-object',
scopePathnames: [
'/x/react-data-grid/api-object',
'/x/react-data-grid/events',
'/x/react-data-grid/state',
],
children: [
{ pathname: '/x/react-data-grid/api-object', title: 'API object' },
{ pathname: '/x/react-data-grid/events' },
{ pathname: '/x/react-data-grid/state' },
],
},
{
pathname: '/x/api/data-grid',
title: 'API Reference',
Expand Down
11 changes: 11 additions & 0 deletions docs/pages/x/react-data-grid/api-object.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import * as React from 'react';
import MarkdownDocs from 'docs/src/modules/components/MarkdownDocs';
import {
demos,
docs,
demoComponents,
} from 'docsx/data/data-grid/api-object/api-object.md?@mui/markdown';

export default function Page() {
return <MarkdownDocs demos={demos} docs={docs} demoComponents={demoComponents} disableAd />;
}