Skip to content
This repository was archived by the owner on Apr 5, 2022. It is now read-only.

Commit 07cc78b

Browse files
authored
Merge pull request #317 from cjdev/add-selected-row-to-data-table
Add selected rows to data table
2 parents 6ce08c7 + ad86cc0 commit 07cc78b

11 files changed

Lines changed: 14157 additions & 1363 deletions

File tree

CHANGELOG.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
11
# Upcoming
22

3+
## New Feature
4+
5+
- Add selectable rows to DataTable.
6+
7+
## Breaking Changes in DataTable
8+
9+
- renderToolbar's data prop now return an array of objects instead of array of arrays.
10+
- Before: data = [['first', 'last']], After: data = [{row: ['first', 'last'], selected: true}]
11+
12+
# Upcoming
13+
314
## New Component
415

516
- Add Chip component

packages/visual-stack-docs/src/containers/Components/Docs/table.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ export default () => (
146146
)}
147147
sortable
148148
pagination
149+
selectable
149150
/>
150151
{/* s7:end */}
151152

packages/visual-stack-redux/package-lock.json

Lines changed: 13791 additions & 1273 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/visual-stack-redux/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
},
3838
"dependencies": {
3939
"ramda": "^0.26.1",
40-
"redux-actions": "^0.9.1"
40+
"redux-actions": "^2.6.5"
4141
},
4242
"devDependencies": {
4343
"@babel/cli": "^7.2.3",

packages/visual-stack-redux/src/actions.js

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { createAction, handleActions } from 'redux-actions';
33
import { sortData } from '@cjdev/visual-stack/lib/components/Table/DataTable/sortingHelper';
44

55
const defaultToEmpty = R.defaultTo({});
6+
const defaultToEmptyArray = R.defaultTo([]);
67
const collapse = R.set(R.lensProp('expanded'), false);
78

89
const OPEN_MODAL = '@cjdev/visual-stack-redux/OPEN_MODAL';
@@ -115,10 +116,13 @@ const findSortingIndex = (sortingOption, columns) =>
115116
R.findIndex(R.propEq('label', sortingOption.label))(columns);
116117

117118
const getDataTableData = ({ columns, data, sortingOption }) => {
118-
if (!sortingOption) return data;
119+
const dataWithSelectedProp = defaultToEmptyArray(data).map((row, index) => {
120+
return { id: index, row, selected: false };
121+
});
122+
if (!sortingOption) return dataWithSelectedProp;
119123

120124
const sortingIndex = findSortingIndex(sortingOption, columns);
121-
return sortData(sortingIndex, sortingOption.order, data);
125+
return sortData(sortingIndex, sortingOption.order, dataWithSelectedProp);
122126
};
123127

124128
const INITIALIZE_DATA_TABLE = '@cjdev/visual-stack-redux/INITIALIZE_DATA_TABLE';
@@ -133,6 +137,9 @@ export const setDataTableSortingOption = createAction(
133137
SET_DATA_TABLE_SORTING_OPTION
134138
);
135139

140+
const SET_DATA_TABLE_ROWS = '@cjdev/visual-stack-redux/SET_DATA_TABLE_ROWS';
141+
export const setDataTableRows = createAction(SET_DATA_TABLE_ROWS);
142+
136143
export const selectDataTable = id => state => {
137144
const dataTable = R.view(R.lensPath(['visualStack', 'dataTable', id]))(state);
138145
const defaultTable = { data: [], pagination: {} };
@@ -211,7 +218,6 @@ export default handleActions(
211218
R.set(R.lensPath(['slidingPanel', filterLabel, 'expanded']), true, state),
212219
[SET_SLIDING_PANEL_ACTIVE_STATE]: (state, { payload: newActiveState }) =>
213220
R.set(R.lensPath(['slidingPanel', 'active']), newActiveState, state),
214-
215221
[SELECT_TAB]: (state, { payload: { tabLayoutId, index } }) => {
216222
return R.set(
217223
R.lensPath(['tabLayout', tabLayoutId, 'index']),
@@ -264,8 +270,8 @@ export default handleActions(
264270
),
265271
[RESET_CALENDAR_SELECTION]: (state, { payload: { datePickerId } }) =>
266272
R.set(R.lensPath(['datePicker', datePickerId]), {}, state),
267-
[INITIALIZE_DATA_TABLE]: (state, { payload }) =>
268-
R.set(
273+
[INITIALIZE_DATA_TABLE]: (state, { payload }) => {
274+
return R.set(
269275
R.lensPath(['dataTable', payload.id]),
270276
{
271277
data: getDataTableData(payload),
@@ -279,24 +285,22 @@ export default handleActions(
279285
)(payload),
280286
},
281287
state
282-
),
288+
);
289+
},
283290
[SET_DATA_TABLE_PAGE]: (state, { payload: { id, pagination } }) =>
284291
R.set(R.lensPath(['dataTable', id, 'pagination']), pagination, state),
285292
[SET_DATA_TABLE_SORTING_OPTION]: (
286293
state,
287-
{ payload: { id, data, sortingOption } }
294+
{ payload: { id, sortingOption } }
288295
) => {
289-
const stateWithSortingOption = R.set(
296+
return R.set(
290297
R.lensPath(['dataTable', id, 'sortingOption']),
291298
sortingOption,
292299
state
293300
);
294-
295-
return R.set(
296-
R.lensPath(['dataTable', id, 'data']),
297-
data,
298-
stateWithSortingOption
299-
);
301+
},
302+
[SET_DATA_TABLE_ROWS]: (state, { payload: { id, data } }) => {
303+
return R.set(R.lensPath(['dataTable', id, 'data']), data, state);
300304
},
301305
},
302306
{
@@ -309,5 +313,6 @@ export default handleActions(
309313
tabLayout: {},
310314
pagination: {},
311315
datePicker: {},
316+
dataTable: {},
312317
}
313318
);

packages/visual-stack-redux/src/components/DataTable/index.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
setDataTablePage,
77
setDataTableSortingOption,
88
selectDataTable,
9+
setDataTableRows,
910
} from '../../actions';
1011
import * as R from 'ramda';
1112
import PropTypes from 'prop-types';
@@ -48,12 +49,14 @@ export class DataTablePure extends Component {
4849
id,
4950
setDataTablePage,
5051
setDataTableSortingOption,
52+
setDataTableRows,
5153
onClick,
5254
renderToolbar,
5355
rowsPerPageTemplate,
5456
totalRecordsTemplate,
5557
isLoading,
5658
loadingMessage,
59+
selectable,
5760
} = this.props;
5861
return (
5962
<div>
@@ -63,6 +66,7 @@ export class DataTablePure extends Component {
6366
description={description}
6467
noDataLabel={noDataLabel}
6568
sortable={sortable}
69+
selectable={selectable}
6670
pagination={pagination}
6771
data={dataTable.data}
6872
isLoading={isLoading}
@@ -75,11 +79,15 @@ export class DataTablePure extends Component {
7579
setDataTablePage({ id, pagination });
7680
}}
7781
onSort={({ data, sortingOption }) => {
78-
setDataTableSortingOption({ id, data, sortingOption });
82+
setDataTableSortingOption({ id, sortingOption });
83+
setDataTableRows({ id, data });
7984
}}
8085
renderToolbar={renderToolbar}
8186
rowsPerPageTemplate={rowsPerPageTemplate}
8287
totalRecordsTemplate={totalRecordsTemplate}
88+
onSelect={({ data }) => {
89+
setDataTableRows({ id, data });
90+
}}
8391
/>
8492
</div>
8593
);
@@ -124,6 +132,7 @@ const mapDispatchToProps = {
124132
initializeDataTable,
125133
setDataTablePage,
126134
setDataTableSortingOption,
135+
setDataTableRows,
127136
};
128137

129138
export const DataTable = connect(

packages/visual-stack-redux/src/components/DataTable/tests/index.test.js

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,10 @@ describe('DataTablePure', () => {
5353
]);
5454
});
5555

56-
it('should call setDataTableSortingOption when sorting from VSDataTable', () => {
56+
it('should call setDataTableSortingOption and setDataTableRows when sorting from VSDataTable', () => {
5757
const id = '1';
5858
const setDataTableSortingOption = jest.fn();
59+
const setDataTableRows = jest.fn();
5960
const sortingPayload = {
6061
sortingOption: { label: 'id', order: 'ASCENDING' },
6162
data: [{ id: 1 }],
@@ -66,6 +67,7 @@ describe('DataTablePure', () => {
6667
dataTable={{ pagination: {} }}
6768
initializeDataTable={() => {}}
6869
setDataTableSortingOption={setDataTableSortingOption}
70+
setDataTableRows={setDataTableRows}
6971
/>
7072
);
7173
const { onSort } = wrapper.find(VSDataTable).props();
@@ -74,7 +76,41 @@ describe('DataTablePure', () => {
7476
[
7577
{
7678
id,
77-
...sortingPayload,
79+
sortingOption: sortingPayload.sortingOption,
80+
},
81+
],
82+
]);
83+
expect(setDataTableRows.mock.calls).toEqual([
84+
[
85+
{
86+
id,
87+
data: sortingPayload.data,
88+
},
89+
],
90+
]);
91+
});
92+
93+
it('should call setDataTableRows when selecting rows from VSDataTable', () => {
94+
const id = '1';
95+
const setDataTableRows = jest.fn();
96+
const payload = {
97+
data: [{ id: 1 }],
98+
};
99+
const wrapper = shallow(
100+
<DataTablePure
101+
id={id}
102+
dataTable={{ pagination: {} }}
103+
initializeDataTable={() => {}}
104+
setDataTableRows={setDataTableRows}
105+
/>
106+
);
107+
const { onSelect } = wrapper.find(VSDataTable).props();
108+
onSelect(payload);
109+
expect(setDataTableRows.mock.calls).toEqual([
110+
[
111+
{
112+
id,
113+
data: payload.data,
78114
},
79115
],
80116
]);

packages/visual-stack-redux/test/actions.test.js

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import reducer, {
1717
initializeDataTable,
1818
setDataTablePage,
1919
setDataTableSortingOption,
20+
setDataTableRows,
2021
} from '../src/actions';
2122
import { selectDataTable } from '../src/actions';
2223
import { DESCENDING } from '@cjdev/visual-stack/lib/components/Table/DataTable/sortingHelper';
@@ -34,6 +35,7 @@ describe('reducer', () => {
3435
tabLayout: {},
3536
pagination: {},
3637
datePicker: {},
38+
dataTable: {},
3739
});
3840
});
3941

@@ -354,20 +356,23 @@ describe('reducer', () => {
354356

355357
test('should initialize dataTable data', () => {
356358
const id = 'sample-data-table';
357-
const data = [
358-
{
359-
id: 1,
360-
},
361-
];
359+
const data = [[1, 'first name', 'last name']];
362360
const columns = [];
363361
const sortingOption = null;
364362
const beforeState = {};
365363
const afterState = reducer(
366364
beforeState,
367365
initializeDataTable({ id, data, columns, sortingOption })
368366
);
367+
const dataWithSelectedProp = [
368+
{
369+
id: 0,
370+
row: [1, 'first name', 'last name'],
371+
selected: false,
372+
},
373+
];
369374
expect(afterState.dataTable[id]).toEqual({
370-
data,
375+
data: dataWithSelectedProp,
371376
pagination: {
372377
page: 1,
373378
rowsPerPage: 10,
@@ -397,7 +402,13 @@ describe('reducer', () => {
397402
data: actualData,
398403
sortingOption: actualSortingOption,
399404
} = afterState.dataTable[id];
400-
expect(actualData).toEqual([[7], [4], [3], [2], [1]]);
405+
expect(actualData).toEqual([
406+
{ id: 2, row: [7], selected: false },
407+
{ id: 0, row: [4], selected: false },
408+
{ id: 1, row: [3], selected: false },
409+
{ id: 4, row: [2], selected: false },
410+
{ id: 3, row: [1], selected: false },
411+
]);
401412
expect(actualSortingOption).toEqual(sortingOption);
402413
});
403414

@@ -419,22 +430,30 @@ describe('reducer', () => {
419430

420431
test('should setDataTableSortingOption', () => {
421432
const id = 'sample-data-table';
422-
const data = [{ id: 1 }];
423433
const sortingOption = {
424434
label: 'id',
425435
order: 'ASCENDING',
426436
};
427437
const beforeState = {};
428438
const afterState = reducer(
429439
beforeState,
430-
setDataTableSortingOption({ id, data, sortingOption })
440+
setDataTableSortingOption({ id, sortingOption })
431441
);
432442
expect(afterState.dataTable[id]).toEqual({
433-
data,
434443
sortingOption,
435444
});
436445
});
437446

447+
test('should setDataTableRows', () => {
448+
const id = 'sample-data-table';
449+
const data = [{ id: 0, row: [1], selected: false }];
450+
const beforeState = {};
451+
const afterState = reducer(beforeState, setDataTableRows({ id, data }));
452+
expect(afterState.dataTable[id]).toEqual({
453+
data,
454+
});
455+
});
456+
438457
test('should select data table from tabe id', () => {
439458
const dataTable = {
440459
data: [{ id: 1 }],

0 commit comments

Comments
 (0)