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

Categories List Block: Match the markup on the block editor to the front end #43666

Merged
merged 2 commits into from
Sep 10, 2022
Merged
Changes from 1 commit
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
62 changes: 37 additions & 25 deletions packages/block-library/src/categories/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* External dependencies
*/
import { unescape } from 'lodash';
import classnames from 'classnames';

/**
* WordPress dependencies
Expand All @@ -28,17 +29,20 @@ export default function CategoriesEdit( {
showEmpty,
},
setAttributes,
className,
} ) {
const selectId = useInstanceId( CategoriesEdit, 'blocks-category-select' );
const query = { per_page: -1, hide_empty: ! showEmpty, context: 'view' };
if ( showOnlyTopLevel ) {
query.parent = 0;
}

const { records: categories, isResolving } = useEntityRecords(
'taxonomy',
'category',
query
);

const getCategoriesList = ( parentId ) => {
if ( ! categories?.length ) {
return [];
Expand All @@ -48,48 +52,41 @@ export default function CategoriesEdit( {
}
return categories.filter( ( { parent } ) => parent === parentId );
};
const getCategoryListClassName = ( level ) => {
return `wp-block-categories__list wp-block-categories__list-level-${ level }`;
};

const toggleAttribute = ( attributeName ) => ( newValue ) =>
setAttributes( { [ attributeName ]: newValue } );

const renderCategoryName = ( name ) =>
! name ? __( '(Untitled)' ) : unescape( name ).trim();

const renderCategoryList = () => {
const parentId = showHierarchy ? 0 : null;
const categoriesList = getCategoriesList( parentId );
return (
<ul className={ getCategoryListClassName( 0 ) }>
{ categoriesList.map( ( category ) =>
renderCategoryListItem( category, 0 )
) }
</ul>
return categoriesList.map( ( category ) =>
renderCategoryListItem( category, 0 )
);
};
const renderCategoryListItem = ( category, level ) => {

const renderCategoryListItem = ( category ) => {
const childCategories = getCategoriesList( category.id );
const { id, link, count, name } = category;
return (
<li key={ id }>
<li key={ id } className={ `cat-item cat-item-${ id }` }>
<a href={ link } target="_blank" rel="noreferrer noopener">
{ renderCategoryName( name ) }
</a>
{ showPostCounts && (
<span className="wp-block-categories__post-count">
{ ` (${ count })` }
</span>
) }
{ showPostCounts && ` (${ count })` }
{ showHierarchy && !! childCategories.length && (
<ul className={ getCategoryListClassName( level + 1 ) }>
<ul className="children">
{ childCategories.map( ( childCategory ) =>
renderCategoryListItem( childCategory, level + 1 )
renderCategoryListItem( childCategory )
) }
</ul>
) }
</li>
);
};

const renderCategoryDropdown = () => {
const parentId = showHierarchy ? 0 : null;
const categoriesList = getCategoriesList( parentId );
Expand All @@ -98,22 +95,21 @@ export default function CategoriesEdit( {
<VisuallyHidden as="label" htmlFor={ selectId }>
{ __( 'Categories' ) }
</VisuallyHidden>
<select
id={ selectId }
className="wp-block-categories__dropdown"
>
<select id={ selectId }>
<option>{ __( 'Select Category' ) }</option>
{ categoriesList.map( ( category ) =>
renderCategoryDropdownItem( category, 0 )
) }
</select>
</>
);
};

const renderCategoryDropdownItem = ( category, level ) => {
const { id, count, name } = category;
const childCategories = getCategoriesList( id );
return [
<option key={ id }>
<option key={ id } className={ `level-${ level }` }>
{ Array.from( { length: level * 3 } ).map( () => '\xa0' ) }
{ renderCategoryName( name ) }
{ showPostCounts && ` (${ count })` }
Expand All @@ -126,8 +122,24 @@ export default function CategoriesEdit( {
];
};

const TagName =
!! categories?.length && ! displayAsDropdown && ! isResolving
? 'ul'
: 'div';

const classes = classnames( className, {
'wp-block-categories-list':
!! categories?.length && ! displayAsDropdown && ! isResolving,
'wp-block-categories-dropdown':
!! categories?.length && displayAsDropdown && ! isResolving,
} );

const blockProps = useBlockProps( {
className: classes,
} );

return (
<div { ...useBlockProps() }>
<TagName { ...blockProps }>
<InspectorControls>
<PanelBody title={ __( 'Settings' ) }>
<ToggleControl
Expand Down Expand Up @@ -176,6 +188,6 @@ export default function CategoriesEdit( {
( displayAsDropdown
? renderCategoryDropdown()
: renderCategoryList() ) }
</div>
</TagName>
);
}