UNPKG

2.5 kBJavaScriptView Raw
1import _extends from "@babel/runtime/helpers/esm/extends";
2import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
3import _createClass from "@babel/runtime/helpers/esm/createClass";
4import _inherits from "@babel/runtime/helpers/esm/inherits";
5import _createSuper from "@babel/runtime/helpers/esm/createSuper";
6import * as React from 'react';
7import defaultLocaleData from './default';
8import LocaleContext from './context';
9
10var LocaleReceiver = /*#__PURE__*/function (_React$Component) {
11 _inherits(LocaleReceiver, _React$Component);
12
13 var _super = _createSuper(LocaleReceiver);
14
15 function LocaleReceiver() {
16 _classCallCheck(this, LocaleReceiver);
17
18 return _super.apply(this, arguments);
19 }
20
21 _createClass(LocaleReceiver, [{
22 key: "getLocale",
23 value: function getLocale() {
24 var _this$props = this.props,
25 componentName = _this$props.componentName,
26 defaultLocale = _this$props.defaultLocale;
27 var locale = defaultLocale || defaultLocaleData[componentName || 'global'];
28 var antLocale = this.context;
29 var localeFromContext = componentName && antLocale ? antLocale[componentName] : {};
30 return _extends(_extends({}, typeof locale === 'function' ? locale() : locale), localeFromContext || {});
31 }
32 }, {
33 key: "getLocaleCode",
34 value: function getLocaleCode() {
35 var antLocale = this.context;
36 var localeCode = antLocale && antLocale.locale; // Had use LocaleProvide but didn't set locale
37
38 if (antLocale && antLocale.exist && !localeCode) {
39 return defaultLocaleData.locale;
40 }
41
42 return localeCode;
43 }
44 }, {
45 key: "render",
46 value: function render() {
47 return this.props.children(this.getLocale(), this.getLocaleCode(), this.context);
48 }
49 }]);
50
51 return LocaleReceiver;
52}(React.Component);
53
54export { LocaleReceiver as default };
55LocaleReceiver.defaultProps = {
56 componentName: 'global'
57};
58LocaleReceiver.contextType = LocaleContext;
59export function useLocaleReceiver(componentName, defaultLocale) {
60 var antLocale = React.useContext(LocaleContext);
61 var componentLocale = React.useMemo(function () {
62 var locale = defaultLocale || defaultLocaleData[componentName || 'global'];
63 var localeFromContext = componentName && antLocale ? antLocale[componentName] : {};
64 return _extends(_extends({}, typeof locale === 'function' ? locale() : locale), localeFromContext || {});
65 }, [componentName, defaultLocale, antLocale]);
66 return [componentLocale];
67}
\No newline at end of file