Skip to content

feat: expose index in FormDataConsumer - #11343

Open
Aayush5154 wants to merge 2 commits into
marmelab:nextfrom
Aayush5154:fix/form-data-consumer-index
Open

feat: expose index in FormDataConsumer#11343
Aayush5154 wants to merge 2 commits into
marmelab:nextfrom
Aayush5154:fix/form-data-consumer-index

Conversation

@Aayush5154

Copy link
Copy Markdown

Fixes #10498

Problem

FormDataConsumer exposes formData and scopedFormData, but does not expose the index of the current item when used inside an ArrayInput.

Solution

Expose the current ArrayInput item index through FormDataConsumerRenderParams.

The index is extracted from the array source and passed to the render function.

How To Test

  • Added a regression test for FormDataConsumer inside an ArrayInput.
  • Verified that the index is correctly exposed for multiple array items.
  • All test suites pass.

Additional Checks

  • The PR targets next for a feature
  • The PR includes unit tests
  • The PR includes no unnecessary changes

if (arraySource) {
const scopedFormData = get(formData, arraySource);
result = children({ formData, scopedFormData });
const index = Number(arraySource.match(/\d+$/)?.[0]);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This won't work for nested arrays (e.g. some.1.foo.2.bar). You should instead take the last match I believe.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Updated to use matchAll and take the last match to handle nested arrays correctly. Also added a test for nested ArrayInput.

@fzaninotto fzaninotto left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Nice! But could you also update the doc to make this new feature discoverable? E.g. in https://marmelab.com/react-admin/Inputs.html#linking-two-inputs

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.

3 participants