Skip to content

Commit c05f10a

Browse files
authored
chore: passes allowCreate into list drawer and adds test (#11284)
### What? We had an `allowCreate` prop for the list drawer that doesn't do anything. This PR passes the prop through so it can be used. ### How? Passes `allowCreate` down to the list view and ties it with `hasCreatePermission` #### Testing - Use `admin` test suite and `withListDrawer` collection. - Test added to the `admin/e2e/list-view`. Fixes #11246
1 parent 26163a7 commit c05f10a

File tree

9 files changed

+119
-4
lines changed

9 files changed

+119
-4
lines changed

packages/ui/src/elements/ListDrawer/DrawerContent.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ export const ListDrawerContent: React.FC<ListDrawerProps> = ({
8787
const { List: ViewResult } = (await serverFunction({
8888
name: 'render-list',
8989
args: {
90+
allowCreate,
9091
collectionSlug: slug,
9192
disableBulkDelete: true,
9293
disableBulkEdit: true,
@@ -160,6 +161,7 @@ export const ListDrawerContent: React.FC<ListDrawerProps> = ({
160161

161162
return (
162163
<ListDrawerContextProvider
164+
allowCreate={allowCreate}
163165
createNewDrawerSlug={documentDrawerSlug}
164166
DocumentDrawerToggler={DocumentDrawerToggler}
165167
drawerSlug={drawerSlug}

packages/ui/src/elements/ListDrawer/Provider.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import type { UseDocumentDrawer } from '../DocumentDrawer/types.js'
77
import type { Option } from '../ReactSelect/index.js'
88

99
export type ListDrawerContextProps = {
10+
readonly allowCreate?: boolean
1011
readonly createNewDrawerSlug?: string
1112
readonly DocumentDrawerToggler?: ReturnType<UseDocumentDrawer>[1]
1213
readonly drawerSlug?: string

packages/ui/src/views/List/index.tsx

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ export function DefaultListView(props: ListViewClientProps) {
5151
disableBulkDelete,
5252
disableBulkEdit,
5353
enableRowSelections,
54-
hasCreatePermission,
54+
hasCreatePermission: hasCreatePermissionFromProps,
5555
listMenuItems,
5656
listPreferences,
5757
newDocumentURL,
@@ -63,7 +63,17 @@ export function DefaultListView(props: ListViewClientProps) {
6363

6464
const [Table, setTable] = useState(InitialTable)
6565

66-
const { createNewDrawerSlug, drawerSlug: listDrawerSlug, onBulkSelect } = useListDrawerContext()
66+
const {
67+
allowCreate,
68+
createNewDrawerSlug,
69+
drawerSlug: listDrawerSlug,
70+
onBulkSelect,
71+
} = useListDrawerContext()
72+
73+
const hasCreatePermission =
74+
allowCreate !== undefined
75+
? allowCreate && hasCreatePermissionFromProps
76+
: hasCreatePermissionFromProps
6777

6878
useEffect(() => {
6979
if (InitialTable) {
@@ -145,7 +155,6 @@ export function DefaultListView(props: ListViewClientProps) {
145155
])
146156
}
147157
}, [setStepNav, labels, drawerDepth])
148-
149158
return (
150159
<Fragment>
151160
<TableColumnsProvider

test/admin/collections/ListDrawer.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import type { CollectionConfig } from 'payload'
2+
3+
import { listDrawerSlug } from '../slugs.js'
4+
5+
export const ListDrawer: CollectionConfig = {
6+
slug: listDrawerSlug,
7+
admin: {
8+
components: {
9+
beforeListTable: [
10+
{
11+
path: '/components/BeforeList/index.js#SelectPostsButton',
12+
},
13+
],
14+
},
15+
},
16+
fields: [
17+
{
18+
name: 'title',
19+
type: 'text',
20+
},
21+
{
22+
name: 'description',
23+
type: 'text',
24+
},
25+
{
26+
name: 'number',
27+
type: 'number',
28+
},
29+
],
30+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// src/components/SelectPostsButton.tsx
2+
'use client'
3+
import { Button, type UseListDrawer, useListDrawer } from '@payloadcms/ui'
4+
import { useMemo } from 'react'
5+
6+
type UseListDrawerArgs = Parameters<UseListDrawer>[0]
7+
8+
export const SelectPostsButton = () => {
9+
const listDrawerArgs = useMemo<UseListDrawerArgs>(
10+
() => ({
11+
collectionSlugs: ['with-list-drawer'],
12+
}),
13+
[],
14+
)
15+
const [ListDrawer, _, { toggleDrawer }] = useListDrawer(listDrawerArgs)
16+
17+
return (
18+
<>
19+
<Button onClick={() => toggleDrawer()}>Select posts</Button>
20+
<ListDrawer allowCreate={false} enableRowSelections={false} />
21+
</>
22+
)
23+
}

test/admin/config.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import { CollectionGroup1B } from './collections/Group1B.js'
1414
import { CollectionGroup2A } from './collections/Group2A.js'
1515
import { CollectionGroup2B } from './collections/Group2B.js'
1616
import { CollectionHidden } from './collections/Hidden.js'
17+
import { ListDrawer } from './collections/ListDrawer.js'
1718
import { CollectionNoApiView } from './collections/NoApiView.js'
1819
import { CollectionNotInView } from './collections/NotInView.js'
1920
import { Posts } from './collections/Posts.js'
@@ -39,7 +40,6 @@ import {
3940
protectedCustomNestedViewPath,
4041
publicCustomViewPath,
4142
} from './shared.js'
42-
4343
export default buildConfigWithDefaults({
4444
admin: {
4545
importMap: {
@@ -157,6 +157,7 @@ export default buildConfigWithDefaults({
157157
DisableDuplicate,
158158
BaseListFilter,
159159
with300Documents,
160+
ListDrawer,
160161
],
161162
globals: [
162163
GlobalHidden,

test/admin/e2e/list-view/e2e.spec.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import { customAdminRoutes } from '../../shared.js'
1919
import {
2020
customViews1CollectionSlug,
2121
geoCollectionSlug,
22+
listDrawerSlug,
2223
postsCollectionSlug,
2324
with300DocumentsSlug,
2425
} from '../../slugs.js'
@@ -56,6 +57,7 @@ describe('List View', () => {
5657
let baseListFiltersUrl: AdminUrlUtil
5758
let customViewsUrl: AdminUrlUtil
5859
let with300DocumentsUrl: AdminUrlUtil
60+
let withListViewUrl: AdminUrlUtil
5961

6062
let serverURL: string
6163
let adminRoutes: ReturnType<typeof getRoutes>
@@ -76,6 +78,7 @@ describe('List View', () => {
7678
with300DocumentsUrl = new AdminUrlUtil(serverURL, with300DocumentsSlug)
7779
baseListFiltersUrl = new AdminUrlUtil(serverURL, 'base-list-filters')
7880
customViewsUrl = new AdminUrlUtil(serverURL, customViews1CollectionSlug)
81+
withListViewUrl = new AdminUrlUtil(serverURL, listDrawerSlug)
7982

8083
const context = await browser.newContext()
8184
page = await context.newPage()
@@ -156,6 +159,20 @@ describe('List View', () => {
156159
`${adminRoutes.routes?.admin}/collections/posts/${id}`,
157160
)
158161
})
162+
163+
test('should hide create new button when allowCreate is false', async () => {
164+
await page.goto(withListViewUrl.list)
165+
166+
const drawerButton = page.locator('button', { hasText: 'Select Posts' })
167+
await expect(drawerButton).toBeVisible()
168+
await drawerButton.click()
169+
170+
const drawer = page.locator('.drawer__content')
171+
await expect(drawer).toBeVisible()
172+
173+
const createButton = page.locator('button', { hasText: 'Create New' })
174+
await expect(createButton).toBeHidden()
175+
})
159176
})
160177

161178
describe('list view custom components', () => {

test/admin/payload-types.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ export interface Config {
8383
'disable-duplicate': DisableDuplicate;
8484
'base-list-filters': BaseListFilter;
8585
with300documents: With300Document;
86+
'with-list-drawer': WithListDrawer;
8687
'payload-locked-documents': PayloadLockedDocument;
8788
'payload-preferences': PayloadPreference;
8889
'payload-migrations': PayloadMigration;
@@ -106,6 +107,7 @@ export interface Config {
106107
'disable-duplicate': DisableDuplicateSelect<false> | DisableDuplicateSelect<true>;
107108
'base-list-filters': BaseListFiltersSelect<false> | BaseListFiltersSelect<true>;
108109
with300documents: With300DocumentsSelect<false> | With300DocumentsSelect<true>;
110+
'with-list-drawer': WithListDrawerSelect<false> | WithListDrawerSelect<true>;
109111
'payload-locked-documents': PayloadLockedDocumentsSelect<false> | PayloadLockedDocumentsSelect<true>;
110112
'payload-preferences': PayloadPreferencesSelect<false> | PayloadPreferencesSelect<true>;
111113
'payload-migrations': PayloadMigrationsSelect<false> | PayloadMigrationsSelect<true>;
@@ -445,6 +447,18 @@ export interface With300Document {
445447
updatedAt: string;
446448
createdAt: string;
447449
}
450+
/**
451+
* This interface was referenced by `Config`'s JSON-Schema
452+
* via the `definition` "with-list-drawer".
453+
*/
454+
export interface WithListDrawer {
455+
id: string;
456+
title?: string | null;
457+
description?: string | null;
458+
number?: number | null;
459+
updatedAt: string;
460+
createdAt: string;
461+
}
448462
/**
449463
* This interface was referenced by `Config`'s JSON-Schema
450464
* via the `definition` "payload-locked-documents".
@@ -519,6 +533,10 @@ export interface PayloadLockedDocument {
519533
| ({
520534
relationTo: 'with300documents';
521535
value: string | With300Document;
536+
} | null)
537+
| ({
538+
relationTo: 'with-list-drawer';
539+
value: string | WithListDrawer;
522540
} | null);
523541
globalSlug?: string | null;
524542
user: {
@@ -822,6 +840,17 @@ export interface With300DocumentsSelect<T extends boolean = true> {
822840
updatedAt?: T;
823841
createdAt?: T;
824842
}
843+
/**
844+
* This interface was referenced by `Config`'s JSON-Schema
845+
* via the `definition` "with-list-drawer_select".
846+
*/
847+
export interface WithListDrawerSelect<T extends boolean = true> {
848+
title?: T;
849+
description?: T;
850+
number?: T;
851+
updatedAt?: T;
852+
createdAt?: T;
853+
}
825854
/**
826855
* This interface was referenced by `Config`'s JSON-Schema
827856
* via the `definition` "payload-locked-documents_select".

test/admin/slugs.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ export const noApiViewCollectionSlug = 'collection-no-api-view'
1313
export const disableDuplicateSlug = 'disable-duplicate'
1414
export const uploadCollectionSlug = 'uploads'
1515
export const customFieldsSlug = 'custom-fields'
16+
17+
export const listDrawerSlug = 'with-list-drawer'
1618
export const collectionSlugs = [
1719
usersCollectionSlug,
1820
customViews1CollectionSlug,
@@ -27,6 +29,7 @@ export const collectionSlugs = [
2729
noApiViewCollectionSlug,
2830
customFieldsSlug,
2931
disableDuplicateSlug,
32+
listDrawerSlug,
3033
]
3134

3235
export const customGlobalViews1GlobalSlug = 'custom-global-views-one'

0 commit comments

Comments
 (0)