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