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

Access FieldArray Items ( not just index ) #243

Open
robodna opened this issue Aug 24, 2024 · 4 comments
Open

Access FieldArray Items ( not just index ) #243

robodna opened this issue Aug 24, 2024 · 4 comments
Assignees
Labels
question Further information is requested

Comments

@robodna
Copy link

robodna commented Aug 24, 2024

I'm using FieldArray. Is there a way to pass the array of items contained the in the field array, and not just the fieldArray.items array which is just a random number index?

I would like to pass them like <MyCustomComponent items={FieldArray.arrayItems}

MyCustomComponent uses a <For to display the FieldArray.arrayItems[].name object property in a single textarea type input box. ( not as single <option tags for example )

@fabian-hiller
Copy link
Owner

Have you read the field arrays guide? I recommend using <Field /> inside of <FieldArray /> and as a fallback you can call getValues(formStore, 'field_array_name').

@fabian-hiller fabian-hiller self-assigned this Aug 25, 2024
@fabian-hiller fabian-hiller added the question Further information is requested label Aug 25, 2024
@robodna
Copy link
Author

robodna commented Aug 25, 2024

I did read the guide and see how I can use <Field inside FieldArray, but that creates 1 element for each item. What would I pass to the element contained inside <Field? field.value would be just 1 value, and fieldArray.items is just a random number index. Perhaps I need to pass children elements to <MyCustomComponent instead of passing them via MyCustomComponent items={fieldArray.arrayItems} if that existed.

So instead of
<FieldArray> <For> <Field> <MyCustomComponent items={fieldArray.arrayItems}>

I would do

<FieldArray> <Field> <MyCustomComponent> <For> <MyCustomComponentChildItem>

@fabian-hiller
Copy link
Owner

The idea of Modular Forms is that every input is represented by an <input />, <select /> or <textarea /> element. That's why every input should should use it's own <Field /> to connect to the form store. You can also wrap two <Field /> components in each other to connect two inputs via a single component.

You can also break out of Modular Forms default pattern with methods like setValue, getValue and getValues but is is only recommended in special cases.

@robodna
Copy link
Author

robodna commented Aug 26, 2024

I was able to get this working by re-organizing my form and inputs.

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

No branches or pull requests

2 participants