Skip to content

Commit ce9db57

Browse files
authored
fix: table search filter (#683)
1 parent 1ee80b6 commit ce9db57

File tree

4 files changed

+165
-6
lines changed

4 files changed

+165
-6
lines changed

src/files-and-videos/generic/table-components/sort-and-filter-modal/utils.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { isEmpty } from 'lodash';
1+
import { isArray, isEmpty } from 'lodash';
22

33
export const getFilterOptions = (columns) => {
44
const allOptions = [];
@@ -37,7 +37,6 @@ export const getFilterOptions = (columns) => {
3737
export const getCheckedFilters = (state) => {
3838
const { filters } = state;
3939
const allFilters = [];
40-
4140
filters.forEach(filter => {
4241
const { id, value } = filter;
4342
let updatedValues = value;
@@ -56,7 +55,11 @@ export const getCheckedFilters = (state) => {
5655
break;
5756
}
5857

59-
allFilters.push(...updatedValues);
58+
if (isArray(updatedValues)) {
59+
allFilters.push(...updatedValues);
60+
} else {
61+
allFilters.push([id, updatedValues]);
62+
}
6063
});
6164

6265
return allFilters;
@@ -66,6 +69,12 @@ export const processFilters = (filters, columns, setAllFilters) => {
6669
const filterableColumns = columns.filter(column => column?.filterChoices);
6770
const allFilters = [];
6871

72+
const [displayNameFilter] = filters.filter(filter => isArray(filter));
73+
if (displayNameFilter) {
74+
const [id, filterValue] = displayNameFilter;
75+
allFilters.push({ id, value: [filterValue] });
76+
}
77+
6978
filterableColumns.forEach(({ id, filterChoices }) => {
7079
const filterValues = filterChoices.map(choice => choice.value);
7180
let processedFilters = filters;

src/files-and-videos/generic/table-components/sort-and-filter-modal/utils.test.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,18 @@ describe('getCheckboxFilters', () => {
9292
expect(actual).toEqual(expected);
9393
});
9494
});
95+
96+
describe('filter with serach bar', () => {
97+
it('should equal array in array with displayName and value', () => {
98+
const state = {
99+
filters: [{ id: 'displayName', value: 'filter' }],
100+
};
101+
const expected = [['displayName', 'filter']];
102+
const actual = getCheckedFilters(state);
103+
104+
expect(actual).toEqual(expected);
105+
});
106+
});
95107
});
96108

97109
describe('getFilterOptions', () => {
@@ -301,4 +313,17 @@ describe('processFilters', () => {
301313
expect(setAllFilters).toHaveBeenCalledWith(expectedParameter);
302314
});
303315
});
316+
317+
describe('filter with serach bar', () => {
318+
it('should call setAllFitlers with displayName filter', () => {
319+
const filters = [['displayName', 'search']];
320+
const columns = [
321+
{ id: 'test', filterChoices: [{ name: 'Filter', value: 'filter' }] },
322+
];
323+
const expectedParameter = [{ id: 'displayName', value: ['search'] }];
324+
processFilters(filters, columns, setAllFilters);
325+
326+
expect(setAllFilters).toHaveBeenCalledWith(expectedParameter);
327+
});
328+
});
304329
});

src/files-and-videos/generic/table-components/utils.js

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { isEmpty } from 'lodash';
1+
import { isEmpty, isArray } from 'lodash';
22
import messages from '../messages';
33

44
const getFilterDisplayName = (column, values) => {
@@ -15,19 +15,35 @@ export const getFilters = (state, columns) => {
1515
const { filters } = state;
1616
const filterableColumns = columns.filter(column => column?.filterChoices);
1717
const allFilters = [];
18+
1819
filters.forEach(filter => {
1920
const { id, value } = filter;
2021
const [filterColumn] = filterableColumns.filter(column => column.id === id);
21-
const currentFilters = getFilterDisplayName(filterColumn, value);
22+
let currentFilters;
23+
24+
if (filterColumn) {
25+
currentFilters = getFilterDisplayName(filterColumn, value);
26+
} else {
27+
const [serachValue] = value;
28+
currentFilters = [{ name: serachValue, value: serachValue }];
29+
}
2230
allFilters.push(...currentFilters);
2331
});
32+
2433
return allFilters;
2534
};
2635

2736
export const removeFilter = (filter, setFilter, setAllFilters, state) => {
2837
const { filters } = state;
2938
const [editedFilter] = filters.filter(currentFilter => currentFilter.value.includes(filter));
30-
const updatedFilterValue = editedFilter.value.filter(value => value !== filter);
39+
40+
let updatedFilterValue;
41+
if (isArray(editedFilter?.value)) {
42+
updatedFilterValue = editedFilter.value.filter(value => value !== filter);
43+
} else {
44+
updatedFilterValue = filter.includes(editedFilter?.value) ? [] : editedFilter.value;
45+
}
46+
3147
if (isEmpty(updatedFilterValue)) {
3248
const updatedFilters = filters.filter(currentFilter => currentFilter.id !== editedFilter.id);
3349
setAllFilters(updatedFilters);
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
import { getCurrentViewRange, getFilters, removeFilter } from './utils';
2+
3+
describe('getCurrentViewRange', () => {
4+
const intl = {
5+
formatMessage: (name, { fileCount, rowCount }) => (
6+
`Showing ${fileCount} of ${rowCount}`
7+
),
8+
};
9+
10+
it('should return with intials row count', () => {
11+
const data = {
12+
filterRowCount: 25,
13+
initialRowCount: 25,
14+
fileCount: 12,
15+
intl,
16+
};
17+
const expected = 'Showing 12 of 25';
18+
const actual = getCurrentViewRange(data);
19+
20+
expect(actual).toEqual(expected);
21+
});
22+
23+
it('should return with filter row count', () => {
24+
const data = {
25+
filterRowCount: 12,
26+
initialRowCount: 25,
27+
fileCount: 12,
28+
intl,
29+
};
30+
const expected = 'Showing 12 of 12';
31+
const actual = getCurrentViewRange(data);
32+
33+
expect(actual).toEqual(expected);
34+
});
35+
});
36+
37+
describe('getFilters', () => {
38+
it('should return filter when columns is empty', () => {
39+
const state = { filters: [{ id: 'test', value: ['unknown'] }] };
40+
const columns = [];
41+
const expected = [{ name: 'unknown', value: 'unknown' }];
42+
const actual = getFilters(state, columns);
43+
44+
expect(actual).toEqual(expected);
45+
});
46+
47+
it('should return filtern for specific column', () => {
48+
const state = { filters: [{ id: 'validColumn', value: ['filter1'] }] };
49+
const columns = [{
50+
id: 'validColumn',
51+
filterChoices: [
52+
{ name: 'Filter 1', value: 'filter1' },
53+
{ name: 'Filter 2', value: 'filter2' },
54+
],
55+
}];
56+
const expected = [{ name: 'Filter 1', value: 'filter1' }];
57+
const acutal = getFilters(state, columns);
58+
59+
expect(acutal).toEqual(expected);
60+
});
61+
});
62+
63+
describe('removeFilter', () => {
64+
const setAllFilters = jest.fn();
65+
const setFilter = jest.fn();
66+
67+
beforeEach(() => {
68+
jest.resetAllMocks();
69+
});
70+
71+
describe('state filter.value is an array', () => {
72+
it('should call setAllFilters', () => {
73+
const state = {
74+
filters: [
75+
{ id: 'test', value: ['filter1'] },
76+
],
77+
};
78+
const filter = 'filter1';
79+
removeFilter(filter, setFilter, setAllFilters, state);
80+
81+
expect(setAllFilters).toHaveBeenCalled();
82+
});
83+
84+
it('should call setFilter', () => {
85+
const state = {
86+
filters: [
87+
{ id: 'test', value: ['filter1', 'filter2'] },
88+
],
89+
};
90+
const filter = 'filter1';
91+
removeFilter(filter, setFilter, setAllFilters, state);
92+
93+
expect(setFilter).toHaveBeenCalled();
94+
});
95+
});
96+
describe('state filter.value is not an array', () => {
97+
it('should call setAllFilters', () => {
98+
const state = {
99+
filters: [
100+
{ id: 'test', value: 'filter1' },
101+
],
102+
};
103+
const filter = 'filter1';
104+
removeFilter(filter, setFilter, setAllFilters, state);
105+
106+
expect(setAllFilters).toHaveBeenCalled();
107+
});
108+
});
109+
});

0 commit comments

Comments
 (0)