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

🐛(react) fix datagrid column unique key #119

Merged
merged 1 commit into from
Jul 12, 2023

Conversation

AntoLC
Copy link
Contributor

@AntoLC AntoLC commented Jul 10, 2023

Purpose

If we had more than 1 columns with custom cells, we would get a warning about duplicate keys, because every columns got the key actions.

Proposal

We add the column index to the key to make it unique.

@AntoLC AntoLC self-assigned this Jul 10, 2023
@changeset-bot
Copy link

changeset-bot bot commented Jul 10, 2023

🦋 Changeset detected

Latest commit: 33c3ea4

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@openfun/cunningham-react Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@AntoLC AntoLC added bug Something isn't working easy pick Simple tasks that are ideal for new contributors to get started. labels Jul 10, 2023
@AntoLC AntoLC marked this pull request as ready for review July 10, 2023 10:43
@AntoLC
Copy link
Contributor Author

AntoLC commented Jul 10, 2023

Hi, I am not sure if I should do something about the Changelog or Changeset?

@lebaudantoine
Copy link
Collaborator

@AntoLC as you are fixing a minor bug, I would add a changeset.

@AntoLC AntoLC force-pushed the issue/datagrid-unique-keys branch from 3535859 to 9a8609d Compare July 10, 2023 14:06
Copy link
Collaborator

@lebaudantoine lebaudantoine left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM ✌️ minor discussion

Comment on lines 25 to 27
let headlessColumns = columns.map((column) => {
let headlessColumns = columns.map((column, index) => {
const opts = {
id: column.field ?? "actions",
id: column.field ?? `actions_${index}`,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Crystal clear! Since I haven't had the opportunity to work on this particular component, my question may not be directly relevant. Therefore, please feel free to disregard it if it doesn't apply.

I noticed that when reordering the columns, the column's id would be updated. However, I'm curious whether this would also trigger a re-rendering of the column content, even if the content itself hasn't changed. In React, using the index as a key for a list is considered an antipattern. I'm wondering if the same principle applies in this scenario as well.

Copy link
Contributor Author

@AntoLC AntoLC Jul 10, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes it's not perfect, but we don't have too much data to rely on (https://github.com/openfun/cunningham/blob/main/packages/react/src/components/DataGrid/index.tsx#L26-L32), we could eventually improve a bit by doing something like that:

id: column.field ?? `actions-${column.headerName ? `${column.headerName}-${index}` : index }`,

Open to suggestion.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could do something like that as well (dd06f1a), an id property is mandatory if the property renderCell is defined. The bad side is that it creates a breaking change with the datagrid with renderCell defined.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@NathanVss WDYT ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The last edit lgtm :D

Copy link
Member

@NathanVss NathanVss left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Thanks you for your contribution 🤩

If we had more than 1 columns with custom cells, we would get a warning
about duplicate keys, because every columns got the key `actions`.
We add the column index to the key to make it unique.
@NathanVss NathanVss merged commit e4c1df5 into main Jul 12, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working easy pick Simple tasks that are ideal for new contributors to get started.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

SimpleDataGrid - Prob unique key with renderCell method
3 participants