Skip to content

Commit

Permalink
Merge a95a3f2 into eed48c3
Browse files Browse the repository at this point in the history
  • Loading branch information
erzhan-temir-mamyrov committed Dec 30, 2022
2 parents eed48c3 + a95a3f2 commit 0698b72
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 6 deletions.
6 changes: 4 additions & 2 deletions packages/react/src/components/Table/baseTableReducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,8 @@ export const baseTableReducer = (state = {}, action) => {
const isClearing = isMultiSelect && selectedIds.length === 0;

const allRowsId = [];
state.data.forEach((row) => fillArrWithRowIds(row, allRowsId));
const iterables = state.view.table.filteredData || state.data;
iterables.forEach((row) => fillArrWithRowIds(row, allRowsId));
const isSelectingAll = isMultiSelect && selectedIds.length === allRowsId.length;

return update(state, {
Expand All @@ -289,7 +290,8 @@ export const baseTableReducer = (state = {}, action) => {

const selectedIds = [];
if (isSelected) {
state.data.forEach((row) => fillArrWithRowIds(row, selectedIds));
const iterables = state.view.table.filteredData || state.data;
iterables.forEach((row) => fillArrWithRowIds(row, selectedIds));
}

return update(state, {
Expand Down
7 changes: 3 additions & 4 deletions packages/react/src/components/Table/tableReducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,7 @@ export const tableReducer = (state = {}, action) => {
action
);
case TABLE_SEARCH_APPLY: {
// console.log(state.data.length)
// Quick search should search within the filtered and sorted data
const data = filterSearchAndSort(
state.data,
Expand Down Expand Up @@ -506,12 +507,10 @@ export const tableReducer = (state = {}, action) => {
}

case TABLE_ROW_SELECT: {
const data = state.view.table.filteredData || state.data;
return baseTableReducer({ ...state, data }, action);
return baseTableReducer(state, action);
}
case TABLE_ROW_SELECT_ALL: {
const data = state.view.table.filteredData || state.data;
return baseTableReducer({ ...state, data }, action);
return baseTableReducer(state, action);
}
// By default we need to setup our sorted and filteredData and turn off the loading state
case TABLE_REGISTER: {
Expand Down
51 changes: 51 additions & 0 deletions packages/react/src/components/Table/tableReducer.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,34 @@ describe('table reducer', () => {
expect(tableWithSelectedRow.view.table.isSelectAllIndeterminate).toEqual(false);
});

it('TABLE_ROW_SELECT with filtered data', () => {
// Init with filtering
const filteredTable = tableReducer(initialState, tableRegister({ data: initialState.data }));
expect(filteredTable.view.table.filteredData).toHaveLength(14);
expect(filteredTable.data).toHaveLength(initialState.data.length);

// Select a row
const tableWithSelectedRow = tableReducer(filteredTable, tableRowSelect(['row-1'], 'multi'));
expect(tableWithSelectedRow.view.table.selectedIds).toEqual(['row-1']);
expect(tableWithSelectedRow.view.table.isSelectAllSelected).toEqual(false);
expect(tableWithSelectedRow.view.table.isSelectAllIndeterminate).toEqual(true);

// Unselect the row
const tableWithUnSelectedRow = tableReducer(
tableWithSelectedRow,
tableRowSelect([], 'multi')
);
expect(tableWithUnSelectedRow.view.table.selectedIds).toEqual([]);
expect(tableWithUnSelectedRow.view.table.isSelectAllSelected).toEqual(false);
expect(tableWithUnSelectedRow.view.table.isSelectAllIndeterminate).toEqual(false);

// Reset filter
const tableFilteredReset = tableReducer(tableWithUnSelectedRow, tableFilterApply({}));
expect(tableFilteredReset.view.filters).toEqual([]);
expect(tableFilteredReset.view.table.filteredData).toHaveLength(initialState.data.length);
expect(tableFilteredReset.data).toHaveLength(initialState.data.length);
});

it('TABLE_ROW_SELECT_ALL', () => {
expect(initialState.view.table.selectedIds).toEqual([]);
// Select all
Expand Down Expand Up @@ -557,6 +585,29 @@ describe('table reducer', () => {
expect(tableWithUnSelectedAll.view.table.isSelectAllSelected).toEqual(false);
});

it('TABLE_ROW_SELECT_ALL with filtered data', () => {
// Init with filtering
const filteredTable = tableReducer(initialState, tableRegister({ data: initialState.data }));
expect(filteredTable.view.table.filteredData).toHaveLength(14);
expect(filteredTable.data).toHaveLength(initialState.data.length);

// Select all
const tableWithSelectedAll = tableReducer(filteredTable, tableRowSelectAll(true));
expect(tableWithSelectedAll.view.table.filteredData).toHaveLength(14);
expect(tableWithSelectedAll.view.table.selectedIds).toHaveLength(14);

// Unselect all
const tableWithUnSelectedAll = tableReducer(tableWithSelectedAll, tableRowSelectAll(false));
expect(tableWithSelectedAll.view.table.filteredData).toHaveLength(14);
expect(tableWithUnSelectedAll.view.table.selectedIds.length).toEqual(0);

// Reset filter
const tableFilteredReset = tableReducer(tableWithUnSelectedAll, tableFilterApply({}));
expect(tableFilteredReset.view.filters).toEqual([]);
expect(tableFilteredReset.view.table.filteredData).toHaveLength(initialState.data.length);
expect(tableFilteredReset.data).toHaveLength(initialState.data.length);
});

it('TABLE_ROW_SELECT_ALL with nested rows should skip non selectable rows', () => {
const initialState = getInitialState();
initialState.data = getTableData().map((row, index) => addChildRows(row, index));
Expand Down

0 comments on commit 0698b72

Please sign in to comment.