-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make mid size parameter settable for Query Pagination block. (#51216)
* Add attribute & context „midSize“ to pagination block. * Add and use QueryPaginationMidSizeControl in PaginationBlock. * React to new „midSize“ context in pagination numbers block. * Adjust unit tests. * Fix php errors from partial line commits. * Remove unneeded React import. * Move midSize control to query pagination numbers * Fix incorrect partial php commit * Get midSize param from attributes not context * Re-add missing array definition line * Update help description. * Remove unnecessary formatting changes * Fix formatting issue. * Move mid size control from own file into edit.js * Only pass mid_size if set; also for inherit query * Fix space alignment * Increase last pagination number to justify … gap * Rebuild test
- Loading branch information
Showing
6 changed files
with
85 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 64 additions & 16 deletions
80
packages/block-library/src/query-pagination-numbers/edit.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,73 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useBlockProps } from '@wordpress/block-editor'; | ||
import { __ } from '@wordpress/i18n'; | ||
import { InspectorControls, useBlockProps } from '@wordpress/block-editor'; | ||
import { PanelBody, RangeControl } from '@wordpress/components'; | ||
|
||
const createPaginationItem = ( content, Tag = 'a', extraClass = '' ) => ( | ||
<Tag className={ `page-numbers ${ extraClass }` }>{ content }</Tag> | ||
<Tag key={ content } className={ `page-numbers ${ extraClass }` }> | ||
{ content } | ||
</Tag> | ||
); | ||
|
||
const previewPaginationNumbers = () => ( | ||
<> | ||
{ createPaginationItem( 1 ) } | ||
{ createPaginationItem( 2 ) } | ||
{ createPaginationItem( 3, 'span', 'current' ) } | ||
{ createPaginationItem( 4 ) } | ||
{ createPaginationItem( 5 ) } | ||
{ createPaginationItem( '...', 'span', 'dots' ) } | ||
{ createPaginationItem( 8 ) } | ||
</> | ||
); | ||
const previewPaginationNumbers = ( midSize ) => { | ||
const paginationItems = []; | ||
|
||
// First set of pagination items. | ||
for ( let i = 1; i <= midSize; i++ ) { | ||
paginationItems.push( createPaginationItem( i ) ); | ||
} | ||
|
||
// Current pagination item. | ||
paginationItems.push( | ||
createPaginationItem( midSize + 1, 'span', 'current' ) | ||
); | ||
|
||
// Second set of pagination items. | ||
for ( let i = 1; i <= midSize; i++ ) { | ||
paginationItems.push( createPaginationItem( midSize + 1 + i ) ); | ||
} | ||
|
||
// Dots. | ||
paginationItems.push( createPaginationItem( '...', 'span', 'dots' ) ); | ||
|
||
// Last pagination item. | ||
paginationItems.push( createPaginationItem( midSize * 2 + 3 ) ); | ||
|
||
return <>{ paginationItems }</>; | ||
}; | ||
|
||
export default function QueryPaginationNumbersEdit() { | ||
const paginationNumbers = previewPaginationNumbers(); | ||
return <div { ...useBlockProps() }>{ paginationNumbers }</div>; | ||
export default function QueryPaginationNumbersEdit( { | ||
attributes, | ||
setAttributes, | ||
} ) { | ||
const { midSize } = attributes; | ||
const paginationNumbers = previewPaginationNumbers( | ||
parseInt( midSize, 10 ) | ||
); | ||
return ( | ||
<> | ||
<InspectorControls> | ||
<PanelBody title={ __( 'Settings' ) }> | ||
<RangeControl | ||
label={ __( 'Number of links' ) } | ||
help={ __( | ||
'Specify how many links can appear before and after the current page number. Links to the first, current and last page are always visible.' | ||
) } | ||
value={ midSize } | ||
onChange={ ( value ) => { | ||
setAttributes( { | ||
midSize: parseInt( value, 10 ), | ||
} ); | ||
} } | ||
min={ 0 } | ||
max={ 5 } | ||
withInputField={ false } | ||
/> | ||
</PanelBody> | ||
</InspectorControls> | ||
<div { ...useBlockProps() }>{ paginationNumbers }</div> | ||
</> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters