Skip to content

Commit eb3b227

Browse files
docs: document ListViewServerProps/ClientProps and custom list view component patterns (#15970)
Fixes #15416 - adds banner on custom views to state props are for root views only - add props table for list view props - adds default list view section and example - adds slot props section --- - To see the specific tasks where the Asana app for GitHub is being used, see below: - https://app.asana.com/0/0/1213860518113230 --------- Co-authored-by: Patrik Kozak <35232443+PatrikKozak@users.noreply.github.com>
1 parent 329090c commit eb3b227

2 files changed

Lines changed: 115 additions & 11 deletions

File tree

docs/custom-components/custom-views.mdx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -138,9 +138,11 @@ Your Custom Views will be provided with the following props:
138138
| `payload` | The [Payload](../local-api/overview) class. |
139139

140140
<Banner type="warning">
141-
**Note:** Some views may receive additional props, such as [Collection
142-
Views](#collection-views) and [Global Views](#global-views). See the relevant
143-
section for more details.
141+
**Note:** The props above apply to **Root Views** and any view added at the
142+
root level. Collection List Views and Collection/Global Edit Views receive a
143+
different set of props specific to their context. See [List View
144+
Props](./list-view#props) and [Document Views](./document-views) for their
145+
respective prop references.
144146
</Banner>
145147

146148
Here is an example of a Custom View component:

docs/custom-components/list-view.mdx

Lines changed: 110 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,23 +18,25 @@ The List View can be swapped out in its entirety for a Custom View, or it can be
1818

1919
## Custom List View
2020

21-
To swap out the entire List View with a [Custom View](./custom-views), use the `admin.components.views.list` property in your [Payload Config](../configuration/overview):
21+
To swap out the entire List View with a [Custom View](./custom-views), use the `admin.components.views.list` property in your [Collection Config](../configuration/collections):
2222

23-
```tsx
24-
import { buildConfig } from 'payload'
23+
```ts
24+
import type { CollectionConfig } from 'payload'
2525

26-
const config = buildConfig({
26+
export const MyCollection: CollectionConfig = {
2727
// ...
2828
admin: {
2929
components: {
3030
views: {
3131
// highlight-start
32-
list: '/path/to/MyCustomListView',
32+
list: {
33+
Component: '/path/to/MyCustomListView',
34+
},
3335
// highlight-end
3436
},
3537
},
3638
},
37-
})
39+
}
3840
```
3941

4042
Here is an example of a custom List View:
@@ -44,7 +46,6 @@ Here is an example of a custom List View:
4446
```tsx
4547
import React from 'react'
4648
import type { ListViewServerProps } from 'payload'
47-
import { DefaultListView } from '@payloadcms/ui'
4849

4950
export function MyCustomServerListView(props: ListViewServerProps) {
5051
return <div>This is a custom List View (Server)</div>
@@ -63,14 +64,115 @@ export function MyCustomClientListView(props: ListViewClientProps) {
6364
}
6465
```
6566

66-
_For details on how to build Custom Views, including all available props, see [Building Custom Views](./custom-views#building-custom-views)._
67+
### Props
68+
69+
Custom List Views receive a different set of props than other views because Payload pre-fetches data and pre-renders components before passing the results to your component. The exact props available depend on whether you are building a Server or Client Component.
70+
71+
#### Server Component Props (`ListViewServerProps`)
72+
73+
Server components receive all [client props](#client-component-props-listviewclientprops) plus the following additional server-only data:
74+
75+
| Prop | Description |
76+
| ---------------------- | ------------------------------------------------------------------------------------- |
77+
| `collectionConfig` | The sanitized [Collection Config](../configuration/collections) for this collection. |
78+
| `data` | The paginated query result, including `docs`, `totalDocs`, `page`, `totalPages`, etc. |
79+
| `limit` | The number of documents displayed per page. |
80+
| `listPreferences` | Saved user preferences for this list (columns, sort, etc.). |
81+
| `listSearchableFields` | The fields configured as searchable in the collection's admin options. |
82+
| `i18n` | The [i18n](../configuration/i18n) object. |
83+
| `locale` | The active locale, if localization is enabled. |
84+
| `params` | Route parameters from the Next.js dynamic route. |
85+
| `payload` | The [Payload](../local-api/overview) class. |
86+
| `permissions` | The current user's permissions. |
87+
| `searchParams` | URL search parameters (page, sort, where, limit, etc.). |
88+
| `user` | The currently authenticated user. |
89+
90+
#### Client Component Props (`ListViewClientProps`)
91+
92+
| Prop | Description |
93+
| ----------------------- | ------------------------------------------------------------------------------------------------- |
94+
| `collectionSlug` | The slug of the collection being displayed. |
95+
| `columnState` | An array of column definitions describing the current table columns and their state. |
96+
| `disableBulkDelete` | When `true`, hides the bulk delete action. |
97+
| `disableBulkEdit` | When `true`, hides the bulk edit action. |
98+
| `disableQueryPresets` | When `true`, disables the query presets feature. |
99+
| `enableRowSelections` | When `true`, enables checkboxes on each row for bulk actions. |
100+
| `hasCreatePermission` | Whether the current user has permission to create new documents. |
101+
| `hasDeletePermission` | Whether the current user has permission to delete documents. |
102+
| `hasTrashPermission` | Whether the current user has permission to use the trash. |
103+
| `newDocumentURL` | The URL to navigate to when creating a new document. |
104+
| `renderedFilters` | A `Map<string, React.ReactNode>` of pre-rendered filter components, keyed by field path. |
105+
| `resolvedFilterOptions` | A `Map<string, ResolvedFilterOptions>` of resolved options for relationship filter fields. |
106+
| `viewType` | The current view type identifier. |
107+
| `Table` | A pre-rendered React node of the documents table. Pass this through to render the built-in table. |
108+
| `AfterList` | Slot for components rendered after the list. |
109+
| `AfterListTable` | Slot for components rendered after the table. |
110+
| `BeforeList` | Slot for components rendered before the list. |
111+
| `BeforeListTable` | Slot for components rendered before the table. |
112+
| `Description` | Slot for the collection description component. |
113+
| `listMenuItems` | Slot for custom items rendered in the list controls menu. |
114+
115+
<Banner type="info">
116+
**Note:** Custom List Views are rendered inside a `ListQueryProvider`. This
117+
means any Client Component in the tree can call `useListQuery()` from
118+
`@payloadcms/ui` to read or update the current query state (search term, sort,
119+
page, filters, etc.).
120+
</Banner>
121+
122+
### Using DefaultListView
123+
124+
If you want to keep Payload's built-in table and controls but add your own layout around them, import and render `DefaultListView` from `@payloadcms/ui`:
125+
126+
```tsx
127+
'use client'
128+
import React from 'react'
129+
import type { ListViewClientProps } from 'payload'
130+
import { DefaultListView } from '@payloadcms/ui'
131+
132+
export function MyCustomClientListView(props: ListViewClientProps) {
133+
return (
134+
<div>
135+
<h1>My Custom Header</h1>
136+
<DefaultListView {...props} />
137+
</div>
138+
)
139+
}
140+
```
67141

68142
## Custom Components
69143

70144
In addition to swapping out the entire List View with a [Custom View](./custom-views), you can also override individual components. This allows you to customize specific parts of the List View without swapping out the entire view for your own.
71145

72146
To override List View components for a Collection, use the `admin.components` property in your [Collection Config](../configuration/collections):
73147

148+
### Slot Props
149+
150+
All slot components (`beforeList`, `beforeListTable`, `afterList`, `afterListTable`) receive the same base set of client props plus additional server-only props in Server Components:
151+
152+
**Client Props** (`BeforeListClientProps`, `AfterListClientProps`, etc.):
153+
154+
| Prop | Description |
155+
| --------------------- | ---------------------------------------------------------------- |
156+
| `collectionSlug` | The slug of the collection being displayed. |
157+
| `hasCreatePermission` | Whether the current user has permission to create new documents. |
158+
| `hasDeletePermission` | Whether the current user has permission to delete documents. |
159+
| `hasTrashPermission` | Whether the current user has permission to use the trash. |
160+
| `newDocumentURL` | The URL to navigate to when creating a new document. |
161+
162+
**Additional Server-Only Props** (`BeforeListServerProps`, `AfterListServerProps`, etc.):
163+
164+
| Prop | Description |
165+
| ---------------------- | ------------------------------------------------------------------------------------- |
166+
| `collectionConfig` | The sanitized [Collection Config](../configuration/collections) for this collection. |
167+
| `data` | The paginated query result, including `docs`, `totalDocs`, `page`, `totalPages`, etc. |
168+
| `limit` | The number of documents displayed per page. |
169+
| `listPreferences` | Saved user preferences for this list (columns, sort, etc.). |
170+
| `listSearchableFields` | The fields configured as searchable in the collection's admin options. |
171+
| `i18n` | The [i18n](../configuration/i18n) object. |
172+
| `payload` | The [Payload](../local-api/overview) class. |
173+
| `permissions` | The current user's permissions. |
174+
| `user` | The currently authenticated user. |
175+
74176
```ts
75177
import type { CollectionConfig } from 'payload'
76178

0 commit comments

Comments
 (0)