From 7a64c9ee8a66274b779980e01d06c093377373ee Mon Sep 17 00:00:00 2001 From: CHaBou Date: Tue, 28 Mar 2017 02:07:55 +0200 Subject: [PATCH] Add onDecorated property to decorated components (#1680) --- lib/utils/plugins.js | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/lib/utils/plugins.js b/lib/utils/plugins.js index 0a405dc4..31d563db 100644 --- a/lib/utils/plugins.js +++ b/lib/utils/plugins.js @@ -370,9 +370,27 @@ export const middleware = store => next => action => { nextMiddleware(middlewares)(action); }; +// expose decorated component instance to the higher-order components +function exposeDecorated(Component) { + return class extends React.Component { + constructor(props, context) { + super(props, context); + this.onRef = this.onRef.bind(this); + } + onRef(decorated) { + if (this.props.onDecorated) { + this.props.onDecorated(decorated); + } + } + render() { + return React.createElement(Component, Object.assign({}, this.props, {ref: this.onRef})); + } + }; +} + function getDecorated(parent, name) { if (!decorated[name]) { - let class_ = parent; + let class_ = exposeDecorated(parent); modules.forEach(mod => { const method = 'decorate' + name;