Skip to content

Commit dc11104

Browse files
authored
fix: incorrect form changed state after doc drawer edit (#9025)
Closes #9000 When you update a relationship document via the document drawer, the initial document is registering `modified: true`. We should only set modified to true on the initial document if the relationship id has changed.
1 parent f67761f commit dc11104

File tree

2 files changed

+36
-16
lines changed

2 files changed

+36
-16
lines changed

packages/ui/src/fields/Relationship/index.tsx

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -457,26 +457,25 @@ const RelationshipFieldComponent: RelationshipFieldClientComponent = (props) =>
457457
i18n,
458458
})
459459

460+
const currentValue = valueRef.current
461+
const docId = args.doc.id
462+
460463
if (hasMany) {
461-
setValue(
462-
valueRef.current
463-
? (valueRef.current as Option[]).map((option) => {
464-
if (option.value === args.doc.id) {
465-
return {
466-
relationTo: args.collectionConfig.slug,
467-
value: args.doc.id,
468-
}
469-
}
464+
const unchanged = (currentValue as Option[]).some((option) =>
465+
typeof option === 'string' ? option === docId : option.value === docId,
466+
)
470467

471-
return option
472-
})
473-
: null,
468+
const valuesToSet = (currentValue as Option[]).map((option) =>
469+
option.value === docId
470+
? { relationTo: args.collectionConfig.slug, value: docId }
471+
: option,
474472
)
473+
474+
setValue(valuesToSet, unchanged)
475475
} else {
476-
setValue({
477-
relationTo: args.collectionConfig.slug,
478-
value: args.doc.id,
479-
})
476+
const unchanged = currentValue === docId
477+
478+
setValue({ relationTo: args.collectionConfig.slug, value: docId }, unchanged)
480479
}
481480
},
482481
[i18n, config, hasMany, setValue],

test/fields-relationship/e2e.spec.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,27 @@ describe('fields - relationship', () => {
453453
).toHaveCount(1)
454454
})
455455

456+
test('should update relationship from drawer without enabling save in main doc', async () => {
457+
await page.goto(url.edit(docWithExistingRelations.id))
458+
459+
const saveButton = page.locator('#action-save')
460+
await expect(saveButton).toBeDisabled()
461+
462+
await openDocDrawer(
463+
page,
464+
'#field-relationship button.relationship--single-value__drawer-toggler ',
465+
)
466+
467+
const field = page.locator('#field-name')
468+
await field.fill('Updated')
469+
470+
await saveButton.nth(1).click()
471+
await expect(page.locator('.payload-toast-container')).toContainText('Updated successfully')
472+
await page.locator('.doc-drawer__header-close').click()
473+
474+
await expect(saveButton).toBeDisabled()
475+
})
476+
456477
test('should allow filtering by polymorphic relationships with version drafts enabled', async () => {
457478
await createVersionedRelationshipFieldDoc('Without relationship')
458479
await createVersionedRelationshipFieldDoc('with relationship', [

0 commit comments

Comments
 (0)