UNPKG

2.48 kBJavaScriptView Raw
1"use strict";
2
3var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard")["default"];
4var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault")["default"];
5Object.defineProperty(exports, "__esModule", {
6 value: true
7});
8exports.useLocaleReceiver = exports["default"] = void 0;
9var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
10var React = _interopRequireWildcard(require("react"));
11var _context = _interopRequireDefault(require("./context"));
12var _en_US = _interopRequireDefault(require("../locale/en_US"));
13var LocaleReceiver = function LocaleReceiver(props) {
14 var _props$componentName = props.componentName,
15 componentName = _props$componentName === void 0 ? 'global' : _props$componentName,
16 defaultLocale = props.defaultLocale,
17 children = props.children;
18 var antLocale = React.useContext(_context["default"]);
19 var getLocale = React.useMemo(function () {
20 var _a;
21 var locale = defaultLocale || _en_US["default"][componentName];
22 var localeFromContext = (_a = antLocale === null || antLocale === void 0 ? void 0 : antLocale[componentName]) !== null && _a !== void 0 ? _a : {};
23 return (0, _extends2["default"])((0, _extends2["default"])({}, locale instanceof Function ? locale() : locale), localeFromContext || {});
24 }, [componentName, defaultLocale, antLocale]);
25 var getLocaleCode = React.useMemo(function () {
26 var localeCode = antLocale && antLocale.locale;
27 // Had use LocaleProvide but didn't set locale
28 if (antLocale && antLocale.exist && !localeCode) {
29 return _en_US["default"].locale;
30 }
31 return localeCode;
32 }, [antLocale]);
33 return children(getLocale, getLocaleCode, antLocale);
34};
35var _default = LocaleReceiver;
36exports["default"] = _default;
37var useLocaleReceiver = function useLocaleReceiver(componentName, defaultLocale) {
38 var antLocale = React.useContext(_context["default"]);
39 var getLocale = React.useMemo(function () {
40 var _a;
41 var locale = defaultLocale || _en_US["default"][componentName];
42 var localeFromContext = (_a = antLocale === null || antLocale === void 0 ? void 0 : antLocale[componentName]) !== null && _a !== void 0 ? _a : {};
43 return (0, _extends2["default"])((0, _extends2["default"])({}, typeof locale === 'function' ? locale() : locale), localeFromContext || {});
44 }, [componentName, defaultLocale, antLocale]);
45 return [getLocale];
46};
47exports.useLocaleReceiver = useLocaleReceiver;
\No newline at end of file