Skip to content

Commit

Permalink
fix(react-grid): fix the editing state mutation (#279)
Browse files Browse the repository at this point in the history
  • Loading branch information
SergeyAlexeev authored Aug 28, 2017
1 parent 4c5b5ca commit a120730
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 19 deletions.
16 changes: 10 additions & 6 deletions packages/dx-grid-core/src/plugins/editing-state/reducers.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ export const stopEditRows = (prevEditingRows, { rowIds }) => {
export const addRow = (addedRows, { row }) => [...addedRows, row];

export const changeAddedRow = (addedRows, { rowId, change }) => {
const result = addedRows.slice();
result[rowId] = Object.assign({}, result[rowId], change);
const result = Array.from(addedRows);
result[rowId] = { ...result[rowId], ...change };
return result;
};

Expand All @@ -27,13 +27,17 @@ export const cancelAddedRows = (addedRows, { rowIds }) => {

export const changeRow = (prevChangedRows, { rowId, change }) => {
const prevChange = prevChangedRows[rowId] || {};
const result = Object.assign({}, prevChangedRows);
result[rowId] = Object.assign(prevChange, change);
return result;
return {
...prevChangedRows,
[rowId]: {
...prevChange,
...change,
},
};
};

export const cancelChanges = (prevChangedRows, { rowIds }) => {
const result = Object.assign({}, prevChangedRows);
const result = { ...prevChangedRows };
rowIds.forEach((rowId) => {
delete result[rowId];
});
Expand Down
28 changes: 15 additions & 13 deletions packages/dx-grid-core/src/plugins/editing-state/reducers.test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import Immutable from 'seamless-immutable';

import {
startEditRows,
stopEditRows,
Expand All @@ -13,7 +15,7 @@ import {
describe('EditingState reducers', () => {
describe('#startEditRows', () => {
it('should work', () => {
const editingRows = [1];
const editingRows = Immutable([1]);
const payload = { rowIds: [2, 3] };

const nextEditingRows = startEditRows(editingRows, payload);
Expand All @@ -22,7 +24,7 @@ describe('EditingState reducers', () => {
});
describe('#stopEditRows', () => {
it('should work', () => {
const editingRows = [1, 2, 3];
const editingRows = Immutable([1, 2, 3]);
const payload = { rowIds: [2] };

const nextEditingRows = stopEditRows(editingRows, payload);
Expand All @@ -31,7 +33,7 @@ describe('EditingState reducers', () => {
});
describe('#addRow', () => {
it('should work', () => {
const addedRows = [{ a: 1 }];
const addedRows = Immutable([{ a: 1 }]);
const payload = { row: { a: 2 } };

const nextAddedRows = addRow(addedRows, payload);
Expand All @@ -40,7 +42,7 @@ describe('EditingState reducers', () => {
});
describe('#changeAddedRow', () => {
it('should work', () => {
const addedRows = [{ a: 1 }, { a: 2 }];
const addedRows = Immutable([{ a: 1 }, { a: 2 }]);
const payload = { rowId: 0, change: { a: 3 } };

const nextAddedRows = changeAddedRow(addedRows, payload);
Expand All @@ -49,7 +51,7 @@ describe('EditingState reducers', () => {
});
describe('#cancelAddedRows', () => {
it('should work', () => {
const addedRows = [{ a: 1 }, { a: 2 }];
const addedRows = Immutable([{ a: 1 }, { a: 2 }]);
const payload = { rowIds: [0] };

const nextAddedRows = cancelAddedRows(addedRows, payload);
Expand All @@ -58,9 +60,9 @@ describe('EditingState reducers', () => {
});
describe('#changeRow', () => {
it('should work on the first change', () => {
const changedRows = {
const changedRows = Immutable({
o1: { a: 1 },
};
});
const payload = { rowId: 'o2', change: { a: 2 } };

const nextChangedRows = changeRow(changedRows, payload);
Expand All @@ -70,9 +72,9 @@ describe('EditingState reducers', () => {
});
});
it('should work on the second change', () => {
const changedRows = {
const changedRows = Immutable({
o1: { a: 1 },
};
});
const payload = { rowId: 'o1', change: { a: 2 } };

const nextChangedRows = changeRow(changedRows, payload);
Expand All @@ -83,10 +85,10 @@ describe('EditingState reducers', () => {
});
describe('#cancelChanges', () => {
it('should work', () => {
const changedRows = {
const changedRows = Immutable({
o1: { a: 1 },
o2: { a: 2 },
};
});
const payload = { rowIds: ['o2'] };

const nextChangedRows = cancelChanges(changedRows, payload);
Expand All @@ -97,7 +99,7 @@ describe('EditingState reducers', () => {
});
describe('#deleteRows', () => {
it('should work', () => {
const deletedRows = [1];
const deletedRows = Immutable([1]);
const payload = { rowIds: [2] };

const nextDeletedRows = deleteRows(deletedRows, payload);
Expand All @@ -106,7 +108,7 @@ describe('EditingState reducers', () => {
});
describe('#cancelDeletedRows', () => {
it('should work', () => {
const deletedRows = [1, 2];
const deletedRows = Immutable([1, 2]);
const payload = { rowIds: [2] };

const nextDeletedRows = cancelDeletedRows(deletedRows, payload);
Expand Down

0 comments on commit a120730

Please sign in to comment.