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

Datatable: onSelectionChange event returns wrong rowIndex with shiftKey selection #3388

Closed
kyybo opened this issue Sep 29, 2022 · 1 comment · Fixed by #3412 or #3417
Closed

Datatable: onSelectionChange event returns wrong rowIndex with shiftKey selection #3388

kyybo opened this issue Sep 29, 2022 · 1 comment · Fixed by #3412 or #3417
Assignees
Labels
Type: Bug Issue contains a defect related to a specific component.
Milestone

Comments

@kyybo
Copy link
Contributor

kyybo commented Sep 29, 2022

Describe the bug

I noticed that the onSelectionChange event of the Datatable component returns values with a bad 'rowIndex' attribute in some cases.

For example : a Datatable where multiple cell selection with MetaKey is activated and we have a pagination.

<Datatable
...
selectionMode="multiple"
cellSelection
metaKeySelection={true}
...
paginatorTemplate="CurrentPageReport FirstPageLink PrevPageLink PageLinks NextPageLink LastPageLink"
rows={5}
rowsPerPageOptions={[5, 10, 50]}
...
>
...
</Datatable>

In that case, if we go to the 2nd page and looked at the values return by the onSelectionChange event onSelectionChange={(e) => console.log(e.value)}, we will see that :

  • with a simple click or with Ctrl+click (ctrlKey), the returned values have a correct rowIndex which match with the pagination. (in the example above, a click on the first element of the 2nd page will have rowIndex=5)
  • with Shift+click, the returned values have a rowIndex which doesn't correspond with the pagination. (in the example above, a shift+click on the first element of the 2nd page will have rowIndex=0)

Reproducer

https://codesandbox.io/embed/nervous-wind-g8lygm?fontsize=14&hidenavigation=1&theme=dark

PrimeReact version

8.6.0

React version

18.x

Language

TypeScript

Build / Runtime

Create React App (CRA)

Browser(s)

No response

Steps to reproduce the behavior

No response

Expected behavior

A Shift+click selection should return values with a rowIndex matching the pagination.

@kyybo kyybo added the Status: Needs Triage Issue will be reviewed by Core Team and a relevant label will be added as soon as possible label Sep 29, 2022
@melloware
Copy link
Member

I think the bug is in this method:

const selectRange = (event) => {
            let rangeStart, rangeEnd;

            if (rangeRowIndex.current > anchorRowIndex.current) {
                rangeStart = anchorRowIndex.current;
                rangeEnd = rangeRowIndex.current;
            } else if (rangeRowIndex.current < anchorRowIndex.current) {
                rangeStart = rangeRowIndex.current;
                rangeEnd = anchorRowIndex.current;
            } else {
                rangeStart = rangeEnd = rangeRowIndex.current;
            }

            if (props.paginator) {
                rangeStart = Math.max(rangeStart - props.first, 0);
                rangeEnd -= props.first;
            }

            return allowCellSelection() ? selectRangeOnCell(event, rangeStart, rangeEnd) : selectRangeOnRow(event, rangeStart, rangeEnd);
        };

melloware added a commit to melloware/primereact that referenced this issue Oct 2, 2022
@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 Oct 2, 2022
@melloware melloware self-assigned this Oct 2, 2022
@melloware melloware added this to the 8.6.2 milestone Oct 2, 2022
mertsincan added a commit that referenced this issue Oct 2, 2022
melloware added a commit to melloware/primereact that referenced this issue Oct 4, 2022
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