forked from WordPress/gutenberg
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: replaced hardcoded values with dynamic approach
- Loading branch information
Showing
2 changed files
with
109 additions
and
97 deletions.
There are no files selected for viewing
167 changes: 82 additions & 85 deletions
167
packages/block-editor/src/components/block-manager/category.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,105 +1,102 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useCallback, forwardRef } from '@wordpress/element'; | ||
import { useCallback } from '@wordpress/element'; | ||
import { useInstanceId } from '@wordpress/compose'; | ||
import { CheckboxControl } from '@wordpress/components'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import BlockTypesChecklist from './checklist'; | ||
const a = forwardRef( () => { | ||
return a; | ||
} ); | ||
|
||
const BlockManagerCategory = forwardRef( | ||
( { title, blockTypes, selectedBlockTypes, onChange }, ref ) => { | ||
const instanceId = useInstanceId( BlockManagerCategory ); | ||
const BlockManagerCategory = ( { | ||
title, | ||
blockTypes, | ||
selectedBlockTypes, | ||
onChange, | ||
} ) => { | ||
const instanceId = useInstanceId( BlockManagerCategory ); | ||
|
||
const toggleVisible = useCallback( | ||
( blockType, nextIsChecked ) => { | ||
if ( nextIsChecked ) { | ||
onChange( [ ...selectedBlockTypes, blockType ] ); | ||
} else { | ||
onChange( | ||
selectedBlockTypes.filter( | ||
( { name } ) => name !== blockType.name | ||
) | ||
); | ||
} | ||
}, | ||
[ selectedBlockTypes, onChange ] | ||
); | ||
const toggleVisible = useCallback( | ||
( blockType, nextIsChecked ) => { | ||
if ( nextIsChecked ) { | ||
onChange( [ ...selectedBlockTypes, blockType ] ); | ||
} else { | ||
onChange( | ||
selectedBlockTypes.filter( | ||
( { name } ) => name !== blockType.name | ||
) | ||
); | ||
} | ||
}, | ||
[ selectedBlockTypes, onChange ] | ||
); | ||
|
||
const toggleAllVisible = useCallback( | ||
( nextIsChecked ) => { | ||
if ( nextIsChecked ) { | ||
onChange( [ | ||
...selectedBlockTypes, | ||
...blockTypes.filter( | ||
( blockType ) => | ||
! selectedBlockTypes.find( | ||
( { name } ) => name === blockType.name | ||
) | ||
), | ||
] ); | ||
} else { | ||
onChange( | ||
selectedBlockTypes.filter( | ||
( selectedBlockType ) => | ||
! blockTypes.find( | ||
( { name } ) => | ||
name === selectedBlockType.name | ||
) | ||
) | ||
); | ||
} | ||
}, | ||
[ blockTypes, selectedBlockTypes, onChange ] | ||
); | ||
const toggleAllVisible = useCallback( | ||
( nextIsChecked ) => { | ||
if ( nextIsChecked ) { | ||
onChange( [ | ||
...selectedBlockTypes, | ||
...blockTypes.filter( | ||
( blockType ) => | ||
! selectedBlockTypes.find( | ||
( { name } ) => name === blockType.name | ||
) | ||
), | ||
] ); | ||
} else { | ||
onChange( | ||
selectedBlockTypes.filter( | ||
( selectedBlockType ) => | ||
! blockTypes.find( | ||
( { name } ) => name === selectedBlockType.name | ||
) | ||
) | ||
); | ||
} | ||
}, | ||
[ blockTypes, selectedBlockTypes, onChange ] | ||
); | ||
|
||
if ( ! blockTypes.length ) { | ||
return null; | ||
} | ||
if ( ! blockTypes.length ) { | ||
return null; | ||
} | ||
|
||
const checkedBlockNames = blockTypes | ||
.map( ( { name } ) => name ) | ||
.filter( ( type ) => | ||
( selectedBlockTypes ?? [] ).some( | ||
( selectedBlockType ) => selectedBlockType.name === type | ||
) | ||
); | ||
const checkedBlockNames = blockTypes | ||
.map( ( { name } ) => name ) | ||
.filter( ( type ) => | ||
( selectedBlockTypes ?? [] ).some( | ||
( selectedBlockType ) => selectedBlockType.name === type | ||
) | ||
); | ||
|
||
const titleId = | ||
'block-editor-block-manager__category-title-' + instanceId; | ||
const titleId = 'block-editor-block-manager__category-title-' + instanceId; | ||
|
||
const isAllChecked = checkedBlockNames.length === blockTypes.length; | ||
const isIndeterminate = ! isAllChecked && checkedBlockNames.length > 0; | ||
const isAllChecked = checkedBlockNames.length === blockTypes.length; | ||
const isIndeterminate = ! isAllChecked && checkedBlockNames.length > 0; | ||
|
||
return ( | ||
<div | ||
role="group" | ||
aria-labelledby={ titleId } | ||
className="block-editor-block-manager__category" | ||
ref={ ref } | ||
> | ||
<CheckboxControl | ||
__nextHasNoMarginBottom | ||
checked={ isAllChecked } | ||
onChange={ toggleAllVisible } | ||
className="block-editor-block-manager__category-title" | ||
indeterminate={ isIndeterminate } | ||
label={ <span id={ titleId }>{ title }</span> } | ||
/> | ||
<BlockTypesChecklist | ||
blockTypes={ blockTypes } | ||
value={ checkedBlockNames } | ||
onItemChange={ toggleVisible } | ||
/> | ||
</div> | ||
); | ||
} | ||
); | ||
return ( | ||
<div | ||
role="group" | ||
aria-labelledby={ titleId } | ||
className="block-editor-block-manager__category" | ||
> | ||
<CheckboxControl | ||
__nextHasNoMarginBottom | ||
checked={ isAllChecked } | ||
onChange={ toggleAllVisible } | ||
className="block-editor-block-manager__category-title" | ||
indeterminate={ isIndeterminate } | ||
label={ <span id={ titleId }>{ title }</span> } | ||
/> | ||
<BlockTypesChecklist | ||
blockTypes={ blockTypes } | ||
value={ checkedBlockNames } | ||
onItemChange={ toggleVisible } | ||
/> | ||
</div> | ||
); | ||
}; | ||
|
||
export default BlockManagerCategory; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters