Skip to content
This repository has been archived by the owner on Oct 1, 2024. It is now read-only.

✨ better behaviour for <FormState /> initialValue changes #446

Merged
merged 3 commits into from
Jan 4, 2019

Conversation

marutypes
Copy link
Contributor

@marutypes marutypes commented Dec 20, 2018

fixes #323 (hopefully, please let me know if this doesn't work for you)

What this?

As seen in the issue above, <FormState />'s behaviour around resetting could be problematic for some usecases. To address this I've added a new prop.

You can now control how <FormState /> reacts to changes in the initialValue prop using onInitialValueChanged. This prop has three options:

  • (default) reset-all: Reset the entire form when initialValues changes.
  • reset-where-changed: Reset only the changed field objects when initialValues changes.
  • ignore: Ignore changes to the initialValues prop. This option makes <FormState /> behave like a fully controlled component. You will generally want to accompany this option with a key or ref.

In adding this I had to make some calls around whether to reset errors / dirty state, and I tried to do the right thing. Let me know if it's bad.

I also ended up moving formState#reset into the public namespace like validateForm so that it can be used with onInitialValueChanged: ignore if preferred to using a key.

Yeah but I want even more control

If there's a way we can make a simple api like this work for you I'd really rather do that. If it's really not doable, we can change this api to be

onInitialValueChanged: 
| 'reset-all'
| 'reset-changed'
| 'ignore'
| typeof FormState.getDerivedStateFromProps

In which case the user would be able to do literally anything they wanted. I had suggested something similar (but scoped to controlling how a given key of fields is reconciled) in the issue, but I'd rather avoid the 'cooking with flamethrowers' approach if we can.

Can I try?

Since the tophat script is still not merged and we don't have a playground yet, I made this sandbox:

https://codesandbox.io/s/z77yjjvwp

It might be a little out of date around prop names because I've changed them a thousand times, but it should give you an idea how this looks.

I think reset-where-changed should be the default

Same, but I want to do a relatively large 1.0+ bump sometime soon, so since that would be a pretty large breaking change lets see if that bears out and do it then :)

@@ -38,6 +38,53 @@ class MyComponent extends React.Component {

If you need to do something immediately on mount you could also use old fashioned [callback refs](https://reactjs.org/docs/refs-and-the-dom.html#callback-refs).

## My form keeps resetting for no reason! / My form is resetting whenever I change an input!
Copy link
Contributor Author

Choose a reason for hiding this comment

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

this is probably the most asked question I get, so I snuck it in here since it's sorta on theme

@marutypes marutypes force-pushed the formstate-derivation-options branch from 034ca5c to 2ddde3e Compare December 20, 2018 01:23
@@ -52,6 +53,7 @@ interface Props<Fields> {
validators?: Partial<ValidatorDictionary<Fields>>;
onSubmit?: SubmitHandler<Fields>;
validateOnSubmit?: boolean;
onInitialValuesChange?: 'reset-all' | 'reset-where-changed' | 'ignore';
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm thinking it would be helpful/safer to make these values constants or maybe an exported enum?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

For those with typescript supporting editors string unions autocomplete just as well either way, for those without they feel like a more naturalJS API. Personally for component apis I enjoy the convenience provided by string unions.

Copy link
Contributor

Choose a reason for hiding this comment

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

For those with typescript supporting editors string unions autocomplete just as well either way

TIL

Copy link
Contributor Author

Choose a reason for hiding this comment

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

image
:)

Copy link
Contributor

@rexmac rexmac left a comment

Choose a reason for hiding this comment

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

LGTM. ❤️ the FAQ update!

Copy link
Contributor

@rox163 rox163 left a comment

Choose a reason for hiding this comment

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

🙏 lgtm!

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

Successfully merging this pull request may close these issues.

Can we have fine grain control over a Forms fields dirty state
3 participants