Skip to content

Commit 4fe409a

Browse files
authored
Merge pull request #2 from netceteragroup/forward-refs
Support for ref forwarding in withMessages HOC
2 parents 8296888 + 7d03a12 commit 4fe409a

File tree

2 files changed

+49
-5
lines changed

2 files changed

+49
-5
lines changed

src/lib/withMessages.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ function enhanceWithMessages(keyPrefix, WrappedComponent) {
1212
/**
1313
* The enhancer HOC.
1414
*/
15-
function Enhancer(props) {
15+
function WithMessages(props) {
1616
const messageSourceApi = useMessageSource(keyPrefix);
1717
if (process.env.NODE_ENV !== 'production') {
1818
const hasOwn = Object.prototype.hasOwnProperty;
@@ -28,11 +28,17 @@ function enhanceWithMessages(keyPrefix, WrappedComponent) {
2828
);
2929
}
3030

31-
return <WrappedComponent {...props} {...messageSourceApi} />;
31+
// eslint-disable-next-line react/prop-types
32+
const { forwardedRef, ...rest } = props;
33+
return <WrappedComponent {...rest} {...messageSourceApi} ref={forwardedRef} />;
3234
}
3335

34-
Enhancer.displayName = `WithMessages(${wrappedComponentName})`;
35-
return hoistNonReactStatics(Enhancer, WrappedComponent);
36+
WithMessages.displayName = `WithMessages(${wrappedComponentName})`;
37+
38+
return hoistNonReactStatics(
39+
React.forwardRef((props, ref) => <WithMessages {...props} forwardedRef={ref} />),
40+
WrappedComponent,
41+
);
3642
}
3743

3844
/**

src/lib/withMessages.test.js

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React from 'react';
1+
import React, { Component } from 'react';
22
import TestRenderer from 'react-test-renderer';
33
import { Provider as MessageSourceProvider } from './MessageSourceContext';
44
import * as MessageSource from './withMessages';
@@ -164,4 +164,42 @@ describe('withMessages', () => {
164164
expect(levelOneComponent.children[0]).toBe('Hello World');
165165
expect(levelTwoComponent.children[0]).toBe('Hallo Welt');
166166
});
167+
168+
it('supports ref forwarding', () => {
169+
const NestedHOC = MessageSource.withMessages('hello')(
170+
class Nested extends Component {
171+
myMethod = () => {
172+
return 100;
173+
};
174+
175+
render() {
176+
const { getMessageWithNamedParams } = this.props; // eslint-disable-line react/prop-types
177+
return <React.Fragment>{getMessageWithNamedParams('hello.world')}</React.Fragment>;
178+
}
179+
},
180+
);
181+
182+
// eslint-disable-next-line react/no-multi-comp
183+
class MyFwRefComponent extends Component {
184+
nestedRef = React.createRef();
185+
186+
render() {
187+
return <NestedHOC ref={this.nestedRef} />;
188+
}
189+
}
190+
191+
const renderer = TestRenderer.create(
192+
<MessageSourceProvider value={translations}>
193+
<MyFwRefComponent />
194+
</MessageSourceProvider>,
195+
);
196+
197+
const { root } = renderer;
198+
const fwRefCompInstance = root.findByType(MyFwRefComponent);
199+
200+
expect(fwRefCompInstance.instance).toBeDefined();
201+
expect(fwRefCompInstance.instance.nestedRef.current).toBeDefined();
202+
expect(fwRefCompInstance.instance.nestedRef.current.myMethod).toBeDefined();
203+
expect(fwRefCompInstance.instance.nestedRef.current.myMethod()).toBe(100);
204+
});
167205
});

0 commit comments

Comments
 (0)