-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extensibility: Defer applying filters for component until it is about…
… to be mounted (#4002)
- Loading branch information
Showing
3 changed files
with
18 additions
and
4 deletions.
There are no files selected for viewing
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
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
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,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; | ||
}; | ||
} |