Skip to content

fix(ui): relationship filter duplicate options when switching operators#16029

Open
howwohmm wants to merge 3 commits intopayloadcms:mainfrom
howwohmm:fix/relationship-filter-duplicate-options
Open

fix(ui): relationship filter duplicate options when switching operators#16029
howwohmm wants to merge 3 commits intopayloadcms:mainfrom
howwohmm:fix/relationship-filter-duplicate-options

Conversation

@howwohmm
Copy link
Copy Markdown

Summary

  • Fixes duplicate options appearing in the relationship filter dropdown when switching the operator (e.g. from equals to is in)
  • Root cause: reduceToIDs in optionsReducer.ts read option.id, but options are created with { label, value } — there is no id property. This meant deduplication never worked: loadedIDs was always [undefined, undefined, ...], so indexOf(doc.id) always returned -1.
  • Fix: Changed option.id to option.value in reduceToIDs so existing options are correctly recognised and skipped.

Fixes #15947

Test plan

  • Open a list view with a relationship field filter
  • Select equals operator and pick a value
  • Switch operator to is in
  • Confirm the dropdown shows no duplicate entries
  • Test with hasMultipleRelations (polymorphic relationship) to verify grouped options also deduplicate correctly

🤖 Generated with Claude Code

The `reduceToIDs` helper in the relationship filter's optionsReducer
read `option.id`, but options are created with `value` (not `id`).
This meant deduplication never worked — `loadedIDs` was always an
array of `undefined` values. When switching from `equals` to `is_in`,
the second useEffect called `addOptionByID` for the already-loaded
value, and because dedup was broken the same option was appended again.

Fix: read `option.value` instead of `option.id` so existing options
are correctly recognised and skipped.

Fixes payloadcms#15947

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@howwohmm
Copy link
Copy Markdown
Author

good catch @themavik — pushed a fix to use option.value ?? option.id as a fallback so dedupe works regardless of which property is populated. thanks for the review.

@JarrodMFlesch
Copy link
Copy Markdown
Contributor

@howwohmm I think we should add an e2e test that fails without this fix and then passes when this fix is implemented. Can you do this?

Tests verify that ADD correctly deduplicates options by value — both for
single-relation flat lists and multi-relation grouped options.

The dedup test would fail with the original `option.id` read (always
undefined on `{ label, value }` options) and passes with `option.value ?? option.id`.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@howwohmm
Copy link
Copy Markdown
Author

added a vitest unit test in optionsReducer.spec.ts covering:

  • dedup in single-relation flat lists (the exact bug — same doc id loaded twice should appear once)
  • dedup in multi-relation grouped options
  • CLEAR with required/optional

the dedup test fails with the original option.id read (always undefined on { label, value } options) and passes with option.value ?? option.id.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

relationship filter dropdown shows duplicate options when switching operators

2 participants