Skip to content

Commit

Permalink
Patterns: Reset current page when search filters change (#52933)
Browse files Browse the repository at this point in the history
Co-authored-by: Glen Davies <glen.davies@automattic.com>
  • Loading branch information
aaronrobertshaw and glendaviesnz committed Jul 26, 2023
1 parent 6cf6643 commit 6ba753e
Showing 1 changed file with 20 additions and 10 deletions.
30 changes: 20 additions & 10 deletions packages/edit-site/src/components/page-patterns/patterns-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,16 @@ export default function PatternsList( { categoryId, type } ) {
}
);

const updateSearchFilter = ( value ) => {
setCurrentPage( 1 );
setFilterValue( value );
};

const updateSyncFilter = ( value ) => {
setCurrentPage( 1 );
setSyncFilter( value );
};

const id = useId();
const titleId = `${ id }-title`;
const descriptionId = `${ id }-description`;
Expand All @@ -90,14 +100,12 @@ export default function PatternsList( { categoryId, type } ) {
const pageIndex = currentPage - 1;
const numPages = Math.ceil( patterns.length / PAGE_SIZE );

const list = useMemo(
() =>
patterns.slice(
pageIndex * PAGE_SIZE,
pageIndex * PAGE_SIZE + PAGE_SIZE
),
[ pageIndex, patterns ]
);
const list = useMemo( () => {
return patterns.slice(
pageIndex * PAGE_SIZE,
pageIndex * PAGE_SIZE + PAGE_SIZE
);
}, [ pageIndex, patterns ] );

const asyncList = useAsyncList( list, { step: 10 } );

Expand Down Expand Up @@ -138,7 +146,9 @@ export default function PatternsList( { categoryId, type } ) {
<FlexBlock className="edit-site-patterns__search-block">
<SearchControl
className="edit-site-patterns__search"
onChange={ ( value ) => setFilterValue( value ) }
onChange={ ( value ) =>
updateSearchFilter( value )
}
placeholder={ __( 'Search patterns' ) }
label={ __( 'Search patterns' ) }
value={ filterValue }
Expand All @@ -152,7 +162,7 @@ export default function PatternsList( { categoryId, type } ) {
label={ __( 'Filter by sync status' ) }
value={ syncFilter }
isBlock
onChange={ ( value ) => setSyncFilter( value ) }
onChange={ ( value ) => updateSyncFilter( value ) }
__nextHasNoMarginBottom
>
{ Object.entries( SYNC_FILTERS ).map(
Expand Down

1 comment on commit 6ba753e

@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 6ba753e.
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/5663150407
📝 Reported issues:

Please sign in to comment.