Skip to content
Merged
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: fixed

Search Blocks: cap the filter-checkbox list at its configured "Maximum items" even after the session has retained options across multiple searches.
9 changes: 8 additions & 1 deletion projects/packages/search/src/search-blocks/store/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,9 @@ function dateBucketSlug( bucket ) {
* the visible label as a tiebreaker so two buckets with the same count don't
* swap positions across re-renders — ES bucket order is unstable on ties.
*
* Sliced to `maxItems` after sorting so retained options and URL-seeded
* selections can't push the list past the configured cap.
*
* @param {object} sharedState - Live store state.
* @param {string} filterKey - Filter key.
* @param {object} config - filterConfigs entry.
Expand Down Expand Up @@ -259,7 +262,11 @@ function checkboxFilterItems( sharedState, filterKey, config ) {
add( value, bucketLabel( value, valueLabels ), 0 );
}

return sortFilterItems( items, config, sharedState.locale );
// Slice after sort so retained options and URL-seeded selections can't
// push the list past `maxItems`; the sort already sinks unchecked count=0
// items to the bottom so retained-but-now-empty entries drop first.
const limit = Math.max( 1, config.maxItems ?? 10 );
return sortFilterItems( items, config, sharedState.locale ).slice( 0, limit );
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,62 @@ describe( 'filter-checkbox view store — filterItems', () => {
] );
} );

it( 'caps the rendered list at maxItems when retained options push it past the limit', () => {
// Repro of SEARCH-208: the ES request is bounded at `size: maxItems`,
// but retained options accumulated from earlier searches are appended
// on the client. Once the merged set exceeds the cap the list must
// still slice to `maxItems`. Sort by count, so the live (non-zero)
// buckets win their slots and the retained-but-now-empty entries
// drop off the bottom first.
contextRef.current = { filterKey: 'category' };
captured.state.aggregations = {
category: {
buckets: [
{ key: 'news/News', doc_count: 9 },
{ key: 'reviews/Reviews', doc_count: 4 },
],
},
};
captured.state.retainedFilterOptions = {
category: [
{ value: 'news', label: 'News' },
{ value: 'reviews', label: 'Reviews' },
{ value: 'archive', label: 'Archive' },
{ value: 'opinion', label: 'Opinion' },
{ value: 'sports', label: 'Sports' },
],
};
captured.state.filterConfigs = {
category: { showCount: true, bucketSortOrder: 'count', valueLabels: {}, maxItems: 3 },
};
const items = captured.state.filterItems;
expect( items.map( i => i.value ) ).toEqual( [ 'news', 'reviews', 'archive' ] );
} );

it( 'falls back to a maxItems default of 10 when the config omits it', () => {
contextRef.current = { filterKey: 'category' };
const buckets = Array.from( { length: 12 }, ( _, i ) => ( {
key: `cat-${ i }/Cat ${ i }`,
doc_count: 12 - i,
} ) );
captured.state.aggregations = { category: { buckets } };
captured.state.filterConfigs = {
category: { showCount: true, bucketSortOrder: 'count', valueLabels: {} },
};
expect( captured.state.filterItems ).toHaveLength( 10 );
} );

it( 'coerces a maxItems below 1 up to 1 so the slice never empties the list', () => {
contextRef.current = { filterKey: 'category' };
captured.state.aggregations = {
category: { buckets: [ { key: 'news/News', doc_count: 9 } ] },
};
captured.state.filterConfigs = {
category: { showCount: true, bucketSortOrder: 'count', valueLabels: {}, maxItems: 0 },
};
expect( captured.state.filterItems.map( i => i.value ) ).toEqual( [ 'news' ] );
} );

it( 'renders selected values that are not in any aggregation yet (URL-seeded deep link)', () => {
contextRef.current = { filterKey: 'post_types' };
captured.state.activeFilters = { post_types: [ 'post' ] };
Expand Down
Loading