Skip to content

Commit 15ece2a

Browse files
ramymaryandrewjohnson
authored andcommitted
Added hoist-non-react-statics to withLocalized HOC (#130)
* -Added hoist-non-react-statics to withLocalized HOC * -Added test case for hoisting statics in withLoclize HOC
1 parent 3eb5875 commit 15ece2a

File tree

4 files changed

+40
-10
lines changed

4 files changed

+40
-10
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@
106106
"dependencies": {
107107
"create-react-context": "^0.2.2",
108108
"flat": "^2.0.1",
109+
"hoist-non-react-statics": "^3.0.1",
109110
"prop-types": "^15.6.1",
110111
"reselect": "^3.0.1"
111112
}

src/withLocalize.js

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
import React, { Component, type ComponentType } from 'react';
2+
import hoistNonReactStatic from 'hoist-non-react-statics';
23
import { LocalizeContext, type LocalizeContextProps } from './LocalizeContext';
34

45
export function withLocalize<Props: {}>(
56
WrappedComponent: ComponentType<Props>
67
): ComponentType<$Diff<Props, { ...LocalizeContextProps }>> {
7-
const LocalizedComponent = (props: Props) => {
8-
return (
9-
<LocalizeContext.Consumer>
10-
{context => <WrappedComponent {...context} {...props} />}
11-
</LocalizeContext.Consumer>
12-
);
13-
};
14-
8+
class LocalizedComponent extends Component {
9+
render() {
10+
return (
11+
<LocalizeContext.Consumer>
12+
{context => <WrappedComponent {...context} {...this.props} />}
13+
</LocalizeContext.Consumer>
14+
);
15+
}
16+
}
17+
hoistNonReactStatic(LocalizedComponent, WrappedComponent);
1518
return LocalizedComponent;
1619
}

tests/withLocalize.test.js

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ const getWithLocalizeWithContext = () => {
2323
}
2424
}
2525
});
26-
26+
2727
return require('withLocalize').withLocalize;
2828
};
2929

@@ -50,4 +50,20 @@ describe('withLocalize', () => {
5050
const wrapper = result.dive();
5151
expect(wrapper.props().name).toEqual('Testy McTest');
5252
});
53-
});
53+
54+
it('should hoist any existing static functions on WrappedComponent', () => {
55+
const withLocalize = getWithLocalizeWithContext();
56+
class WrapperComponent extends React.Component {
57+
static sayHello() {
58+
return 'hello';
59+
}
60+
render() {
61+
return (
62+
<h1>Hello You!</h1>
63+
);
64+
}
65+
};
66+
const Wrapped = withLocalize(WrapperComponent);
67+
expect(Wrapped.sayHello()).toEqual('hello')
68+
});
69+
});

yarn.lock

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2830,6 +2830,12 @@ hmac-drbg@^1.0.0:
28302830
minimalistic-assert "^1.0.0"
28312831
minimalistic-crypto-utils "^1.0.1"
28322832

2833+
hoist-non-react-statics@^3.0.1:
2834+
version "3.0.1"
2835+
resolved "https://registry.yarnpkg.com/hoist-non-react-statics/-/hoist-non-react-statics-3.0.1.tgz#fba3e7df0210eb9447757ca1a7cb607162f0a364"
2836+
dependencies:
2837+
react-is "^16.3.2"
2838+
28332839
home-or-tmp@^2.0.0:
28342840
version "2.0.0"
28352841
resolved "https://registry.yarnpkg.com/home-or-tmp/-/home-or-tmp-2.0.0.tgz#e36c3f2d2cae7d746a857e38d18d5f32a7882db8"
@@ -4981,6 +4987,10 @@ react-dom@^16.3.1:
49814987
object-assign "^4.1.1"
49824988
prop-types "^15.6.0"
49834989

4990+
react-is@^16.3.2:
4991+
version "16.5.2"
4992+
resolved "https://registry.yarnpkg.com/react-is/-/react-is-16.5.2.tgz#e2a7b7c3f5d48062eb769fcb123505eb928722e3"
4993+
49844994
react-is@^16.4.0:
49854995
version "16.4.0"
49864996
resolved "https://registry.yarnpkg.com/react-is/-/react-is-16.4.0.tgz#cc9fdc855ac34d2e7d9d2eb7059bbc240d35ffcf"

0 commit comments

Comments
 (0)