Skip to content

Commit

Permalink
Extensibility: Defer applying filters for component until it is about…
Browse files Browse the repository at this point in the history
… to be mounted (#4002)
  • Loading branch information
gziolo authored Dec 15, 2017
1 parent 7f346ea commit 0f35fc6
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 4 deletions.
2 changes: 1 addition & 1 deletion blocks/block-edit/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { withFilters } from '@wordpress/components';
*/
import { getBlockType } from '../api';

function BlockEdit( props ) {
export function BlockEdit( props ) {
const { name, ...editProps } = props;
const blockType = getBlockType( name );

Expand Down
2 changes: 1 addition & 1 deletion blocks/block-edit/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { noop } from 'lodash';
/**
* Internal dependencies
*/
import BlockEdit from '../';
import { BlockEdit } from '../';
import {
registerBlockType,
unregisterBlockType,
Expand Down
18 changes: 16 additions & 2 deletions components/higher-order/with-filters/index.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,30 @@
/**
* WordPress dependencies
*/
import { Component, getWrapperDisplayName } from '@wordpress/element';
import { applyFilters } from '@wordpress/hooks';

/**
* Creates a higher-order component which adds filtering capability to the wrapped component.
* Filters get applied when the original component is about to be mounted.
*
* @param {String} hookName Hook name exposed to be used by filters.
* @return {Function} Higher-order component factory.
* @return {Function} Higher-order component factory.
*/
export default function withFilters( hookName ) {
return ( OriginalComponent ) => {
return applyFilters( hookName, OriginalComponent );
class FilteredComponent extends Component {
constructor( props ) {
super( props );
this.Component = applyFilters( hookName, OriginalComponent );
}

render() {
return <this.Component { ...this.props } />;
}
}
FilteredComponent.displayName = getWrapperDisplayName( OriginalComponent, 'filters' );

return FilteredComponent;
};
}

0 comments on commit 0f35fc6

Please sign in to comment.