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

OrderList: Behavior when using spread syntax in onChange #4089

Closed
xabierun opened this issue Feb 21, 2023 · 6 comments · Fixed by #5732
Closed

OrderList: Behavior when using spread syntax in onChange #4089

xabierun opened this issue Feb 21, 2023 · 6 comments · Fixed by #5732
Assignees
Labels
Type: Bug Issue contains a defect related to a specific component.
Milestone

Comments

@xabierun
Copy link

Describe the bug

Hello.
When updating state using spread syntax with the value of [event.value] passed when the onChange event of OrderList is fired, the following bug occurs.

  • Focus is lost when selecting.
  • When the down arrow is pressed continuously, "undefined" is stored in the array and becomes an invalid value.

Reproducer

https://codesandbox.io/s/primereact-demo-forked-i55nkl?file=/src/demo.js

PrimeReact version

8.3.0

React version

17.x

Language

TypeScript

Build / Runtime

Next.js

Browser(s)

No response

Steps to reproduce the behavior

  1. select one of the products shown.
  2. press the down arrow once (the focus is removed at this point)
  3. Press the down arrow again (the value of the item at the top is undefined)

image

Expected behavior

Ideally, no focus is lost and no undefined is stored in the array.

@xabierun xabierun added the Status: Needs Triage Issue will be reviewed by Core Team and a relevant label will be added as soon as possible label Feb 21, 2023
@melloware
Copy link
Member

I will try and diagnose what is going on here. Is it only with spread notation? Not if you use individual props?

@xabierun
Copy link
Author

xabierun commented Feb 21, 2023

For example, if I keep sort numbers in my product list and I want to update only the other values without changing the sort numbers, I want to use the spread syntax.

  const sortEventHandle = (value) => {
    const newData = value.map((item, index) => {
      return {
          ...item,
         sort_number: products[index].sort_number
       };
    });
    setProducts(newData);
  };

@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 11, 2023
@melloware melloware changed the title Behavior when using spread syntax in OrderList onChange OrderList : Behavior when using spread syntax in onChange May 11, 2023
@melloware melloware changed the title OrderList : Behavior when using spread syntax in onChange OrderList: Behavior when using spread syntax in onChange May 11, 2023
@kl-nevermore
Copy link
Contributor

@xabierun
when dataKey isn't passed, ObjectUtils.findIndexInList will use reference checking

return dataKey ? ... : list.findIndex((item) => item === value);

So you should passed dataKey like...

<OrderList
    dataKey="id"
    value={products}
    onChange={(e) => {
        sortEventHandle(e);
    }}
    itemTemplate={itemTemplate}
    header="Products"
></OrderList>;

@kl-nevermore
Copy link
Contributor

kl-nevermore commented Jan 4, 2024

@melloware
Maybe give a warning in development mode when not pass dataKey?
or
Update doc to inform users of the importance of dataKey(like Table, OrderList, etc.)

@melloware
Copy link
Member

melloware commented Jan 4, 2024

Lets update docs and make sure all showcase examples have a dataKey ??

@melloware melloware added this to the 10.2.2 milestone Jan 4, 2024
@melloware melloware added Component: Documentation Issue or pull request is related to Documentation and removed Type: Bug Issue contains a defect related to a specific component. labels Jan 4, 2024
@gucal gucal modified the milestones: 10.3.0, 10.3.1 Jan 5, 2024
@melloware melloware modified the milestones: 10.3.1, 10.4.0, 10.4.1 Jan 9, 2024
@melloware melloware self-assigned this Jan 10, 2024
melloware added a commit to melloware/primereact that referenced this issue Jan 10, 2024
melloware added a commit to melloware/primereact that referenced this issue Jan 10, 2024
@melloware melloware added Type: Bug Issue contains a defect related to a specific component. and removed Component: Documentation Issue or pull request is related to Documentation labels Jan 10, 2024
@melloware
Copy link
Member

OK I updated all examples and switching dataKey to required in the TypeScript def.

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
Development

Successfully merging a pull request may close this issue.

4 participants