index.js
76 lines
| 1 | import ApplyFilters from '../apply-filters/'; |
| 2 | |
| 3 | import { |
| 4 | PanelBody, |
| 5 | } from '@wordpress/components'; |
| 6 | |
| 7 | import { |
| 8 | Component, |
| 9 | } from '@wordpress/element'; |
| 10 | |
| 11 | import { |
| 12 | applyFilters, |
| 13 | } from '@wordpress/hooks'; |
| 14 | |
| 15 | /** |
| 16 | * Component Class |
| 17 | */ |
| 18 | export default class PanelArea extends Component { |
| 19 | render() { |
| 20 | const { |
| 21 | title = false, |
| 22 | initialOpen = false, |
| 23 | icon, |
| 24 | className, |
| 25 | id, |
| 26 | state, |
| 27 | showPanel = true, |
| 28 | children, |
| 29 | } = this.props; |
| 30 | |
| 31 | const show = applyFilters( 'generateblocks.editor.showPanel', showPanel, id, this.props ); |
| 32 | |
| 33 | if ( ! show ) { |
| 34 | return null; |
| 35 | } |
| 36 | |
| 37 | let hasChildren = true; |
| 38 | |
| 39 | if ( '' === children ) { |
| 40 | hasChildren = false; |
| 41 | } |
| 42 | |
| 43 | // If we have items in the panel, make sure they're not empty. |
| 44 | if ( 'object' === typeof children ) { |
| 45 | hasChildren = Object.values( children ).some( ( x ) => ( x !== null && x !== false && x !== '' ) ); |
| 46 | } |
| 47 | |
| 48 | if ( ! hasChildren ) { |
| 49 | return null; |
| 50 | } |
| 51 | |
| 52 | return ( |
| 53 | <ApplyFilters name={ 'generateblocks.panel.' + id } props={ this.props } state={ state }> |
| 54 | { title ? ( |
| 55 | <PanelBody |
| 56 | title={ title } |
| 57 | initialOpen={ initialOpen } |
| 58 | icon={ icon } |
| 59 | className={ className } |
| 60 | > |
| 61 | { |
| 62 | applyFilters( 'generateblocks.editor.panelContents', children, id, this.props ) |
| 63 | } |
| 64 | </PanelBody> |
| 65 | ) : ( |
| 66 | <PanelBody> |
| 67 | { |
| 68 | applyFilters( 'generateblocks.editor.panelContents', children, id, this.props ) |
| 69 | } |
| 70 | </PanelBody> |
| 71 | ) } |
| 72 | </ApplyFilters> |
| 73 | ); |
| 74 | } |
| 75 | } |
| 76 |