Repository: jaredpalmer/formik (ts)

Issue:
resetForm should reinitialize this.initialValues even if nextValues is not provided
## Bug, Feature, or Question?

IMO Bug ;-)

## Current Behavior

Calling `resetForm` without `nextValues` causing that `dirty` stay equal to `true`, because `this.initialValues` is not updated.

## Desired Behavior

`resetForm` should also reset `this.initialValues` so `dirty` flag will be equal to `false` after reset. 

## Suggested Solutions

change this 3 lines https://github.com/jaredpalmer/formik/blob/master/src/formik.tsx#L574-L576
```
    if (nextValues) {
      this.initialValues = nextValues;
    }

```
on this 
```javascript
this.initialValues = nextValues ? nextValues : this.props.initialValues;
```







Pull request description:
resetForm should reinitialize this.initialValues even if nextValues is not provided
closes #441 

Patch:
diff --git a/src/formik.tsx b/src/formik.tsx
index 3142dcf..d80ae31 100644
--- a/src/formik.tsx
+++ b/src/formik.tsx
@@ -571,9 +571,9 @@ export class Formik<ExtraProps = {}, Values = object> extends React.Component<
   };
 
   resetForm = (nextValues?: Values) => {
-    if (nextValues) {
-      this.initialValues = nextValues;
-    }
+    const values = nextValues ? nextValues : this.props.initialValues;
+
+    this.initialValues = values;
 
     this.setState({
       isSubmitting: false,
@@ -581,7 +581,7 @@ export class Formik<ExtraProps = {}, Values = object> extends React.Component<
       touched: {},
       error: undefined,
       status: undefined,
-      values: nextValues ? nextValues : this.props.initialValues,
+      values,
     });
   };
 


Test patch:
diff --git a/test/formik.test.tsx b/test/formik.test.tsx
index a691157..c737697 100644
--- a/test/formik.test.tsx
+++ b/test/formik.test.tsx
@@ -1,7 +1,7 @@
 import * as React from 'react';
 
 import { Formik, FormikProps } from '../src/formik';
-import { shallow } from 'enzyme';
+import { shallow, mount } from 'enzyme';
 import { sleep, noop } from './testHelpers';
 
 interface Values {
@@ -16,6 +16,7 @@ const Form: React.SFC<FormikProps<Values>> = ({
   handleBlur,
   status,
   errors,
+  dirty,
   isSubmitting,
 }) => {
   return (
@@ -42,6 +43,25 @@ const BasicForm = (
   <Formik initialValues={{ name: 'jared' }} onSubmit={noop} component={Form} />
 );
 
+class WithState extends React.Component<{}, { data: { name: string } }> {
+  constructor(props: {}) {
+    super(props);
+    this.state = {
+      data: { name: 'ivan' },
+    };
+  }
+
+  render() {
+    return (
+      <Formik
+        initialValues={this.state.data}
+        onSubmit={noop}
+        component={Form}
+      />
+    );
+  }
+}
+
 describe('<Formik>', () => {
   it('should initialize Formik state and pass down props', () => {
     const tree = shallow(BasicForm);
@@ -963,5 +983,37 @@ describe('<Formik>', () => {
 
       expect((tree.instance() as any).resetForm).toHaveBeenCalledWith('data');
     });
+
+    it('should reset dirty flag even if initialValues has changed', async () => {
+      const tree = mount(<WithState />);
+      expect(tree.find(Form).props().dirty).toEqual(false);
+
+      tree
+        .find(Form)
+        .find('input')
+        .simulate('change', {
+          persist: noop,
+          target: {
+            id: 'name',
+            value: 'Ian',
+          },
+        });
+
+      expect(tree.find(Form).props().dirty).toEqual(true);
+
+      tree.setState({ data: { name: 'Jared' } });
+
+      await tree
+        .find(Form)
+        .props()
+        .handleReset();
+
+      expect(
+        tree
+          .update()
+          .find(Form)
+          .props().dirty
+      ).toEqual(false);
+    });
   });
 });