lingui-react
Advanced tools
Comparing version 0.6.0 to 0.7.0
@@ -26,3 +26,5 @@ 'use strict'; | ||
_ref$update = _ref.update, | ||
update = _ref$update === undefined ? true : _ref$update; | ||
update = _ref$update === undefined ? true : _ref$update, | ||
_ref$withRef = _ref.withRef, | ||
withRef = _ref$withRef === undefined ? false : _ref$withRef; | ||
@@ -46,3 +48,11 @@ return function (WrappedComponent) { | ||
return _ret = (_temp = (_this = _possibleConstructorReturn(this, (_ref2 = WithI18n.__proto__ || Object.getPrototypeOf(WithI18n)).call.apply(_ref2, [this].concat(args))), _this), _this.checkUpdate = function () { | ||
return _ret = (_temp = (_this = _possibleConstructorReturn(this, (_ref2 = WithI18n.__proto__ || Object.getPrototypeOf(WithI18n)).call.apply(_ref2, [this].concat(args))), _this), _this.wrappedInstance = null, _this.setWrappedInstance = function (ref) { | ||
if (withRef) _this.wrappedInstance = ref; | ||
}, _this.getWrappedInstance = function () { | ||
if (!withRef) { | ||
throw new Error('To access the wrapped instance, you need to specify { withRef: true }' + ' in the options argument of the withI18n() call.'); | ||
} | ||
return _this.wrappedInstance; | ||
}, _this.checkUpdate = function () { | ||
_this.forceUpdate(); | ||
@@ -83,3 +93,3 @@ }, _temp), _possibleConstructorReturn(_this, _ret); | ||
return _react2.default.createElement(WrappedComponent, _extends({}, this.props, { i18n: i18n })); | ||
return _react2.default.createElement(WrappedComponent, _extends({ ref: this.setWrappedInstance }, this.props, { i18n: i18n })); | ||
} | ||
@@ -86,0 +96,0 @@ }]); |
{ | ||
"name": "lingui-react", | ||
"version": "0.6.0", | ||
"version": "0.7.0", | ||
"description": "React components for translations", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -6,3 +6,4 @@ // @flow | ||
type WithI18nOptions = { | ||
update?: boolean | ||
update?: boolean, | ||
withRef?: boolean | ||
} | ||
@@ -14,3 +15,3 @@ | ||
export default ({ update = true }: WithI18nOptions = {}) => function<P, C: React$Component<*, P, *>> (WrappedComponent: Class<C>): Class<React.Component<void, $Diff<P, WithI18nProps>, void>> { | ||
export default ({ update = true, withRef = false }: WithI18nOptions = {}) => function<P, C: React$Component<*, P, *>> (WrappedComponent: Class<C>): Class<React.Component<void, $Diff<P, WithI18nProps>, void>> { | ||
return class WithI18n extends React.Component { | ||
@@ -21,2 +22,19 @@ static contextTypes = { | ||
wrappedInstance = null | ||
setWrappedInstance = ref => { | ||
if (withRef) this.wrappedInstance = ref | ||
} | ||
getWrappedInstance = () => { | ||
if (!withRef) { | ||
throw new Error( | ||
'To access the wrapped instance, you need to specify { withRef: true }' + | ||
' in the options argument of the withI18n() call.' | ||
) | ||
} | ||
return this.wrappedInstance | ||
} | ||
componentDidMount () { | ||
@@ -47,3 +65,3 @@ if (update) { | ||
// $FlowIgnore: https://github.com/facebook/flow/issues/3241 | ||
return <WrappedComponent {...this.props} i18n={i18n} /> | ||
return <WrappedComponent ref={this.setWrappedInstance} {...this.props} i18n={i18n} /> | ||
} | ||
@@ -50,0 +68,0 @@ } |
@@ -22,5 +22,9 @@ // @flow | ||
const spy = jest.fn() | ||
const Sink = WithI18n(options)((props) => { | ||
spy(props) | ||
return null | ||
const Sink = WithI18n(options)(class extends React.Component { | ||
customMethod = () => 42 | ||
render () { | ||
spy(this.props) | ||
return <div /> | ||
} | ||
}) | ||
@@ -92,2 +96,16 @@ return { Sink, spy } | ||
}) | ||
it('should hold ref to wrapped instance when withRef is enabled', function () { | ||
const { node } = mountHoc({}, { withRef: true }) | ||
expect(node.node.getWrappedInstance()).not.toBeNull() | ||
expect(node.node.getWrappedInstance().customMethod).not.toBeNull() | ||
expect(node.node.getWrappedInstance().customMethod()).toEqual(42) | ||
}) | ||
it('should not hold ref to wrapped instance when withRef is disabled', function () { | ||
const { node } = mountHoc() | ||
expect(() => node.node.getWrappedInstance()).toThrow( | ||
'To access the wrapped instance, you need to specify { withRef: true } in the options argument of the withI18n() call.' | ||
) | ||
}) | ||
}) |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
115146
2123