question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

form.resetDirty() expected behavior?

See original GitHub issue

What package has an issue

@mantine/form

Describe the bug

Before diving into the code, I just wanted to ask if this is expected behavior. It doesn’t seem like it is, but maybe I’m using the API incorrectly.

Here’s the setup. We have a form with a single field, which initially has a value of “a”. Since it’s not dirty, the “Save” button is disabled. The user changed the input value to “b”. The form is now dirty so the button is enabled.

The user clicks “Save”, some network stuff happens (aka, the db is synced with the form data) and then we call form.resetDirty(). Everything is synced, the form data is clean and the button is disabled again.

Awesome. So far…

Now change the value back to “a”. Uh oh. The form isn’t dirty and the user can’t save. The user can now change the value to “b” and it’s still dirty. The user can set the value to anything other than the initial value and it’s still dirty.

Now it seems we have a mess on our hands.

import { TextInput, Text, Button, MantineProvider } from "@mantine/core";
import { useForm } from "@mantine/form";

export default function App() {
  const form = useForm({ initialValues: { text: "a" } });

  const handleSubmit = () => {
    // mutateSomeData(form.values);
    form.resetDirty();
  };

  return (
    <MantineProvider withGlobalStyles withNormalizeCSS>
      <form onSubmit={form.onSubmit(handleSubmit)}>
        <TextInput {...form.getInputProps("text")} label="Touched/dirty demo" />
        <Button type="submit" mt="lg" disabled={!form.isDirty()}>
          Save
        </Button>
      </form>

      <Text size="sm" mt="sm">
        Touched:{" "}
        <Text span color={form.isTouched("text") ? "blue" : "red"}>
          {form.isTouched("text") ? "touched" : "not touched"}
        </Text>
      </Text>

      <Text size="sm">
        Dirty:{" "}
        <Text span color={form.isDirty("text") ? "blue" : "red"}>
          {form.isDirty("text") ? "dirty" : "not dirty"}
        </Text>
      </Text>
    </MantineProvider>
  );
}

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

5.1.3

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

https://codesandbox.io/s/dreamy-satoshi-sfkijt?file=/src/App.tsx

Do you know how to fix the issue

No

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

Yes

Possible fix

I haven’t looked at the source, but it seems like isDirty() is just comparing the initial state with the current state, whereas if resetDirty() is invoked, isDirty() should compare the state when resetDirty() was last invoked.

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
rtivitalcommented, Aug 17, 2022

Hi @bbar, currently form.resetDirty() compares values with initialValues, but I think we can change the logic to make it work as you’ve proposed. I’ll change it in the next minor release.

0reactions
rtivitalcommented, Aug 18, 2022

Released in 5.2.0

Read more comments on GitHub >

github_iconTop Results From Across the Web

Form reset resets dirty fields when added to omitResetState
Describe the bug When resetting a form that has been given {dirtyFields: true} in the omitResetState config, dirty fields are reset.
Read more >
angularjs - How to reset $dirty in form - Stack Overflow
According to my observation it set $dirty value of every field in your form by placing ng-dirty class. Even if you remove that...
Read more >
Input controls remain dirty and not pristine after form.reset() in ...
When calling form.reset() after changing a controls value, I would expect the controls to be reset to dirty: false, pristine: true, touched: false....
Read more >
EditForm, NewForm, SubmitForm, ResetForm, and ViewForm ...
The ResetForm function resets the contents of a form to their initial values, before the user made any changes. If the form is...
Read more >
FormGroup - Angular
You reset to a specific form state by passing in a map of states that matches the structure of your form, with control...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found