Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow multiple filter values, fixes #7328 #7329

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .stylelintrc
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,8 @@
{ "ignoreKeywords": ["dummyValue"], "camelCaseSvgKeywords": true }
]
},
"ignoreFiles": ["packages/decap-cms-lib-auth/index.d.ts"]
"ignoreFiles": [
"packages/decap-cms-lib-auth/index.d.ts",
"packages/decap-cms-core/src/backend.ts"
]
}
27 changes: 27 additions & 0 deletions packages/decap-cms-core/src/__tests__/backend.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
{
Expand Down
32 changes: 27 additions & 5 deletions packages/decap-cms-core/src/backend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ import {
I18N_STRUCTURE,
} from './lib/i18n';

import type { StaticallyTypedRecord } from './types/immutable';
import type { I18nInfo } from './lib/i18n';
import type AssetProxy from './valueObjects/AssetProxy';
import type {
Expand Down Expand Up @@ -1351,14 +1352,35 @@ export class Backend {
}

filterEntries(collection: { entries: EntryValue[] }, filterRule: FilterRule) {
const filterValues = this.valuesAsArray(filterRule.get('value'));

const fieldName = filterRule.get('field');

return collection.entries.filter(entry => {
const fieldValue = entry.data[filterRule.get('field')];
if (Array.isArray(fieldValue)) {
return fieldValue.includes(filterRule.get('value'));
}
return fieldValue === filterRule.get('value');
const fieldValues = this.valuesAsArray(entry.data[fieldName]);

return filterValues.some(filterValue => fieldValues.includes(filterValue));
});
}

// Values can be a string, a list of strings, or statically-typed-record lists of strings
// depending on context (unit test vs. actual config, single vs. multiple values, etc.)
//
// We normalize to a simple list for easier processing above.
//
valuesAsArray(rawValue: string | List<string> | StaticallyTypedRecord<List<string>>): string[] {
let values: string[] = [];

if ((<StaticallyTypedRecord<List<string>>>rawValue).toJS) {
values = (<StaticallyTypedRecord<List<string>>>rawValue).toJS().toArray();
} else if (Array.isArray(rawValue)) {
values = <string[]>rawValue;
} else {
values = [<string>rawValue];
}

return values;
}
}

export function resolveBackend(config: CmsConfig) {
Expand Down
2 changes: 1 addition & 1 deletion packages/decap-cms-core/src/types/redux.ts
Original file line number Diff line number Diff line change
Expand Up @@ -565,7 +565,7 @@ export type EntryField = StaticallyTypedRecord<{
export type EntryFields = List<EntryField>;

export type FilterRule = StaticallyTypedRecord<{
value: string;
value: string | List<string> | StaticallyTypedRecord<List<string>>;
field: string;
}>;

Expand Down
Loading