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

Array items now have unique, stable keys (#1046) #1335

Merged
merged 8 commits into from
Jul 9, 2019

Conversation

fsteger
Copy link
Contributor

@fsteger fsteger commented Jun 25, 2019

Reasons for making this change

Array items did not have stable, unique keys and instead had to rely on the index of the item.
Fixes #1046
Fixes #1333

Checklist

  • I'm updating documentation
  • I'm adding or updating code
    • I've added and/or updated tests
    • I've updated docs if needed
  • I'm adding a new feature
    • I've updated the playground with an example use of the feature

@fsteger
Copy link
Contributor Author

fsteger commented Jun 26, 2019

When using componentWillReceiveProps, the incoming props may be updated via onChange before the setState call completes. Latest change moves onChange to setState callback to ensure it's updated first.

This didn't appear to be an issue with getDerivedStateFromProps.

Copy link
Member

@epicfaace epicfaace left a comment

Choose a reason for hiding this comment

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

Looks good, can you add some tests?

/*
// React 16.3 replaces componentWillReceiveProps with getDerivedStateFromProps
//
static getDerivedStateFromProps(nextProps, prevState) {
Copy link
Member

Choose a reason for hiding this comment

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

I think it might be cleaner (and easier to migrate to React 16) just to use getDerivedStateFromProps and https://github.com/reactjs/react-lifecycles-compat for now?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done in latest commit.

src/components/fields/ArrayField.js Show resolved Hide resolved
@epicfaace
Copy link
Member

@fsteger do you think there is any way to pass on this benefit to those who make their own ArrayFieldTemplates? As of now, they will only get this benefit if they use the default template.

@fsteger
Copy link
Contributor Author

fsteger commented Jul 1, 2019

@fsteger do you think there is any way to pass on this benefit to those who make their own ArrayFieldTemplates? As of now, they will only get this benefit if they use the default template.

Yeah; that was my intention. Each item in the array of props.items in ArrayFieldTemplate should now have access to the key property. I had missed this initially for fixed arrays, but it should consistently be present now.

I updated the customArray.js in the playground to use the key, and updated the "should move from first to last in the list" test (ArrayField_test.js), which uses a custom ArrayFieldTemplate, to include the keys as well.

<div key={props.index} className={props.className}>
<div
key={props.key}
id={`array-item-${props.key}`}
Copy link
Member

Choose a reason for hiding this comment

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

I don't think there's any point in exposing the key to the user as the id? Currently ids are only used as per the idSchema (for example, root_listOfStrings_0) so having another function for id may also not be the best.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

What would you recommend here?

Copy link
Member

@epicfaace epicfaace Jul 3, 2019

Choose a reason for hiding this comment

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

Can we just remove the id?

Or is there a particular reason why you thought of adding it?

Copy link
Contributor Author

@fsteger fsteger Jul 8, 2019

Choose a reason for hiding this comment

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

I'm using the id right now to ensure that the key does not change inside the ArrayField tests. If you remove the id, the tests for inserting new rows will fail. The re-order and delete tests only pass when the attribute is removed because the id both before and after the action are undefined, but these should really test to ensure an id exists as well.

If we remove the id, I'm not sure how these tests should be updated to verify the correct behavior as the key doesn't appear to be exposed anywhere else.

Regarding the idSchema, were you thinking of just passing the key into the toIdSchema method for each item, and then accessing it via props.idSchema.key?

Copy link
Member

@epicfaace epicfaace Jul 8, 2019

Choose a reason for hiding this comment

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

Got it. I don't think we should add it to the idSchema or expose it as the id attribute, though, given that the array item ids have no meaning apart from being unique and stable (as opposed to the other ids in the idSchema).

Here are two possible solutions:

  • Can we expose the key as data-rjsf-itemkey or something instead?
  • Or, we don't expose the key at all -- in our tests, create a custom ArrayFieldTemplate that exposes the key in the DOM so that the tests can access it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I removed the id attribute, and updated the tests to use a custom template.

@epicfaace
Copy link
Member

Great, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Removing or reordering an ArrayField breaks app Array item keys should not be based on array index
3 participants