|
1 | 1 | /** |
2 | 2 | * WordPress dependencies |
3 | 3 | */ |
4 | | -import { useBlockProps } from '@wordpress/block-editor'; |
| 4 | +import { __ } from '@wordpress/i18n'; |
| 5 | +import { InspectorControls, useBlockProps } from '@wordpress/block-editor'; |
| 6 | +import { PanelBody, RangeControl } from '@wordpress/components'; |
5 | 7 |
|
6 | 8 | const createPaginationItem = ( content, Tag = 'a', extraClass = '' ) => ( |
7 | | - <Tag className={ `page-numbers ${ extraClass }` }>{ content }</Tag> |
| 9 | + <Tag key={ content } className={ `page-numbers ${ extraClass }` }> |
| 10 | + { content } |
| 11 | + </Tag> |
8 | 12 | ); |
9 | 13 |
|
10 | | -const previewPaginationNumbers = () => ( |
11 | | - <> |
12 | | - { createPaginationItem( 1 ) } |
13 | | - { createPaginationItem( 2 ) } |
14 | | - { createPaginationItem( 3, 'span', 'current' ) } |
15 | | - { createPaginationItem( 4 ) } |
16 | | - { createPaginationItem( 5 ) } |
17 | | - { createPaginationItem( '...', 'span', 'dots' ) } |
18 | | - { createPaginationItem( 8 ) } |
19 | | - </> |
20 | | -); |
| 14 | +const previewPaginationNumbers = ( midSize ) => { |
| 15 | + const paginationItems = []; |
| 16 | + |
| 17 | + // First set of pagination items. |
| 18 | + for ( let i = 1; i <= midSize; i++ ) { |
| 19 | + paginationItems.push( createPaginationItem( i ) ); |
| 20 | + } |
| 21 | + |
| 22 | + // Current pagination item. |
| 23 | + paginationItems.push( |
| 24 | + createPaginationItem( midSize + 1, 'span', 'current' ) |
| 25 | + ); |
| 26 | + |
| 27 | + // Second set of pagination items. |
| 28 | + for ( let i = 1; i <= midSize; i++ ) { |
| 29 | + paginationItems.push( createPaginationItem( midSize + 1 + i ) ); |
| 30 | + } |
| 31 | + |
| 32 | + // Dots. |
| 33 | + paginationItems.push( createPaginationItem( '...', 'span', 'dots' ) ); |
| 34 | + |
| 35 | + // Last pagination item. |
| 36 | + paginationItems.push( createPaginationItem( midSize * 2 + 3 ) ); |
| 37 | + |
| 38 | + return <>{ paginationItems }</>; |
| 39 | +}; |
21 | 40 |
|
22 | | -export default function QueryPaginationNumbersEdit() { |
23 | | - const paginationNumbers = previewPaginationNumbers(); |
24 | | - return <div { ...useBlockProps() }>{ paginationNumbers }</div>; |
| 41 | +export default function QueryPaginationNumbersEdit( { |
| 42 | + attributes, |
| 43 | + setAttributes, |
| 44 | +} ) { |
| 45 | + const { midSize } = attributes; |
| 46 | + const paginationNumbers = previewPaginationNumbers( |
| 47 | + parseInt( midSize, 10 ) |
| 48 | + ); |
| 49 | + return ( |
| 50 | + <> |
| 51 | + <InspectorControls> |
| 52 | + <PanelBody title={ __( 'Settings' ) }> |
| 53 | + <RangeControl |
| 54 | + label={ __( 'Number of links' ) } |
| 55 | + help={ __( |
| 56 | + 'Specify how many links can appear before and after the current page number. Links to the first, current and last page are always visible.' |
| 57 | + ) } |
| 58 | + value={ midSize } |
| 59 | + onChange={ ( value ) => { |
| 60 | + setAttributes( { |
| 61 | + midSize: parseInt( value, 10 ), |
| 62 | + } ); |
| 63 | + } } |
| 64 | + min={ 0 } |
| 65 | + max={ 5 } |
| 66 | + withInputField={ false } |
| 67 | + /> |
| 68 | + </PanelBody> |
| 69 | + </InspectorControls> |
| 70 | + <div { ...useBlockProps() }>{ paginationNumbers }</div> |
| 71 | + </> |
| 72 | + ); |
25 | 73 | } |
0 commit comments