Skip to content

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

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

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
Contributor

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
Contributor 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
Contributor 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.

@johnSmithTech

This comment was marked as spam.

@JarrodMFlesch
Copy link
Copy Markdown
Contributor

JarrodMFlesch commented Apr 7, 2026

@howwohmm Looks like you archived the fork. Do you want to revive it? I opened another PR that included your fixes and some changes I had. Let me know!

JarrodMFlesch added a commit that referenced this pull request Apr 7, 2026
…rs (#16204)

Fixes #15947

## Summary
- Fixes duplicate options appearing in relationship filter dropdowns
when switching operators
- The `reduceToIDs` function was incorrectly using `option.id` (which
doesn't exist on the Option type) instead of `option.value` for
deduplication

## Test plan
- [x] Unit tests added for optionsReducer deduplication logic
- [x] E2E test added that verifies no duplicate options when switching
operators

Continuation from #16029 which
was from a fork of payload that was archived.

Co-authored-by: https://github.com/howwohmm

---------

Co-authored-by: OM MISHRA <152969928+howwohmm@users.noreply.github.com>
Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
@JarrodMFlesch
Copy link
Copy Markdown
Contributor

Closing, merged in #16204

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

relationship filter dropdown shows duplicate options when switching operators

4 participants