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

Add index to FormDataConsumer when used inside an ArrayInput #10498

Open
dalen opened this issue Feb 6, 2025 · 2 comments
Open

Add index to FormDataConsumer when used inside an ArrayInput #10498

dalen opened this issue Feb 6, 2025 · 2 comments

Comments

@dalen
Copy link

dalen commented Feb 6, 2025

Is your feature request related to a problem? Please describe.

When writing some types of field level validators for fields inside an ArrayInput it would be useful to be able to get the index of the current component in some way. But I can also imagine other use cases for it.

Describe the solution you'd like

FormDataConsumer exposes formData & scopedFormData. I would like it to also expose index

Describe alternatives you've considered

Additional context

@djhi
Copy link
Collaborator

djhi commented Feb 11, 2025

This is not properly documented but there is a useSimpleFormIteratorItem hook that returns the index as you can see in the <SimpleFormIterator> documentation.

However, this implies to create a component which FormDataConsumer was actually meant to avoid. Can you open a PR for this targeting the next branch?

@afilp
Copy link
Contributor

afilp commented Feb 20, 2025

@djhi I am also interested in having the index somehow, as I need it for onChange cases.

Use Case:
I want on onChange of an input that is inside an ArrayInput to be able to change the value of a sibling input that is in the same scopedFormData (i.e. in the same index on the ArrayInput). I do not find a way to do this using the formMethods of react-hook-form as I do not have the index to know which one to change, I do not have the path of the object.

I need to do something like that, to change the sibling url input on the onChange of a title input:

    formMethods.setValue(`${path}url`, null, {
      shouldDirty: true,
      shouldTouch: true,
    }) 

but, how can someone get this path? (i.e. the index of the current "record item" of the ArrayInput?

I cannot use a hook there, inside the onChange callback, so IF index or path was also exposed on onChange that would be great!

I believe it would be very useful to expose the path and/or the index of the item.
(the index is OK, but the total form's field path is also needed, as there may be nested-of-nested-of-nested ArrayInput fields...)

Thanks a lot!

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

No branches or pull requests

3 participants