From 0257b88f070020269512695a7a455d17a8ad9033 Mon Sep 17 00:00:00 2001 From: Kohei Yoshino <kohei.yoshino@gmail.com> Date: Fri, 15 Nov 2024 15:39:46 -0500 Subject: [PATCH] Support multiple possible values in a collection filter As per https://github.com/decaporg/decap-cms/issues/7328 --- src/lib/services/contents/index.js | 28 ++++++++++++++++++++++------ src/lib/typedefs.js | 2 +- 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/src/lib/services/contents/index.js b/src/lib/services/contents/index.js index 4cd6235e..1069bb70 100644 --- a/src/lib/services/contents/index.js +++ b/src/lib/services/contents/index.js @@ -202,12 +202,28 @@ export const getEntriesByCollection = (collectionName) => { _i18n: { defaultLocale: locale }, } = collection; - return get(allEntries).filter( - (entry) => - getCollectionsByEntry(entry).some((_collection) => _collection.name === collectionName) && - (!filter || - getPropertyValue({ entry, locale, collectionName, key: filter.field }) === filter.value), - ); + const filterField = filter?.field; + + // eslint-disable-next-line no-nested-ternary + const filterValues = filter?.value + ? Array.isArray(filter.value) + ? filter.value + : [filter.value] + : []; + + return get(allEntries).filter((entry) => { + if (!getCollectionsByEntry(entry).some(({ name }) => name === collectionName)) { + return false; + } + + if (!filterField) { + return true; + } + + return filterValues.includes( + getPropertyValue({ entry, locale, collectionName, key: filterField }), + ); + }); }; /** diff --git a/src/lib/typedefs.js b/src/lib/typedefs.js index b10ab788..8d95d035 100644 --- a/src/lib/typedefs.js +++ b/src/lib/typedefs.js @@ -367,7 +367,7 @@ * @property {string} [public_folder] - Public media folder path for a folder/entry collection. * @property {object} [filter] - Filter for a folder/entry collection. * @property {string} filter.field - Field name. - * @property {any} filter.value - Field value. + * @property {any | any[]} filter.value - Field value. Multiple values can be defined with an array. * @property {object} [nested] - Nested collection config for a folder/entry collection. * @property {boolean} [hide] - Whether to hide the collection in the UI. * @property {boolean} [create] - Whether to allow creating items in a folder/entry collection.