From b446894a1d21aa948a34fc39223b42129ef82765 Mon Sep 17 00:00:00 2001 From: gnoff Date: Fri, 31 Jul 2015 00:19:38 -0700 Subject: [PATCH] use ref callback to place instance of underlying component on wrapped component. provide wrapper method to get underlying ref to provide access to unerlying refs methods --- src/components/createConnectDecorator.js | 6 ++++- test/components/connect.spec.js | 34 ++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/src/components/createConnectDecorator.js b/src/components/createConnectDecorator.js index 705349009..d65f8878e 100644 --- a/src/components/createConnectDecorator.js +++ b/src/components/createConnectDecorator.js @@ -114,8 +114,12 @@ export default function createConnectDecorator(React) { return merged; } + getUnderlyingRef() { + return this.underlyingRef; + } + render() { - return ; + return (this.underlyingRef = component)} {...this.props} {...this.merge()} />; } }; }; diff --git a/test/components/connect.spec.js b/test/components/connect.spec.js index 5938d99c3..d8e35026b 100644 --- a/test/components/connect.spec.js +++ b/test/components/connect.spec.js @@ -514,5 +514,39 @@ describe('React', () => { expect(decorated.DecoratedComponent).toBe(Container); }); + + it('should return the instance of the wrapped component for use in calling child methods', () => { + const store = createStore(() => ({})); + + const someData = { + some: 'data' + }; + + class Container extends Component { + someInstanceMethod() { + return someData; + } + + render() { + return
; + } + } + + const decorator = connect(state => state); + const Decorated = decorator(Container); + + const tree = TestUtils.renderIntoDocument( + + {() => ( + + )} + + ); + + const decorated = TestUtils.findRenderedComponentWithType(tree, Decorated); + + expect(() => decorated.someInstanceMethod()).toThrow(); + expect(decorated.getUnderlyingRef().someInstanceMethod()).toBe(someData); + }); }); });