Skip to content

Commit

Permalink
Reset page after changing pattern filters or search value (#54774)
Browse files Browse the repository at this point in the history
  • Loading branch information
kevin940726 authored Sep 25, 2023
1 parent 4705a98 commit 2b06db0
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* WordPress dependencies
*/
import { useMemo, useEffect, useRef } from '@wordpress/element';
import { useMemo, useEffect, useRef, useState } from '@wordpress/element';
import { _n, sprintf } from '@wordpress/i18n';
import { useDebounce } from '@wordpress/compose';
import { __experimentalHeading as Heading } from '@wordpress/components';
Expand Down Expand Up @@ -114,6 +114,14 @@ function PatternList( { searchValue, selectedCategory, patternCategories } ) {
container
);

// Reset page when search value changes.
const [ previousSearchValue, setPreviousSearchValue ] =
useState( searchValue );
if ( searchValue !== previousSearchValue ) {
setPreviousSearchValue( searchValue );
pagingProps.changePage( 1 );
}

const hasItems = !! filteredBlockPatterns?.length;
return (
<div
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -279,11 +279,27 @@ export function BlockPatternsCategoryPanel( {
category,
scrollContainerRef
);
const { changePage } = pagingProps;

// Hide block pattern preview on unmount.
// eslint-disable-next-line react-hooks/exhaustive-deps
useEffect( () => () => onHover( null ), [] );

const onSetPatternSyncFilter = useCallback(
( value ) => {
setPatternSyncFilter( value );
changePage( 1 );
},
[ setPatternSyncFilter, changePage ]
);
const onSetPatternSourceFilter = useCallback(
( value ) => {
setPatternSourceFilter( value );
changePage( 1 );
},
[ setPatternSourceFilter, changePage ]
);

return (
<div className="block-editor-inserter__patterns-category-panel">
<VStack
Expand All @@ -299,8 +315,8 @@ export function BlockPatternsCategoryPanel( {
<BlockPatternsSyncFilter
patternSyncFilter={ patternSyncFilter }
patternSourceFilter={ patternSourceFilter }
setPatternSyncFilter={ setPatternSyncFilter }
setPatternSourceFilter={ setPatternSourceFilter }
setPatternSyncFilter={ onSetPatternSyncFilter }
setPatternSourceFilter={ onSetPatternSourceFilter }
scrollContainerRef={ scrollContainerRef }
category={ category }
/>
Expand Down

1 comment on commit 2b06db0

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Flaky tests detected in 2b06db0.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/6295954662
📝 Reported issues:

Please sign in to comment.