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

Adding item to field array mutates initialValue #627

Open
marcomuser opened this issue May 4, 2024 · 4 comments
Open

Adding item to field array mutates initialValue #627

marcomuser opened this issue May 4, 2024 · 4 comments

Comments

@marcomuser
Copy link
Contributor

Describe the bug and the expected behavior

When adding an item to a field array, conform automatically adds a new array with a value of type undefined to both form.value as well as form.initialValue. I would not expect it to mutate the initialValue object before the form is actually successfully submitted.

Conform version

v1.1.3

Steps to Reproduce the Bug or Issue

  1. Go to reproduction repo: https://stackblitz.com/edit/vitejs-vite-gayrv1?file=src%2FApp.tsx
  2. Add a couple of items in the UI
  3. Notice that it directly adds new items to the initialValue object

What browsers are you seeing the problem on?

No response

Screenshots or Videos

No response

Additional context

No response

@edmundhung
Copy link
Owner

This is working as designed. The initialValue is exactly used to tell react what the initial value should be for each field. Are you looking for the defaultValue you set to useForm? It is not exposed at the moment and I will consider exposing it if there are valid usecase.

@marcomuser
Copy link
Contributor Author

🙈 I was looking for defaultValue, indeed. This is the second time I'm falling into this trap confusing initialValue and defaultValue. My bad. If I understand you correctly initialValue is only relevant for telling react with what initial value it should render uncontrolled html inputs. Is that correct? Would you be open to add JSDoc comments to these properties to get inline documentation via intellisense? Happy to open a PR (once I understand the difference precisely ofc 😸)

@edmundhung
Copy link
Owner

edmundhung commented May 5, 2024

I think many people will find it confusing to see both defaultValue and initialValue exists in the metadata. But they are different in the sense that defaultValue will not be changed once the form is initialized until form reset happens, while initialValue will be updated as you do different form operations like insert, update or reset etc. For example, when you insert a new item with default value "foobar", Conform will update the initialValue so that it sets the defaultValue of the new input element to "foobar". This is different from the actual default value of the form in which it would be undefined because of a new field.

What do you need the defaultValue for?

@marcomuser
Copy link
Contributor Author

For deriving state whether the user already submitted values before or not when loading the page in the context of an offline-first SPA. I don't necessarily need the defaultValue as part of the form context for that. I was just confused because I expected the initialValue to give me what I'm looking for. If I have time I'll look into providing a PR for adding JSDoc to the FormMetadata interface so that we would get inline documentation for all the properties. I have a feeling that I'm not the only one that is sometimes unclear about what all the form properties like initialValue, dirty etc. are actually reflecting and for what they can be relied upon. Thank you a ton for all the clarifications! ❤️

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

No branches or pull requests

2 participants