File tree 3 files changed +58
-4
lines changed
packages/decap-cms-core/src
3 files changed +58
-4
lines changed Original file line number Diff line number Diff line change @@ -95,6 +95,33 @@ describe('Backend', () => {
95
95
expect ( result . length ) . toBe ( 1 ) ;
96
96
} ) ;
97
97
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
+
98
125
it ( 'filters list values' , ( ) => {
99
126
const result = backend . filterEntries (
100
127
{
Original file line number Diff line number Diff line change @@ -1351,12 +1351,39 @@ export class Backend {
1351
1351
}
1352
1352
1353
1353
filterEntries ( collection : { entries : EntryValue [ ] } , filterRule : FilterRule ) {
1354
+ let filterValues = filterRule . get ( 'value' ) ;
1355
+
1356
+ if ( filterValues . toJS ) {
1357
+ filterValues = filterValues . toJS ( ) ;
1358
+ }
1359
+
1360
+ if ( ! Array . isArray ( filterValues ) ) {
1361
+ filterValues = [ filterValues ] ;
1362
+ }
1363
+
1364
+ const fieldName = filterRule . get ( 'field' ) ;
1365
+
1354
1366
return collection . entries . filter ( entry => {
1355
- const fieldValue = entry . data [ filterRule . get ( 'field' ) ] ;
1367
+ const fieldValue = entry . data [ fieldName ] ;
1368
+
1369
+ let fieldValues = [ ] ;
1370
+
1356
1371
if ( Array . isArray ( fieldValue ) ) {
1357
- return fieldValue . includes ( filterRule . get ( 'value' ) ) ;
1372
+ fieldValues = fieldValue ;
1373
+ } else {
1374
+ fieldValues = [ fieldValue ] ;
1358
1375
}
1359
- return fieldValue === filterRule . get ( 'value' ) ;
1376
+
1377
+ let found = false ;
1378
+
1379
+ for ( const filterValue of filterValues ) {
1380
+ if ( fieldValues . includes ( filterValue ) ) {
1381
+ found = true ;
1382
+ break ;
1383
+ }
1384
+ }
1385
+
1386
+ return found ;
1360
1387
} ) ;
1361
1388
}
1362
1389
}
Original file line number Diff line number Diff line change @@ -565,7 +565,7 @@ export type EntryField = StaticallyTypedRecord<{
565
565
export type EntryFields = List < EntryField > ;
566
566
567
567
export type FilterRule = StaticallyTypedRecord < {
568
- value : string ;
568
+ value : string | List < string > ;
569
569
field : string ;
570
570
} > ;
571
571
You can’t perform that action at this time.
0 commit comments