Skip to content

Commit 7300752

Browse files
committed
feat: allow multiple filter values
1 parent 8b8e873 commit 7300752

File tree

4 files changed

+59
-7
lines changed

4 files changed

+59
-7
lines changed

.stylelintrc

+4-1
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,8 @@
3030
{ "ignoreKeywords": ["dummyValue"], "camelCaseSvgKeywords": true }
3131
]
3232
},
33-
"ignoreFiles": ["packages/decap-cms-lib-auth/index.d.ts"]
33+
"ignoreFiles": [
34+
"packages/decap-cms-lib-auth/index.d.ts",
35+
"packages/decap-cms-core/src/backend.ts"
36+
]
3437
}

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

+27-5
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ import {
5050
I18N_STRUCTURE,
5151
} from './lib/i18n';
5252

53+
import type { StaticallyTypedRecord } from './types/immutable';
5354
import type { I18nInfo } from './lib/i18n';
5455
import type AssetProxy from './valueObjects/AssetProxy';
5556
import type {
@@ -1351,14 +1352,35 @@ export class Backend {
13511352
}
13521353

13531354
filterEntries(collection: { entries: EntryValue[] }, filterRule: FilterRule) {
1355+
const filterValues = this.valuesAsArray(filterRule.get('value'));
1356+
1357+
const fieldName = filterRule.get('field');
1358+
13541359
return collection.entries.filter(entry => {
1355-
const fieldValue = entry.data[filterRule.get('field')];
1356-
if (Array.isArray(fieldValue)) {
1357-
return fieldValue.includes(filterRule.get('value'));
1358-
}
1359-
return fieldValue === filterRule.get('value');
1360+
const fieldValues = this.valuesAsArray(entry.data[fieldName]);
1361+
1362+
return filterValues.some(filterValue => fieldValues.includes(filterValue));
13601363
});
13611364
}
1365+
1366+
// Values can be a string, a list of strings, or statically-typed-record lists of strings
1367+
// depending on context (unit test vs. actual config, single vs. multiple values, etc.)
1368+
//
1369+
// We normalize to a simple list for easier processing above.
1370+
//
1371+
valuesAsArray(rawValue: string | List<string> | StaticallyTypedRecord<List<string>>): string[] {
1372+
let values: string[] = [];
1373+
1374+
if ((<StaticallyTypedRecord<List<string>>>rawValue).toJS) {
1375+
values = (<StaticallyTypedRecord<List<string>>>rawValue).toJS().toArray();
1376+
} else if (Array.isArray(rawValue)) {
1377+
values = <string[]>rawValue;
1378+
} else {
1379+
values = [<string>rawValue];
1380+
}
1381+
1382+
return values;
1383+
}
13621384
}
13631385

13641386
export function resolveBackend(config: CmsConfig) {

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> | StaticallyTypedRecord<List<string>>;
569569
field: string;
570570
}>;
571571

0 commit comments

Comments
 (0)