linkmore-design
Version:
π πlmη»δ»ΆεΊγπ
37 lines β’ 2.09 kB
JavaScript
import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import * as React from 'react';
import LocaleContext from "./context";
import defaultLocaleData from "./default";
var LocaleReceiver = function LocaleReceiver(props) {
var _props$componentName = props.componentName,
componentName = _props$componentName === void 0 ? 'global' : _props$componentName,
defaultLocale = props.defaultLocale,
children = props.children;
var antLocale = React.useContext(LocaleContext);
var getLocale = React.useMemo(function () {
var _antLocale$componentN;
var locale = defaultLocale || defaultLocaleData[componentName];
var localeFromContext = (_antLocale$componentN = antLocale === null || antLocale === void 0 ? void 0 : antLocale[componentName]) !== null && _antLocale$componentN !== void 0 ? _antLocale$componentN : {};
return _objectSpread(_objectSpread({}, locale instanceof Function ? locale() : locale), localeFromContext || {});
}, [componentName, defaultLocale, antLocale]);
var getLocaleCode = React.useMemo(function () {
var localeCode = antLocale && antLocale.locale;
// Had use LocaleProvide but didn't set locale
if (antLocale && antLocale.exist && !localeCode) {
return defaultLocaleData.locale;
}
return localeCode;
}, [antLocale]);
return children(getLocale, getLocaleCode, antLocale);
};
export default LocaleReceiver;
export var useLocaleReceiver = function useLocaleReceiver(componentName, defaultLocale) {
var antLocale = React.useContext(LocaleContext);
var getLocale = React.useMemo(function () {
var _antLocale$componentN2;
var locale = defaultLocale || defaultLocaleData[componentName];
var localeFromContext = (_antLocale$componentN2 = antLocale === null || antLocale === void 0 ? void 0 : antLocale[componentName]) !== null && _antLocale$componentN2 !== void 0 ? _antLocale$componentN2 : {};
return _objectSpread(_objectSpread({}, typeof locale === 'function' ? locale() : locale), localeFromContext || {});
}, [componentName, defaultLocale, antLocale]);
return [getLocale];
};