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

[Feature]: dirtyFields & isDirty #104

Open
guvenakcoban opened this issue Apr 30, 2022 · 5 comments
Open

[Feature]: dirtyFields & isDirty #104

guvenakcoban opened this issue Apr 30, 2022 · 5 comments
Labels
help wanted Extra attention is needed

Comments

@guvenakcoban
Copy link

What is the new or updated feature that you are suggesting?

dirtyFields and isDirty states like react-hook-form

Why should this feature be included?

It would be really useful If we can get isDirty state to use disabling submit button and/or we can post only dirty fields.

@airjp73
Copy link
Owner

airjp73 commented May 1, 2022

This is something I want to add, but not sure I can promise a timeline right now.

@ThomiReynoso
Copy link

It would be really useful to have this! I'm aware too!

@alexbu92
Copy link

Seconding this, it's necessary to know if the form is dirty to show reset button when dirty and disable submit button when not dirty.

@hauptrolle
Copy link

Would be awesome to have this!

It would be very useful for building an You have unsaved changes dialog when having a dirty form on a route change.

@airjp73 airjp73 added the help wanted Extra attention is needed label Oct 27, 2022
@airjp73
Copy link
Owner

airjp73 commented Oct 27, 2022

Not sure how much time I have to implement this, so I want to leave a workaround for anyone struggling with this.

You can handle most use-cases for this to some degree without an explicit dirtyFields state. You can add listen to change events for the whole form by adding a single change listener higher in the DOM. Here's an example:

const [hasUnsavedChanges, setHasUnsavedChanges] = useState(false);
return (
  <ValidatedForm validator={validator} onChange={() => setHasUnsavedChanges(true)}>
    {/* etc */}
  </ValidatedForm>
)

If you have inputs outside the form itself, you could attach the onChange to any element like a div.

const [hasUnsavedChanges, setHasUnsavedChanges] = useState(false);
return (
  <div onChange={() => setHasUnsavedChanges(true)}>
    <ValidatedForm validator={validator}>
      {/* etc */}
    </ValidatedForm>
  </div>
)

This won't handle 100% of cases, like if you have a custom component that only works through useControlField, but it should work for most people.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

5 participants