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

Page List Block: Adds a longdash tree to the parent selector #46336

Merged
merged 2 commits into from
Dec 8, 2022
Merged
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
42 changes: 29 additions & 13 deletions packages/block-library/src/page-list/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,30 @@ export default function PageListEdit( {
}, [] );
};

const makePagesTree = ( parentId = 0, level = 0 ) => {
const childPages = pagesByParentId.get( parentId );

if ( ! childPages?.length ) {
return [];
}

return childPages.reduce( ( tree, page ) => {
const hasChildren = pagesByParentId.has( page.id );
const item = {
value: page.id,
label: '— '.repeat( level ) + page.title.rendered,
rawName: page.title.rendered,
};
tree.push( item );
if ( hasChildren ) {
tree.push( ...makePagesTree( page.id, level + 1 ) );
}
return tree;
}, [] );
};

const pagesTree = useMemo( makePagesTree, [ pagesByParentId ] );

const blockList = useMemo( getBlockList, [
pagesByParentId,
parentPageID,
Expand Down Expand Up @@ -161,14 +185,6 @@ export default function PageListEdit( {
}
};

const parentOptions = pages?.reduce( ( accumulator, page ) => {
accumulator.push( {
value: page.id,
label: page.title.rendered,
} );
return accumulator;
}, [] );

useEffect( () => {
__unstableMarkNextChangeAsNotPersistent();
if ( blockList ) {
Expand Down Expand Up @@ -220,22 +236,22 @@ export default function PageListEdit( {
</Button>
</PanelBody>
) }
<PanelBody>
{ parentOptions && (
{ pagesTree.length > 0 && (
<PanelBody>
<ComboboxControl
className="editor-page-attributes__parent"
label={ __( 'Parent page' ) }
value={ parentPageID }
options={ parentOptions }
options={ pagesTree }
onChange={ ( value ) =>
setAttributes( { parentPageID: value ?? 0 } )
}
help={ __(
'Choose a page to show only its subpages.'
) }
/>
) }
</PanelBody>
</PanelBody>
) }
</InspectorControls>
{ allowConvertToLinks && totalPages > 0 && (
<BlockControls group="other">
Expand Down