Skip to content

Commit

Permalink
Design Picker: Allow empty selection
Browse files Browse the repository at this point in the history
  • Loading branch information
arthur791004 committed Dec 11, 2024
1 parent 98b37ca commit d51b299
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,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
21 changes: 10 additions & 11 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 Down

0 comments on commit d51b299

Please sign in to comment.