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.
67 lines
2.5 KiB
67 lines
2.5 KiB
import _extends from "@babel/runtime/helpers/esm/extends";
|
|
import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
|
|
import _createClass from "@babel/runtime/helpers/esm/createClass";
|
|
import _inherits from "@babel/runtime/helpers/esm/inherits";
|
|
import _createSuper from "@babel/runtime/helpers/esm/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 !== null && componentName !== void 0 ? componentName : 'global'];
|
|
var antLocale = this.context;
|
|
var localeFromContext = componentName && antLocale ? antLocale[componentName] : {};
|
|
return _extends(_extends({}, locale instanceof 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];
|
|
} |