feat: join field admin.defaultColumns (#9982)

Add the ability to specify which columns should appear in the
relationship table of a join fields

The new property is in the Join field `admin.defaultColumns` and can be
set to an array of strings containing the field names in the desired
order.
This commit is contained in:
Dan Ribbens
2024-12-14 22:31:31 -05:00
committed by GitHub
parent f5516b96da
commit 2ec4d0c2ef
6 changed files with 42 additions and 16 deletions

View File

@@ -91,6 +91,9 @@ export const Categories: CollectionConfig = {
type: 'join',
collection: postsSlug,
on: 'group.category',
admin: {
defaultColumns: ['id', 'createdAt', 'title'],
},
},
{
name: 'camelCasePosts',

View File

@@ -218,16 +218,28 @@ test.describe('Join Field', () => {
const titleAscButton = titleColumn.locator('button.sort-column__asc')
await expect(titleAscButton).toBeVisible()
await titleAscButton.click()
await expect(joinField.locator('tbody tr:first-child td:nth-child(2)')).toHaveText(
'Test Post 1',
)
await expect(joinField.locator('tbody .row-1')).toContainText('Test Post 1')
const titleDescButton = titleColumn.locator('button.sort-column__desc')
await expect(titleDescButton).toBeVisible()
await titleDescButton.click()
await expect(joinField.locator('tbody tr:first-child td:nth-child(2)')).toHaveText(
'Test Post 3',
)
await expect(joinField.locator('tbody .row-1')).toContainText('Test Post 3')
})
test('should display relationship table with columns from admin.defaultColumns', async () => {
await page.goto(categoriesURL.edit(categoryID))
const joinField = page.locator('#field-group__relatedPosts.field-type.join')
const thead = joinField.locator('.relationship-table thead')
await expect(thead).toContainText('ID')
await expect(thead).toContainText('Created At')
await expect(thead).toContainText('Title')
const innerText = await thead.innerText()
// expect the order of columns to be 'ID', 'Created At', 'Title'
// eslint-disable-next-line payload/no-flaky-assertions
expect(innerText.indexOf('ID')).toBeLessThan(innerText.indexOf('Created At'))
// eslint-disable-next-line payload/no-flaky-assertions
expect(innerText.indexOf('Created At')).toBeLessThan(innerText.indexOf('Title'))
})
test('should update relationship table when new document is created', async () => {
@@ -276,9 +288,7 @@ test.describe('Join Field', () => {
await titleField.fill('Test Post 1 Updated')
await drawer.locator('button[id="action-save"]').click()
await expect(drawer).toBeHidden()
await expect(joinField.locator('tbody tr:first-child td:nth-child(2)')).toHaveText(
'Test Post 1 Updated',
)
await expect(joinField.locator('tbody .row-1')).toContainText('Test Post 1 Updated')
})
test('should render empty relationship table when creating new document', async () => {