Skip to content

Commit e507ff3

Browse files
committed
feat: allow multiple filter values
1 parent 022dbe5 commit e507ff3

File tree

3 files changed

+58
-4
lines changed

3 files changed

+58
-4
lines changed

packages/decap-cms-core/src/__tests__/backend.spec.js

+27
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,33 @@ describe('Backend', () => {
9595
expect(result.length).toBe(1);
9696
});
9797

98+
it('filters multiple values', () => {
99+
const result = backend.filterEntries(
100+
{
101+
entries: [
102+
{
103+
data: {
104+
testField: 'one',
105+
},
106+
},
107+
{
108+
data: {
109+
testField: 'two',
110+
},
111+
},
112+
{
113+
data: {
114+
testField: 'three',
115+
},
116+
},
117+
],
118+
},
119+
Map({ field: 'testField', value: ['one', 'two'] }),
120+
);
121+
122+
expect(result.length).toBe(2);
123+
});
124+
98125
it('filters list values', () => {
99126
const result = backend.filterEntries(
100127
{

packages/decap-cms-core/src/backend.ts

+30-3
Original file line numberDiff line numberDiff line change
@@ -1351,12 +1351,39 @@ export class Backend {
13511351
}
13521352

13531353
filterEntries(collection: { entries: EntryValue[] }, filterRule: FilterRule) {
1354+
let filterValues = filterRule.get('value');
1355+
1356+
if (filterValues.toJS) {
1357+
filterValues = filterValues.toJS();
1358+
}
1359+
1360+
if (!Array.isArray(filterValues)) {
1361+
filterValues = [filterValues];
1362+
}
1363+
1364+
const fieldName = filterRule.get('field');
1365+
13541366
return collection.entries.filter(entry => {
1355-
const fieldValue = entry.data[filterRule.get('field')];
1367+
const fieldValue = entry.data[fieldName];
1368+
1369+
let fieldValues = [];
1370+
13561371
if (Array.isArray(fieldValue)) {
1357-
return fieldValue.includes(filterRule.get('value'));
1372+
fieldValues = fieldValue;
1373+
} else {
1374+
fieldValues = [fieldValue];
13581375
}
1359-
return fieldValue === filterRule.get('value');
1376+
1377+
let found = false;
1378+
1379+
for (const filterValue of filterValues) {
1380+
if (fieldValues.includes(filterValue)) {
1381+
found = true;
1382+
break;
1383+
}
1384+
}
1385+
1386+
return found;
13601387
});
13611388
}
13621389
}

packages/decap-cms-core/src/types/redux.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -565,7 +565,7 @@ export type EntryField = StaticallyTypedRecord<{
565565
export type EntryFields = List<EntryField>;
566566

567567
export type FilterRule = StaticallyTypedRecord<{
568-
value: string;
568+
value: string | List<string>;
569569
field: string;
570570
}>;
571571

0 commit comments

Comments
 (0)