You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

50 lines
2.5 KiB

  1. import _extends from "@babel/runtime/helpers/extends";
  2. import _defineProperty from "@babel/runtime/helpers/defineProperty";
  3. import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
  4. import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
  5. function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }
  6. function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(source, true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(source).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
  7. import React from 'react';
  8. import { useTranslation } from './useTranslation';
  9. import { getDisplayName } from './utils';
  10. export function withTranslation(ns) {
  11. var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  12. return function Extend(WrappedComponent) {
  13. function I18nextWithTranslation(_ref) {
  14. var forwardedRef = _ref.forwardedRef,
  15. rest = _objectWithoutProperties(_ref, ["forwardedRef"]);
  16. var _useTranslation = useTranslation(ns, rest),
  17. _useTranslation2 = _slicedToArray(_useTranslation, 3),
  18. t = _useTranslation2[0],
  19. i18n = _useTranslation2[1],
  20. ready = _useTranslation2[2];
  21. var passDownProps = _objectSpread({}, rest, {
  22. t: t,
  23. i18n: i18n,
  24. tReady: ready
  25. });
  26. if (options.withRef && forwardedRef) {
  27. passDownProps.ref = forwardedRef;
  28. }
  29. return React.createElement(WrappedComponent, passDownProps);
  30. }
  31. I18nextWithTranslation.displayName = "withI18nextTranslation(".concat(getDisplayName(WrappedComponent), ")");
  32. I18nextWithTranslation.WrappedComponent = WrappedComponent;
  33. var forwardRef = function forwardRef(props, ref) {
  34. return React.createElement(I18nextWithTranslation, _extends({}, props, {
  35. forwardedRef: ref
  36. }));
  37. };
  38. return options.withRef ? React.forwardRef(forwardRef) : I18nextWithTranslation;
  39. };
  40. }