Skip to content

Commit 0257b88

Browse files
committed
Support multiple possible values in a collection filter
As per decaporg/decap-cms#7328
1 parent 809fea0 commit 0257b88

File tree

2 files changed

+23
-7
lines changed

2 files changed

+23
-7
lines changed

src/lib/services/contents/index.js

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -202,12 +202,28 @@ export const getEntriesByCollection = (collectionName) => {
202202
_i18n: { defaultLocale: locale },
203203
} = collection;
204204

205-
return get(allEntries).filter(
206-
(entry) =>
207-
getCollectionsByEntry(entry).some((_collection) => _collection.name === collectionName) &&
208-
(!filter ||
209-
getPropertyValue({ entry, locale, collectionName, key: filter.field }) === filter.value),
210-
);
205+
const filterField = filter?.field;
206+
207+
// eslint-disable-next-line no-nested-ternary
208+
const filterValues = filter?.value
209+
? Array.isArray(filter.value)
210+
? filter.value
211+
: [filter.value]
212+
: [];
213+
214+
return get(allEntries).filter((entry) => {
215+
if (!getCollectionsByEntry(entry).some(({ name }) => name === collectionName)) {
216+
return false;
217+
}
218+
219+
if (!filterField) {
220+
return true;
221+
}
222+
223+
return filterValues.includes(
224+
getPropertyValue({ entry, locale, collectionName, key: filterField }),
225+
);
226+
});
211227
};
212228

213229
/**

src/lib/typedefs.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,7 @@
367367
* @property {string} [public_folder] - Public media folder path for a folder/entry collection.
368368
* @property {object} [filter] - Filter for a folder/entry collection.
369369
* @property {string} filter.field - Field name.
370-
* @property {any} filter.value - Field value.
370+
* @property {any | any[]} filter.value - Field value. Multiple values can be defined with an array.
371371
* @property {object} [nested] - Nested collection config for a folder/entry collection.
372372
* @property {boolean} [hide] - Whether to hide the collection in the UI.
373373
* @property {boolean} [create] - Whether to allow creating items in a folder/entry collection.

0 commit comments

Comments
 (0)