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

[@mantine/form] Issues resetting dirty and touched #3329

Closed
JonatanLindstrom opened this issue Jan 9, 2023 · 2 comments
Closed

[@mantine/form] Issues resetting dirty and touched #3329

JonatanLindstrom opened this issue Jan 9, 2023 · 2 comments

Comments

@JonatanLindstrom
Copy link

JonatanLindstrom commented Jan 9, 2023

What package has an issue

@mantine/form

Background

I've found issues with using resetDirty in my project (the issue exists in 5.9.0 and previous versions), when trying to reproduce and test if a newer version would resolve it, I got an even worse behaviour. I'll therefore divide this into two parts, one for my original issue in 5.9.0 and another part regarding ^5.9.1 to latest (version 5.10.0 as of writing).

Original issue found in version up to 5.9.0

When running setFieldValue and resetDirty in the same callback it causes a race condition, that I currently can only solve using a setTimeout to delay the resetDirty. I think it could be beneficial to allow programmatically changing an input value without flipping its dirty state.

The use-case I have is that I want to give a suggested name dependant on another field, as long as the user hasn't modified the input manually. (I'll also allow it to suggest a new name if the user manually cleared the field)

While the setTimeout works as a workaround, I'd propose adding options to setFieldValue so that one can set the value without modifying dirty, e.g.

type SetFieldValue = (path, value, options?: Options) => void
type Options = {
  shouldUpdateDirtyState?: boolean
}

Note: During this I've only written dirty but in my experience using isTouched and resetTouched gave the same results. Therefore if this were to be implemented, options for both dirty and touched should be considered.

Issue in ^5.9.1 (which as of writing resolves to 5.10.0 as latest)

Invoking resetDirty, no matter a timeout to delay or not, does not reset the dirty state for the field. As #3025 was merged in that version, which changes this code specifically, it might be worth revisiting those changes as a start in debugging.

Note: This new issue only regards dirty and NOT touched state.

What version of @mantine/hooks page do you have in package.json?

First bug: 5.9.0
Second bug: 5.9.1 - 5.10.0

If possible, please include a link to a codesandbox with the reproduced problem

https://codesandbox.io/s/mantine-setfieldvalue-isdirty-resetdirty-2dxr3i?file=/src/App.tsx

Are you willing to participate in fixing this issue and create a pull request with the fix

Yes

@rtivital
Copy link
Member

rtivital commented Jan 9, 2023

You can update dirty values reference by calling form.resetDirty(newValues)https://codesandbox.io/s/mantine-setfieldvalue-isdirty-resetdirty-forked-9hgv38?file=/src/App.tsx

@JonatanLindstrom
Copy link
Author

Works like a charm! Unforturnately, I couldn't find my way to this from the docs, but thank you for pointing out this solution!

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