Skip to content

Commit

Permalink
Fixed regression for styles sidebar: if no category is passed to the …
Browse files Browse the repository at this point in the history
…Examples component it should render all available examples.

Renamed getCategoryExamples function to getExamplesByCategory
  • Loading branch information
ramonjd committed Sep 30, 2024
1 parent 10bf3f1 commit 032182e
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 14 deletions.
4 changes: 2 additions & 2 deletions packages/edit-site/src/components/style-book/categories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import type {
* @param {BlockExample[]} examples An array of block examples.
* @return {CategoryExamples|undefined} An object containing the category examples.
*/
export function getCategoryExamples(
export function getExamplesByCategory(
categoryDefinition: StyleBookCategory,
examples: BlockExample[]
): CategoryExamples | undefined {
Expand All @@ -24,7 +24,7 @@ export function getCategoryExamples(
if ( categoryDefinition?.subcategories?.length ) {
return categoryDefinition.subcategories.reduce(
( acc, subcategoryDefinition ) => {
const subcategoryExamples = getCategoryExamples(
const subcategoryExamples = getExamplesByCategory(
subcategoryDefinition,
examples
);
Expand Down
19 changes: 10 additions & 9 deletions packages/edit-site/src/components/style-book/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import { ENTER, SPACE } from '@wordpress/keycodes';
import { unlock } from '../../lock-unlock';
import EditorCanvasContainer from '../editor-canvas-container';
import { STYLE_BOOK_CATEGORIES, STYLE_BOOK_IFRAME_STYLES } from './constants';
import { getCategoryExamples } from './categories';
import { getExamplesByCategory } from './categories';
import { getExamples } from './examples';

const {
Expand Down Expand Up @@ -150,7 +150,6 @@ function StyleBook( {
</div>
) : (
<StyleBookBody
category={ tabs[ 0 ].slug }
examples={ examples }
isSelected={ isSelected }
onClick={ onClick }
Expand Down Expand Up @@ -245,13 +244,15 @@ const StyleBookBody = ( {

const Examples = memo(
( { className, examples, category, label, isSelected, onSelect } ) => {
const categoryDefinition = STYLE_BOOK_CATEGORIES.find(
( _category ) => _category.slug === category
);
const filteredExamples = getCategoryExamples(
categoryDefinition,
examples
);
const categoryDefinition = category
? STYLE_BOOK_CATEGORIES.find(
( _category ) => _category.slug === category
)
: null;

const filteredExamples = categoryDefinition
? getExamplesByCategory( categoryDefinition, examples )
: { examples };

return (
<Composite
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* Internal dependencies
*/
import { getCategoryExamples } from '../categories';
import { getExamplesByCategory } from '../categories';
import { STYLE_BOOK_CATEGORIES } from '../constants';

// Fixtures
Expand Down Expand Up @@ -59,12 +59,12 @@ const exampleThemeBlocks = [
];

describe( 'utils', () => {
describe( 'getCategoryExamples', () => {
describe( 'getExamplesByCategory', () => {
it( 'returns theme subcategories examples', () => {
const themeCategory = STYLE_BOOK_CATEGORIES.find(
( category ) => category.slug === 'theme'
);
const themeCategoryExamples = getCategoryExamples(
const themeCategoryExamples = getExamplesByCategory(
themeCategory,
exampleThemeBlocks
);
Expand Down

0 comments on commit 032182e

Please sign in to comment.