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

Auto-updating the field's value breaks when not using useForm's defaultValue #771

Closed
AndreSilva1993 opened this issue Sep 16, 2024 · 8 comments

Comments

@AndreSilva1993
Copy link

AndreSilva1993 commented Sep 16, 2024

Describe the bug and the expected behavior

Before #729 went in I could have something like this:

const schema = z.object({ displayName: z.string() });

const [form, fields] = useForm({
  onValidate({ formData }) {
    return parseWithZod(formData, { schema });
  },
});

const defaultValueForInput = "defaultValueForInput";

<Texfield
  name={fields.displayName.name}
  defaultValue={defaultValueForInput}
/>

The input was properly initialised with the provided default value and that'd be it. With the #729, either we provide a defaultValue to the useForm hook or the input's value will be overwritten to an empty text input / unchecked radio or checkbox.

I think such a change could either be marked as a major upgrade since the documentation / previous versions did not force users to provide the defaultValue to the useForm hook when wanting to pass a default value to their inputs - meaning that inputs that are initialised "out of the useForm realm" will be overwritten. Not necessarily arguing that this is better than using the defaultValue property in useForm since I can see arguments for both side, but it definitely caught me off-guard and broke my edit forms since they would be all empty by bumping to this version.

Or it could make sure that it only overwrites stuff if the defaultValue is provided and input is currently empty.

Conform version

v1.2.1

Steps to Reproduce the Bug or Issue

const schema = z.object({ displayName: z.string() });

const [fields] = useForm({ 
  onValidate: ({ formData }) => {
    return parseWithZod(formData, { schema });
  }
});

const inputDefaultValue = "default-value";

<input name={fields.displayName.name} defaultValue={inputDefaultValue} />

In here we can assert that the input will in fact be changed to an empty one.

What browsers are you seeing the problem on?

Chrome, Firefox, Microsoft Edge, Safari

Screenshots or Videos

No response

Additional context

No response

@itsmegood
Copy link

Same issue if my isActive boolean default value is set to true the form won't pick up the value until I turn the value off and then on again

@edmundhung
Copy link
Owner

Thanks for the writeup. I have gave it some thoughts last night and my current plan is to make this opt-in with an option to customize the behavior. I will put up a draft PR with more information later today. Sorry for the trouble!

@AndreSilva1993
Copy link
Author

Thanks for the writeup. I have gave it some thoughts last night and my current plan is to make this opt-in with an option to customize the behavior. I will put up a draft PR with more information later today. Sorry for the trouble!

That'd be perfect, thanks for the quick turnaround. It's really appreciated 👍

@MattyBalaam
Copy link

We have just noticed this… I am now conflicted about what pattern I prefer!

@edmundhung
Copy link
Owner

edmundhung commented Sep 17, 2024

I have put up a fix on #775. Can you give the pre-release a try and let me know if you run into any issues? Thanks.

@AndreSilva1993
Copy link
Author

AndreSilva1993 commented Sep 18, 2024

I have put up a fix on #775. Can you give the pre-release a try and let me know if you run into any issues? Thanks.

Tested these pre-release versions and no longer running into issues with my inputs being cleared due to the lack of defaultValue.

@edmundhung
Copy link
Owner

I'm sorry for the trouble. I've concluded that it's best to revert the relevant changes, and I've just published v1.2.2.

@AndreSilva1993
Copy link
Author

That works as well! Already tested 1.2.2 and can attest it fixes this issue, thus I'm closing this. 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

No branches or pull requests

4 participants