react-intl
Version:
Internationalize React apps. This library provides React components and an API to format dates, numbers, and strings, including pluralization and handling translations.
35 lines (34 loc) • 1.9 kB
JavaScript
import { __assign } from "tslib";
import * as React from 'react';
import hoistNonReactStatics_ from 'hoist-non-react-statics';
// Since rollup cannot deal with namespace being a function,
// this is to interop with TypeScript since `invariant`
// does not export a default
// https://github.com/rollup/rollup/issues/1267
var hoistNonReactStatics = hoistNonReactStatics_.default || hoistNonReactStatics_;
import { invariantIntlContext } from '../utils';
function getDisplayName(Component) {
return Component.displayName || Component.name || 'Component';
}
// TODO: We should provide initial value here
var IntlContext = React.createContext(null);
var IntlConsumer = IntlContext.Consumer, IntlProvider = IntlContext.Provider;
export var Provider = IntlProvider;
export var Context = IntlContext;
export default function injectIntl(WrappedComponent, options) {
var _a = options || {}, _b = _a.intlPropName, intlPropName = _b === void 0 ? 'intl' : _b, _c = _a.forwardRef, forwardRef = _c === void 0 ? false : _c, _d = _a.enforceContext, enforceContext = _d === void 0 ? true : _d;
var WithIntl = function (props) { return (React.createElement(IntlConsumer, null, function (intl) {
var _a;
if (enforceContext) {
invariantIntlContext(intl);
}
var intlProp = (_a = {}, _a[intlPropName] = intl, _a);
return (React.createElement(WrappedComponent, __assign({}, props, intlProp, { ref: forwardRef ? props.forwardedRef : null })));
})); };
WithIntl.displayName = "injectIntl(" + getDisplayName(WrappedComponent) + ")";
WithIntl.WrappedComponent = WrappedComponent;
if (forwardRef) {
return hoistNonReactStatics(React.forwardRef(function (props, ref) { return (React.createElement(WithIntl, __assign({}, props, { forwardedRef: ref }))); }), WrappedComponent);
}
return hoistNonReactStatics(WithIntl, WrappedComponent);
}