Skip to content

Commit 8fe5f04

Browse files
authored
feat: allow client components to also be used as custom collection views (#16312)
Allows client components to also be used as custom collection views, bringing it inline with internal views. A continuation of #16243
1 parent bc590bd commit 8fe5f04

6 files changed

Lines changed: 68 additions & 0 deletions

File tree

packages/next/src/views/Root/index.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,11 @@ export const RootPage = async ({
282282
clientProps: {
283283
browseByFolderSlugs,
284284
clientConfig,
285+
collectionSlug: collectionConfig?.slug,
286+
docID: routeParams.id,
285287
documentSubViewType,
288+
folderID,
289+
globalSlug: globalConfig?.slug,
286290
viewType,
287291
} satisfies AdminViewClientProps,
288292
Component: DefaultView.payloadComponent,

packages/payload/src/admin/views/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,11 @@ export type AdminViewConfig = {
3434
export type AdminViewClientProps = {
3535
browseByFolderSlugs?: SanitizedCollectionConfig['slug'][]
3636
clientConfig: ClientConfig
37+
collectionSlug?: SanitizedCollectionConfig['slug']
38+
docID?: number | string
3739
documentSubViewType?: DocumentSubViewTypes
40+
folderID?: number | string
41+
globalSlug?: SanitizedGlobalConfig['slug']
3842
viewType: ViewTypes
3943
}
4044

test/admin/collections/CustomCollectionView.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ export const CustomCollectionView: CollectionConfig = {
1212
path: '/grid',
1313
exact: true,
1414
},
15+
gridClient: {
16+
Component: '/components/views/CustomCollectionView/Client.js#CustomCollectionViewClient',
17+
path: '/grid-client',
18+
exact: true,
19+
},
1520
},
1621
},
1722
},
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
'use client'
2+
import type { AdminViewClientProps } from 'payload'
3+
4+
import { useConfig } from '@payloadcms/ui'
5+
import React from 'react'
6+
7+
import { customCollectionViewClientTitle } from '../../../shared.js'
8+
9+
export function CustomCollectionViewClient({ collectionSlug }: AdminViewClientProps) {
10+
const { getEntityConfig } = useConfig()
11+
12+
if (!collectionSlug) {
13+
return null
14+
}
15+
16+
const clientCollectionConfig = getEntityConfig({ collectionSlug })
17+
18+
if (!clientCollectionConfig) {
19+
return null
20+
}
21+
22+
const { labels } = clientCollectionConfig
23+
24+
return (
25+
<div
26+
style={{
27+
marginTop: 'calc(var(--base) * 2)',
28+
paddingLeft: 'var(--gutter-h)',
29+
paddingRight: 'var(--gutter-h)',
30+
}}
31+
>
32+
<h1 id="custom-collection-view-client-title">{customCollectionViewClientTitle}</h1>
33+
<p id="custom-collection-view-client-slug">{clientCollectionConfig.slug}</p>
34+
<p id="custom-collection-view-client-label">{String(labels?.plural)}</p>
35+
</div>
36+
)
37+
}

test/admin/e2e/general/e2e.spec.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -783,6 +783,22 @@ describe('General', () => {
783783
'Custom Collection View',
784784
)
785785
})
786+
787+
test('should render custom collection view as a client component', async () => {
788+
await page.goto(
789+
formatAdminURL({
790+
adminRoute,
791+
path: '/collections/custom-collection-view/grid-client',
792+
serverURL,
793+
}),
794+
)
795+
await expect(page.locator('h1#custom-collection-view-client-title')).toContainText(
796+
'Custom Collection View (Client)',
797+
)
798+
await expect(page.locator('#custom-collection-view-client-slug')).toContainText(
799+
'custom-collection-view',
800+
)
801+
})
786802
})
787803

788804
describe('header actions', () => {

test/admin/shared.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ export const customViewTitle = 'Custom View'
2020

2121
export const customCollectionViewTitle = 'Custom Collection View'
2222

23+
export const customCollectionViewClientTitle = 'Custom Collection View (Client)'
24+
2325
export const customParamViewTitle = 'Custom Param View'
2426

2527
export const customNestedViewTitle = 'Custom Nested View'

0 commit comments

Comments
 (0)