Skip to content

Commit

Permalink
Design Picker: Allow people to deselect all categories (#97266)
Browse files Browse the repository at this point in the history
* Design Picker: Allow empty selection

* Design Picker: Change the default selections if no selected goals
  • Loading branch information
arthur791004 authored Dec 11, 2024
1 parent 42c4698 commit 4782432
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,17 +60,21 @@ export function getCategorizationOptions(
goals: Onboard.SiteGoal[],
{ isMultiSelection = false }: CategorizationOptions = {}
) {
const defaultSelections = Array.from(
let defaultSelections = Array.from(
new Set(
goals
.map( ( goal ) => {
const preferredCategories = getGoalsPreferredCategories( goal );
return isMultiSelection ? preferredCategories : preferredCategories.slice( 0, 1 );
} )
.flat() ?? [ CATEGORIES.BUSINESS ]
.flat()
)
);

if ( defaultSelections.length === 0 ) {
defaultSelections = [ CATEGORIES.BLOG ];
}

return {
defaultSelections,
sort: makeSortCategoryToTop( defaultSelections ),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,9 @@ const DesignPicker: React.FC< DesignPickerProps > = ( {
designs={ best }
/>
) }
{ isMultiFilterEnabled && categorization && categorization.selections.length === 0 && (
<DesignCardGroup { ...designCardProps } designs={ all } />
) }
{ /* We want to show the last one on top first. */ }
{ Object.entries( designsByGroup )
.reverse()
Expand Down
30 changes: 16 additions & 14 deletions packages/design-picker/src/hooks/use-categorization.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useEffect, useMemo, useCallback } from 'react';
import { useEffect, useMemo, useCallback, useRef } from 'react';
import { useDesignPickerFilters } from './use-design-picker-filters';
import type { Category } from '../types';

Expand Down Expand Up @@ -26,6 +26,7 @@ export function useCategorization(
handleDeselect,
}: UseCategorizationOptions
): Categorization {
const isInitRef = useRef( false );
const categories = useMemo( () => {
const categoryMapKeys = Object.keys( categoryMap ) || [];
const result = categoryMapKeys.map( ( slug ) => ( {
Expand All @@ -52,23 +53,21 @@ export function useCategorization(
return setSelectedCategories( [ ...selectedCategories, value ] );
}

// The selections should at least have one.
if ( selectedCategories.length > 1 ) {
handleDeselect?.( value );
return setSelectedCategories( [
...selectedCategories.slice( 0, index ),
...selectedCategories.slice( index + 1 ),
] );
}
handleDeselect?.( value );
return setSelectedCategories( [
...selectedCategories.slice( 0, index ),
...selectedCategories.slice( index + 1 ),
] );
},
[ selectedCategories, isMultiSelection, setSelectedCategories, handleSelect, handleDeselect ]
);

useEffect( () => {
if ( categories.length > 0 && selectedCategories.length === 0 ) {
if ( ! isInitRef.current && categories.length > 0 && selectedCategories.length === 0 ) {
setSelectedCategories( chooseDefaultSelections( categories, defaultSelections ) );
isInitRef.current = true;
}
}, [ categories ] );
}, [ isInitRef, categories ] );

return {
categories,
Expand All @@ -86,9 +85,12 @@ export function useCategorization(
* @returns the default category or null if none is available
*/
function chooseDefaultSelections( categories: Category[], defaultSelections: string[] ): string[] {
const defaultSelectionsSet = new Set( defaultSelections );
if ( defaultSelections && categories.find( ( { slug } ) => defaultSelectionsSet.has( slug ) ) ) {
return defaultSelections;
const categorySlugsSet = new Set( categories.map( ( { slug } ) => slug ) );
const availableDefaultSelections = defaultSelections.filter( ( selection ) =>
categorySlugsSet.has( selection )
);
if ( availableDefaultSelections.length > 0 ) {
return availableDefaultSelections;
}

return categories[ 0 ]?.slug ? [ categories[ 0 ]?.slug ] : [];
Expand Down

0 comments on commit 4782432

Please sign in to comment.