Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MultiSelect: Duplicate Labels in MultiSelect Component Cause Errors #6650

Closed
sebasdeweert opened this issue May 22, 2024 · 1 comment · Fixed by #6652, leoo1992/GeradorQRCode#40 or mtech-31-quemistry/quemistry_client_web#3
Assignees
Labels
Type: Bug Issue contains a defect related to a specific component.
Milestone

Comments

@sebasdeweert
Copy link

sebasdeweert commented May 22, 2024

Describe the bug

When using the MultiSelect component with options that have identical labels, such as "New York", the component uses the label as the key. This leads to warnings and unexpected behavior.

Steps to Reproduce

  1. Define a list of options with duplicate labels:
  const cities = [
    { label: 'Amsterdam', value: 0 },
    { label: 'Dublin', value: 1 },
    { label: 'New York', value: 2 },
    { label: 'New York', value: 3 },
    { label: 'Berlin', value: 4 },
  ];
  1. Implement the MultiSelect component with this list.

  2. Select the first "New York" option.

  3. Notice the warning in the console:

Warning: Encountered two children with the same key, New York. Keys should be unique so that components maintain their identity across updates. Non-unique keys may cause children to be duplicated and/or omitted — the behavior is unsupported and could change in a future version.

  1. Attempt to delete the duplicate "New York" option.

Expected Behavior
The component should handle duplicate labels without warnings or errors, allowing users to select and delete items correctly.

Actual Behavior

  • A warning appears in the console about duplicate keys.

  • When attempting to delete the duplicate, new values are incorrectly added to the list.

Possible Solution
Allow the use of a unique key other than the label for the MultiSelect component. This could be achieved by using the value field or an additional unique identifier for each option.

Additional Context
This issue can occur in scenarios where items have the same name, such as a list of people with identical first and last names.

Reproducer

https://stackblitz.com/edit/vitejs-vite-1u4ahw?file=src%2FApp.tsx

PrimeReact version

10.6.5

React version

18.x

Language

TypeScript

Build / Runtime

Vite

@sebasdeweert sebasdeweert added the Status: Needs Triage Issue will be reviewed by Core Team and a relevant label will be added as soon as possible label May 22, 2024
@melloware melloware added Type: Bug Issue contains a defect related to a specific component. and removed Status: Needs Triage Issue will be reviewed by Core Team and a relevant label will be added as soon as possible labels May 22, 2024
@melloware melloware self-assigned this May 22, 2024
@melloware melloware added this to the 10.7.0 milestone May 22, 2024
melloware added a commit to melloware/primereact that referenced this issue May 22, 2024
@melloware
Copy link
Member

Thanks PR submitted!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Type: Bug Issue contains a defect related to a specific component.
Projects
None yet
2 participants