acud
Version:
acg react 组件库
64 lines • 2.89 kB
JavaScript
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 _possibleConstructorReturn from "@babel/runtime/helpers/esm/possibleConstructorReturn";
import _isNativeReflectConstruct from "@babel/runtime/helpers/esm/isNativeReflectConstruct";
import _getPrototypeOf from "@babel/runtime/helpers/esm/getPrototypeOf";
import _inherits from "@babel/runtime/helpers/esm/inherits";
function _callSuper(t, o, e) { return o = _getPrototypeOf(o), _possibleConstructorReturn(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], _getPrototypeOf(t).constructor) : o.apply(t, e)); }
/**
* @file
* @author zhousheng
*/
import * as React from 'react';
import defaultLocaleData from './default';
import LocaleContext from './context';
var LocaleReceiver = /*#__PURE__*/function (_React$Component) {
function LocaleReceiver() {
_classCallCheck(this, LocaleReceiver);
return _callSuper(this, LocaleReceiver, arguments);
}
_inherits(LocaleReceiver, _React$Component);
return _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 acudLocale = this.context;
var localeFromContext = componentName && acudLocale ? acudLocale[componentName] : {};
return _extends(_extends({}, typeof locale === 'function' ? locale() : locale), localeFromContext || {});
}
}, {
key: "getLocaleCode",
value: function getLocaleCode() {
var acudLocale = this.context;
var localeCode = acudLocale && acudLocale.locale;
// Had use LocaleProvide but didn't set locale
if (acudLocale && acudLocale.exist && !localeCode) {
return defaultLocaleData.locale;
}
return localeCode;
}
}, {
key: "render",
value: function render() {
return this.props.children(this.getLocale(), this.getLocaleCode(), this.context);
}
}]);
}(React.Component);
export { LocaleReceiver as default };
LocaleReceiver.defaultProps = {
componentName: 'global'
};
LocaleReceiver.contextType = LocaleContext;
export function useLocaleReceiver(componentName, defaultLocale) {
var acudLocale = React.useContext(LocaleContext);
var componentLocale = React.useMemo(function () {
var locale = defaultLocale || defaultLocaleData[componentName || 'global'];
var localeFromContext = componentName && acudLocale ? acudLocale[componentName] : {};
return _extends(_extends({}, typeof locale === 'function' ? locale() : locale), localeFromContext || {});
}, [componentName, defaultLocale, acudLocale]);
return [componentLocale];
}