diff --git a/packages/decap-cms-core/src/__tests__/backend.spec.js b/packages/decap-cms-core/src/__tests__/backend.spec.js index c673c19a01c4..0de73e4fd3ec 100644 --- a/packages/decap-cms-core/src/__tests__/backend.spec.js +++ b/packages/decap-cms-core/src/__tests__/backend.spec.js @@ -95,6 +95,33 @@ describe('Backend', () => { expect(result.length).toBe(1); }); + it('filters multiple values', () => { + const result = backend.filterEntries( + { + entries: [ + { + data: { + testField: 'one', + }, + }, + { + data: { + testField: 'two', + }, + }, + { + data: { + testField: 'three', + }, + }, + ], + }, + Map({ field: 'testField', value: ['one', 'two'] }), + ); + + expect(result.length).toBe(2); + }); + it('filters list values', () => { const result = backend.filterEntries( { diff --git a/packages/decap-cms-core/src/backend.ts b/packages/decap-cms-core/src/backend.ts index bcb8881de045..6ee62f5e191a 100644 --- a/packages/decap-cms-core/src/backend.ts +++ b/packages/decap-cms-core/src/backend.ts @@ -1351,12 +1351,39 @@ export class Backend { } filterEntries(collection: { entries: EntryValue[] }, filterRule: FilterRule) { + let filterValues = filterRule.get('value'); + + if (filterValues.toJS) { + filterValues = filterValues.toJS(); + } + + if (!Array.isArray(filterValues)) { + filterValues = [filterValues]; + } + + const fieldName = filterRule.get('field'); + return collection.entries.filter(entry => { - const fieldValue = entry.data[filterRule.get('field')]; + const fieldValue = entry.data[fieldName]; + + let fieldValues = []; + if (Array.isArray(fieldValue)) { - return fieldValue.includes(filterRule.get('value')); + fieldValues = fieldValue; + } else { + fieldValues = [fieldValue]; } - return fieldValue === filterRule.get('value'); + + let found = false; + + for (const filterValue of filterValues) { + if (fieldValues.includes(filterValue)) { + found = true; + break; + } + } + + return found; }); } } diff --git a/packages/decap-cms-core/src/types/redux.ts b/packages/decap-cms-core/src/types/redux.ts index b69a82311532..7848b1653677 100644 --- a/packages/decap-cms-core/src/types/redux.ts +++ b/packages/decap-cms-core/src/types/redux.ts @@ -565,7 +565,7 @@ export type EntryField = StaticallyTypedRecord<{ export type EntryFields = List; export type FilterRule = StaticallyTypedRecord<{ - value: string; + value: string | List; field: string; }>;