mirror of
https://github.com/ant-design/ant-design.git
synced 2024-11-25 03:29:59 +08:00
cefb7b29f2
* Upgrade locale context * fix logic * fix lint * add context ignore in test case
52 lines
1.4 KiB
TypeScript
52 lines
1.4 KiB
TypeScript
import * as React from 'react';
|
|
import defaultLocaleData from './default';
|
|
import LocaleContext from './context';
|
|
|
|
export interface LocaleReceiverProps {
|
|
componentName?: string;
|
|
defaultLocale?: object | Function;
|
|
children: (locale: object, localeCode?: string, fullLocale?: object) => React.ReactNode;
|
|
}
|
|
|
|
interface LocaleInterface {
|
|
[key: string]: any;
|
|
}
|
|
|
|
export interface LocaleReceiverContext {
|
|
antLocale?: LocaleInterface;
|
|
}
|
|
|
|
export default class LocaleReceiver extends React.Component<LocaleReceiverProps> {
|
|
static defaultProps = {
|
|
componentName: 'global',
|
|
};
|
|
|
|
static contextType = LocaleContext;
|
|
|
|
getLocale() {
|
|
const { componentName, defaultLocale } = this.props;
|
|
const locale: object | Function =
|
|
defaultLocale || (defaultLocaleData as LocaleInterface)[componentName || 'global'];
|
|
const antLocale = this.context;
|
|
const localeFromContext = componentName && antLocale ? antLocale[componentName] : {};
|
|
return {
|
|
...(typeof locale === 'function' ? locale() : locale),
|
|
...(localeFromContext || {}),
|
|
};
|
|
}
|
|
|
|
getLocaleCode() {
|
|
const antLocale = this.context;
|
|
const localeCode = antLocale && antLocale.locale;
|
|
// Had use LocaleProvide but didn't set locale
|
|
if (antLocale && antLocale.exist && !localeCode) {
|
|
return defaultLocaleData.locale;
|
|
}
|
|
return localeCode;
|
|
}
|
|
|
|
render() {
|
|
return this.props.children(this.getLocale(), this.getLocaleCode(), this.context);
|
|
}
|
|
}
|