add-library.js
2 years ago
category-list.js
1 year ago
insert-pattern.js
2 years ago
library-cache.js
2 years ago
library-layout.js
1 year ago
library-provider.js
2 years ago
library-selector.js
1 year ago
manage-libraries.js
2 years ago
pattern-details-header.js
1 year ago
pattern-details.js
1 year ago
pattern-list.js
1 year ago
pattern-search.js
2 years ago
pattern.js
1 year ago
selected-patterns.js
1 year ago
category-list.js
80 lines
| 1 | import { Button, DropdownMenu, MenuGroup, MenuItem } from '@wordpress/components'; |
| 2 | import { useLibrary } from './library-provider'; |
| 3 | import { __, sprintf } from '@wordpress/i18n'; |
| 4 | import { check, chevronDown } from '@wordpress/icons'; |
| 5 | import { decodeEntities } from '@wordpress/html-entities'; |
| 6 | |
| 7 | export default function CategoryList( { bulkInsertEnabled } ) { |
| 8 | const { categories, activeCategory, setActiveCategory } = useLibrary(); |
| 9 | |
| 10 | const getNameById = ( id ) => { |
| 11 | const item = categories.find( ( element ) => element.id === id ); |
| 12 | return item ? decodeEntities( item.name ) : null; |
| 13 | }; |
| 14 | |
| 15 | return ( |
| 16 | <div className="pattern-category-list" style={ { background: !! bulkInsertEnabled ? 'none' : '' } }> |
| 17 | { !! bulkInsertEnabled ? ( |
| 18 | <DropdownMenu |
| 19 | className="pattern-category-dropdown" |
| 20 | icon={ chevronDown } |
| 21 | toggleProps={ { |
| 22 | variant: 'secondary', |
| 23 | children: sprintf( |
| 24 | /* translators: %s: category name */ |
| 25 | __( 'Category: %s', 'generateblocks' ), |
| 26 | getNameById( activeCategory ) || __( 'All', 'generateblocks' ) |
| 27 | ), |
| 28 | } } |
| 29 | > |
| 30 | { ( { onClose } ) => ( |
| 31 | <MenuGroup> |
| 32 | <MenuItem |
| 33 | isPressed={ '' === activeCategory } |
| 34 | onClick={ () => { |
| 35 | setActiveCategory( '' ); |
| 36 | onClose(); |
| 37 | } } |
| 38 | > |
| 39 | { __( 'All', 'generateblocks' ) } |
| 40 | </MenuItem> |
| 41 | { categories && categories.map( ( category ) => ( |
| 42 | <MenuItem |
| 43 | key={ category.id } |
| 44 | icon={ category.id === activeCategory ? check : null } |
| 45 | onClick={ () => { |
| 46 | setActiveCategory( category.id ); |
| 47 | onClose(); |
| 48 | } } |
| 49 | > |
| 50 | { decodeEntities( category.name ) } |
| 51 | </MenuItem> |
| 52 | ) ) } |
| 53 | </MenuGroup> |
| 54 | ) } |
| 55 | </DropdownMenu> |
| 56 | ) : ( |
| 57 | <> |
| 58 | <Button |
| 59 | id="pattern-category-all" |
| 60 | isPressed={ '' === activeCategory } |
| 61 | onClick={ () => setActiveCategory( '' ) } |
| 62 | > |
| 63 | { __( 'All', 'generateblocks' ) } |
| 64 | </Button> |
| 65 | { categories && categories.map( ( category ) => ( |
| 66 | <Button |
| 67 | id={ `pattern-category-${ category.id }` } |
| 68 | key={ category.id } |
| 69 | isPressed={ category.id === activeCategory } |
| 70 | onClick={ () => setActiveCategory( category.id ) } |
| 71 | > |
| 72 | { decodeEntities( category.name ) } |
| 73 | </Button> |
| 74 | ) ) } |
| 75 | </> |
| 76 | ) } |
| 77 | </div> |
| 78 | ); |
| 79 | } |
| 80 |