Skip to content
This repository was archived by the owner on Jan 10, 2024. It is now read-only.

Commit cbf2a0f

Browse files
Merge pull request #12 from eugene-manuilov/feature/with-refs
Feature/with refs
2 parents 54310ad + dae266d commit cbf2a0f

File tree

3 files changed

+28
-16
lines changed

3 files changed

+28
-16
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
**Implemented enhancements:**
66

7-
-
7+
- Implemented ability to get wrapped component.
88

99
## v0.3.3 (2017-08-12)
1010

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,12 +104,14 @@ See an [example](https://github.com/eugene-manuilov/react-gettext/tree/master/ex
104104

105105
## Documentation
106106

107-
### withGettext(translations, pluralForms)
107+
### withGettext(translations, pluralForms, options)
108108

109109
Higher-order function which is exported by default from `react-gettext` package. It accepts two arguments and returns function to create higher-order component.
110110

111111
- **translations**: a hash object or a function which returns hash object where keys are original messages and values are translated messages.
112112
- **pluralForms**: a string to calculate plural form (used by [Gettext PO](http://docs.translatehouse.org/projects/localization-guide/en/latest/l10n/pluralforms.html?id=l10n/pluralforms)) or a function which accepts a number and calculates a plural form number. Pay attentions that plural forms are zero-based what means to get 1st plural form it should return 0, to get 2nd - 1, and so on.
113+
- **options**: a hash object with options. Currently supports following options:
114+
- **withRef**: an optional boolean flag that determines whether or not to set `ref` property to a wrapped component what will allow you to get wrapped component instance by calling `getWrappedComponent()` function of the HOC. By default: `FALSE`.
113115

114116
Example:
115117

src/index.js

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,35 @@ import React from 'react';
22
import hoistNonReactStatic from 'hoist-non-react-statics';
33
import Textdomain from './Textdomain';
44

5-
export { Textdomain };
5+
const withGettext = (translations = {}, pluralForm = 'n != 1', options = {}) => (WrappedComponent) => {
6+
const args = Object.assign({ withRef: false }, options);
7+
8+
class WithGettext extends Textdomain {
69

7-
export default function withGettext(translations = {}, pluralForm = 'n != 1') {
8-
return (WrappedComponent) => {
9-
class WithGettext extends Textdomain {
10+
getWrappedComponent() {
11+
return this.refs.wrappedComponent;
12+
}
1013

11-
render() {
12-
return React.createElement(WrappedComponent, this.props);
14+
render() {
15+
const newprops = Object.assign({}, this.props);
16+
if (args.withRef) {
17+
newprops.ref = 'wrappedComponent';
1318
}
1419

20+
return React.createElement(WrappedComponent, newprops);
1521
}
1622

17-
WithGettext.defaultProps = {
18-
translations,
19-
plural: pluralForm,
20-
};
21-
22-
WithGettext.displayName = `withGettext(${WrappedComponent.displayName || WrappedComponent.name || 'Component'})`;
23+
}
2324

24-
return hoistNonReactStatic(WithGettext, WrappedComponent);
25+
WithGettext.defaultProps = {
26+
translations,
27+
plural: pluralForm,
2528
};
26-
}
29+
30+
WithGettext.displayName = `withGettext(${WrappedComponent.displayName || WrappedComponent.name || 'Component'})`;
31+
32+
return hoistNonReactStatic(WithGettext, WrappedComponent);
33+
};
34+
35+
export { Textdomain };
36+
export default withGettext;

0 commit comments

Comments
 (0)