Skip to content

Commit 20b79f1

Browse files
committed
#149 add support for Actions.GridActions.insertRow
1 parent 9df45af commit 20b79f1

File tree

5 files changed

+136
-11
lines changed

5 files changed

+136
-11
lines changed

src/actions/GridActions.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ import {
88
SET_SORT_DIRECTION,
99
SET_TREE_DATA_PARTIAL,
1010
SET_TREE_NODE_VISIBILITY,
11-
SORT_DATA
11+
SORT_DATA,
12+
INSERT_ROW
1213
} from '../constants/ActionTypes';
1314

1415
import { setLoaderState } from '../actions/plugins/loader/LoaderActions';
@@ -402,6 +403,15 @@ export const setTreeData = ({
402403
};
403404
};
404405

406+
export const insertRow = ({
407+
stateKey, data, index = 0
408+
}) => ({
409+
type: INSERT_ROW,
410+
stateKey,
411+
data,
412+
index
413+
});
414+
405415
export const setTreeNodeVisibility = ({
406416
id, visible, stateKey, showTreeRootNode
407417
}) => ({

src/constants/ActionTypes.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ export const SORT_REMOTE = '@@react-redux-grid/SORT_REMOTE';
77
export const GET_DATA = '@@react-redux-grid/GET_DATA';
88
export const SET_DATA = '@@react-redux-grid/SET_DATA';
99
export const SORT_DATA = '@@react-redux-grid/SORT_DATA';
10+
export const INSERT_ROW = '@@react-redux-grid/INSERT_ROW';
1011

1112
export const SET_SELECTION = '@@react-redux-grid/SET_SELECTION';
1213
export const SELECT_ALL = '@@react-redux-grid/SELECT_ALL';

src/reducers/actionHelpers/datasource.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,21 @@ export const saveRow = (state, { rowIndex, stateKey, values }) => {
252252
}, DataSource, 'mergeIn');
253253
};
254254

255+
export const insertRow = (state, { data, index, stateKey }) => {
256+
const prevData = state.getIn([stateKey, 'data']);
257+
const newData = prevData
258+
? prevData.insert(index, fromJS(data))
259+
: new List(fromJS(data));
260+
261+
return getUpdatedRecord(state, stateKey, {
262+
data: newData,
263+
proxy: newData,
264+
currentRecords: newData,
265+
total: newData.size,
266+
lastUpdate: generateLastUpdate()
267+
}, DataSource, 'mergeIn');
268+
};
269+
255270
export const sortData = (state, { data, stateKey }) => getUpdatedRecord(
256271
state, stateKey, {
257272
data: data,

src/reducers/components/datasource.js

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,26 +12,28 @@ import {
1212
SET_TREE_NODE_VISIBILITY,
1313
SET_TREE_DATA_PARTIAL,
1414
SORT_DATA,
15-
UPDATE_ROW
15+
UPDATE_ROW,
16+
INSERT_ROW
1617
} from '../../constants/ActionTypes';
1718

1819
import
1920
handleActions
2021
from './../../util/handleActions';
2122

2223
import {
23-
setData,
24-
setPartialTreeData,
25-
dismissEditor,
26-
removeRow,
27-
updateRow,
2824
addNewRow,
25+
clearFilter,
26+
dismissEditor,
27+
filterData,
28+
insertRow,
2929
moveNode,
30-
setTreeNodeVisibility,
30+
removeRow,
3131
saveRow,
32+
setData,
33+
setPartialTreeData,
34+
setTreeNodeVisibility,
3235
sortData,
33-
filterData,
34-
clearFilter
36+
updateRow
3537
} from './../actionHelpers/datasource';
3638

3739
const initialState = new OrderedMap();
@@ -41,6 +43,7 @@ export default handleActions({
4143
[CLEAR_FILTER_LOCAL]: clearFilter,
4244
[DISMISS_EDITOR]: dismissEditor,
4345
[FILTER_DATA]: filterData,
46+
[INSERT_ROW]: insertRow,
4447
[MOVE_NODE]: moveNode,
4548
[REMOVE_ROW]: removeRow,
4649
[SAVE_ROW]: saveRow,

test/reducers/components/datasource.test.js

Lines changed: 97 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ import {
1111
SORT_DATA,
1212
CLEAR_FILTER_LOCAL,
1313
FILTER_DATA,
14-
UPDATE_ROW
14+
UPDATE_ROW,
15+
INSERT_ROW
1516
} from './../../../src/constants/ActionTypes';
1617

1718
import
@@ -133,6 +134,101 @@ describe('The grid dataSource reducer setData func', () => {
133134

134135
});
135136

137+
describe('The grid dataSource reducer insertRow func', () => {
138+
beforeEach(() => resetLastUpdate());
139+
140+
it('Should insert an item into dataSource', () => {
141+
142+
const inState = testState({
143+
'test-grid': new DataSourceRecord({
144+
data: fromJS([
145+
{ cell: 1 },
146+
{ cell: 2 }
147+
]),
148+
total: 2
149+
})
150+
});
151+
152+
const action = {
153+
stateKey: 'test-grid',
154+
data: { cell: 43 },
155+
index: 1,
156+
type: INSERT_ROW
157+
};
158+
159+
expect(
160+
dataSource(inState, action).get('test-grid')
161+
).toEqualImmutable(
162+
new DataSourceRecord({
163+
proxy: fromJS([
164+
{ cell: 1 },
165+
{ cell: 43 },
166+
{ cell: 2 }
167+
]),
168+
total: 3,
169+
data: fromJS([
170+
{ cell: 1 },
171+
{ cell: 43 },
172+
{ cell: 2 }
173+
]),
174+
currentRecords: fromJS([
175+
{ cell: 1 },
176+
{ cell: 43 },
177+
{ cell: 2 }
178+
]),
179+
isEditing: false,
180+
lastUpdate: 1
181+
})
182+
);
183+
184+
});
185+
186+
it('Should insert an item to beginning of dataSource', () => {
187+
188+
const inState = testState({
189+
'test-grid': new DataSourceRecord({
190+
data: fromJS([
191+
{ cell: 1 },
192+
{ cell: 2 }
193+
]),
194+
total: 2
195+
})
196+
});
197+
198+
const action = {
199+
stateKey: 'test-grid',
200+
data: { cell: 43 },
201+
type: INSERT_ROW
202+
};
203+
204+
expect(
205+
dataSource(inState, action).get('test-grid')
206+
).toEqualImmutable(
207+
new DataSourceRecord({
208+
proxy: fromJS([
209+
{ cell: 43 },
210+
{ cell: 1 },
211+
{ cell: 2 }
212+
]),
213+
total: 3,
214+
data: fromJS([
215+
{ cell: 43 },
216+
{ cell: 1 },
217+
{ cell: 2 }
218+
]),
219+
currentRecords: fromJS([
220+
{ cell: 43 },
221+
{ cell: 1 },
222+
{ cell: 2 }
223+
]),
224+
isEditing: false,
225+
lastUpdate: 1
226+
})
227+
);
228+
229+
});
230+
});
231+
136232
describe('The grid dataSource reducer dissmissEditor func', () => {
137233
beforeEach(() => resetLastUpdate());
138234

0 commit comments

Comments
 (0)