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

Initialize with keepDirty and empty array creates an empty object in form values #3095

Closed
percevalw opened this issue Jun 23, 2017 · 2 comments

Comments

@percevalw
Copy link
Contributor

Are you submitting a bug report or a feature request?

Bug report

What is the current behavior?

Hi, I'm working on an array structure form. When I delete a field in the array list that was before set to a 'empty-like' value {name: ""}, and triggers initialize with keepDirty option and an new empty initial value to make it pristine, the ReduxForm reducer creates an empty object in the state values.

What is the expected behavior?

The values array should not be altered by the reducer as the keepDirty parameter is set to true and the field indeed dirty.

Sandbox Link

https://www.webpackbin.com/bins/-KnHcmQvY3yKJ4ZF8m0C

What's your environment?

Any environment

Other information

import { reducer, initialize } from 'redux-form';
import structure from 'redux-form/es/structure/plain';
const { deepEqual, fromJS } = structure;

const before = {
  myForm: {
    registeredFields: {
      // field has been deleted in array before so fields have been unregistered
      myList: { name: 'myList', type: 'Field', count: 0 },
      'myList.0.name': { name: 'myList.0.name', type: 'Field', count: 0 },
    },
    values: {
      myList: [], // field has been deleted in array before
    },
    initial: {
      myList: [{ name: '' }], // but was still present in initial values, with an empty string value
    },
  },
};

// let's submit the data to the server, and the form will update its initialValues
// either programmatically with an action or with enableReinitialize for example
// so the new initialValues should now be
const actionInitialValues = [];
const actionKeepDirty = true;

const state = reducer(
  fromJS(before),
  initialize('myForm', actionInitialValues, actionKeepDirty),
);

console.log(JSON.stringify({
  myForm: {
    registeredFields: {
      // this part is not affected
      myList: { name: 'myList', type: 'Field', count: 0 },
      'myList.0.name': { name: 'myList.0.name', type: 'Field', count: 0 },
    },
    values: {
      // this is the problematic part, as it should be empty
      myList: [],
    },
    initial: {
      // new initial values are indeed modified to empty array
      myList: [],
    },
  },
}, null, 2));

console.log(JSON.stringify(state, null, 2));
       }
     },
     "values": {
-      "myList": []
+      "myList": [
+        {}
+      ]
     },
     "initial": {
       "myList": []

In the createReducer/js file :
The deepEqual function is guilty in part, because it considers [{name: ''}] and [] equals regarding the myList.0.name field. Therefore, since the old value is equal to the old initial value, it sets the new value equal to the new initial value to keep it pristine, which creates an empty object to set the 0.name property to undefined.

@erikras
Copy link
Member

erikras commented Jul 11, 2017

Fix published in v7.0.0.

@lock
Copy link

lock bot commented Jul 11, 2018

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@lock lock bot locked as resolved and limited conversation to collaborators Jul 11, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants