Skip to content

Commit be84ad7

Browse files
authored
fix(ui): make relationship fields update the collection when it is changed in the drawer dropdown (#10338)
To reproduce the bug: 1. Within a Lexical editor, insert a relationship field. 2. In the drawer, change the selected collection. 3. The table below changes correctly, but the title and the "create new" button quickly revert to the original option. https://github.com/user-attachments/assets/e4b7c615-4b98-4c11-a4b9-a828606edb6f <!-- Thank you for the PR! Please go through the checklist below and make sure you've completed all the steps. Please review the [CONTRIBUTING.md](https://github.com/payloadcms/payload/blob/main/CONTRIBUTING.md) document in this repository if you haven't already. The following items will ensure that your PR is handled as smoothly as possible: - PR Title must follow conventional commits format. For example, `feat: my new feature`, `fix(plugin-seo): my fix`. - Minimal description explained as if explained to someone not immediately familiar with the code. - Provide before/after screenshots or code diffs if applicable. - Link any related issues/discussions from GitHub or Discord. - Add review comments if necessary to explain to the reviewer the logic behind a change ### What? ### Why? ### How? Fixes # -->
1 parent 3ea1d39 commit be84ad7

File tree

4 files changed

+52
-21
lines changed

4 files changed

+52
-21
lines changed

packages/richtext-lexical/src/features/relationship/client/drawer/index.tsx

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,12 +80,6 @@ const RelationshipDrawerComponent: React.FC<Props> = ({ enabledCollectionSlugs }
8080
[editor, closeListDrawer, replaceNodeKey],
8181
)
8282

83-
useEffect(() => {
84-
// always reset back to first option
85-
// TODO: this is not working, see the ListDrawer component
86-
setSelectedCollectionSlug(enabledCollectionSlugs?.[0])
87-
}, [isListDrawerOpen, enabledCollectionSlugs])
88-
8983
return <ListDrawer onSelect={onSelect} />
9084
}
9185

packages/richtext-slate/src/field/elements/relationship/Button/index.tsx

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,6 @@ const RelationshipButtonComponent: React.FC<Props> = ({ enabledCollectionSlugs }
5555
[editor, closeDrawer],
5656
)
5757

58-
useEffect(() => {
59-
// always reset back to first option
60-
// TODO: this is not working, see the ListDrawer component
61-
setSelectedCollectionSlug(enabledCollectionSlugs[0])
62-
}, [isDrawerOpen, enabledCollectionSlugs])
63-
6458
return (
6559
<Fragment>
6660
<ListDrawerToggler>

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

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import React, { useCallback, useEffect, useState } from 'react'
77
import type { ListDrawerProps } from './types.js'
88

99
import { useDocumentDrawer } from '../../elements/DocumentDrawer/index.js'
10+
import { useIgnoredEffect } from '../../hooks/useIgnoredEffect.js'
1011
import { useConfig } from '../../providers/Config/index.js'
1112
import { useServerFunctions } from '../../providers/ServerFunctions/index.js'
1213
import { hoistQueryParamsToAnd } from '../../utilities/mergeListSearchAndWhere.js'
@@ -58,15 +59,18 @@ export const ListDrawerContent: React.FC<ListDrawerProps> = ({
5859
useDocumentDrawer({
5960
collectionSlug: selectedOption.value,
6061
})
61-
62-
useEffect(() => {
63-
if (selectedCollectionFromProps && selectedCollectionFromProps !== selectedOption?.value) {
64-
setSelectedOption({
65-
label: collections.find(({ slug }) => slug === selectedCollectionFromProps).labels,
66-
value: selectedCollectionFromProps,
67-
})
68-
}
69-
}, [selectedCollectionFromProps, collections, selectedOption])
62+
useIgnoredEffect(
63+
() => {
64+
if (selectedCollectionFromProps && selectedCollectionFromProps !== selectedOption?.value) {
65+
setSelectedOption({
66+
label: collections.find(({ slug }) => slug === selectedCollectionFromProps).labels,
67+
value: selectedCollectionFromProps,
68+
})
69+
}
70+
},
71+
[selectedCollectionFromProps],
72+
[collections, selectedOption],
73+
)
7074

7175
const renderList = useCallback(
7276
async (slug: string, query?: ListQuery) => {

test/fields/collections/Lexical/e2e/main/e2e.spec.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1179,6 +1179,45 @@ describe('lexicalMain', () => {
11791179
})
11801180
})
11811181

1182+
test('make relationship fields update the collection when it is changed in the drawer dropdown', async () => {
1183+
await navigateToLexicalFields()
1184+
const richTextField = page.locator('.rich-text-lexical').first()
1185+
await richTextField.scrollIntoViewIfNeeded()
1186+
await expect(richTextField).toBeVisible()
1187+
// Wait until there at least 10 blocks visible in that richtext field - thus wait for it to be fully loaded
1188+
await expect(page.locator('.rich-text-lexical').nth(2).locator('.lexical-block')).toHaveCount(
1189+
10,
1190+
)
1191+
await expect(page.locator('.shimmer-effect')).toHaveCount(0)
1192+
await richTextField.locator('.ContentEditable__root').first().click()
1193+
const lastParagraph = richTextField.locator('p').first()
1194+
await lastParagraph.scrollIntoViewIfNeeded()
1195+
await expect(lastParagraph).toBeVisible()
1196+
1197+
await lastParagraph.click()
1198+
await page.keyboard.type('/Relationship')
1199+
const slashMenuPopover = page.locator('#slash-menu .slash-menu-popup')
1200+
await expect(slashMenuPopover).toBeVisible()
1201+
await page.keyboard.press('Enter')
1202+
1203+
const relationshipInput = page.locator('.drawer__content .rs__input').first()
1204+
await expect(relationshipInput).toBeVisible()
1205+
await page.getByRole('heading', { name: 'Lexical Fields' })
1206+
await relationshipInput.click()
1207+
const user = await page.getByRole('option', { name: 'User' })
1208+
await user.click()
1209+
1210+
const userListDrawer = page
1211+
.locator('div')
1212+
.filter({ hasText: /^User$/ })
1213+
.first()
1214+
await expect(userListDrawer).toBeVisible()
1215+
await page.getByRole('heading', { name: 'Users' })
1216+
const button = await page.getByLabel('Add new User')
1217+
await button.click()
1218+
await page.getByText('Creating new User')
1219+
})
1220+
11821221
describe('localization', () => {
11831222
test.skip('ensure simple localized lexical field works', async () => {
11841223
await navigateToLexicalFields(true, true)

0 commit comments

Comments
 (0)