Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Patterns: backport fix for bug that causes search and filtering to only work on first page of results #53091

Merged
merged 1 commit into from
Jul 28, 2023
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
11 changes: 8 additions & 3 deletions packages/edit-site/src/components/page-patterns/grid.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
__experimentalText as Text,
Button,
} from '@wordpress/components';
import { useRef, useState, useMemo } from '@wordpress/element';
import { useRef, useMemo } from '@wordpress/element';
import { __, _x, _n, sprintf } from '@wordpress/i18n';
import { useAsyncList } from '@wordpress/compose';

Expand Down Expand Up @@ -82,8 +82,13 @@ function Pagination( { currentPage, numPages, changePage, totalItems } ) {
);
}

export default function Grid( { categoryId, items, ...props } ) {
const [ currentPage, setCurrentPage ] = useState( 1 );
export default function Grid( {
categoryId,
items,
currentPage,
setCurrentPage,
...props
} ) {
const gridRef = useRef();
const totalItems = items.length;
const pageIndex = currentPage - 1;
Expand Down
17 changes: 15 additions & 2 deletions packages/edit-site/src/components/page-patterns/patterns-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ const SYNC_DESCRIPTIONS = {
};

export default function PatternsList( { categoryId, type } ) {
const [ currentPage, setCurrentPage ] = useState( 1 );
const location = useLocation();
const history = useHistory();
const isMobileViewport = useViewportMatch( 'medium', '<' );
Expand All @@ -73,6 +74,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 Down Expand Up @@ -109,7 +120,7 @@ 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 @@ -123,7 +134,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 Expand Up @@ -157,6 +168,8 @@ export default function PatternsList( { categoryId, type } ) {
items={ patterns }
aria-labelledby={ titleId }
aria-describedby={ descriptionId }
currentPage={ currentPage }
setCurrentPage={ setCurrentPage }
/>
) }
{ ! isResolving && ! hasPatterns && <NoPatterns /> }
Expand Down
Loading